From d3bb098189c671a2ca72a64503ae81a98eee11fe Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Fri, 15 Sep 2023 13:06:30 +0200 Subject: [PATCH] feat(components/commandList): add 'selectCurrent' imperative handle --- .../core/components/commandList/CommandList.tsx | 16 ++++++++++++++++ .../src/core/components/commandList/types.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/packages/sanity/src/core/components/commandList/CommandList.tsx b/packages/sanity/src/core/components/commandList/CommandList.tsx index 372803dcbdc0..ce987c3ca1c8 100644 --- a/packages/sanity/src/core/components/commandList/CommandList.tsx +++ b/packages/sanity/src/core/components/commandList/CommandList.tsx @@ -463,13 +463,29 @@ export const CommandList = forwardRef(funct setActiveIndex({index}) enableChildContainerPointerEvents(true) }, + selectCurrent() { + const childElements = Array.from(childContainerElement?.children || []) as HTMLElement[] + const currentElement = childElements.find( + (el) => + Number(el.dataset.index) === + itemIndices.findIndex((i) => i.activeIndex === activeIndexRef.current), + ) + if (currentElement) { + const clickableElement = currentElement?.querySelector( + LIST_ITEM_INTERACTIVE_SELECTOR, + ) + clickableElement?.click() + } + }, } }, [ + childContainerElement?.children, enableChildContainerPointerEvents, focusInputElement, focusListElement, handleGetTopIndex, + itemIndices, setActiveIndex, ], ) diff --git a/packages/sanity/src/core/components/commandList/types.ts b/packages/sanity/src/core/components/commandList/types.ts index 26867fc6a500..5c29926ea342 100644 --- a/packages/sanity/src/core/components/commandList/types.ts +++ b/packages/sanity/src/core/components/commandList/types.ts @@ -34,6 +34,7 @@ export interface CommandListHandle { focusListElement: () => void getTopIndex: () => number scrollToIndex: (index: number) => void + selectCurrent: () => void } /** @internal */