Skip to content

Commit

Permalink
feat(components/commandList): add 'selectCurrent' imperative handle
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Sep 26, 2023
1 parent a128ae8 commit d3bb098
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/sanity/src/core/components/commandList/CommandList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,29 @@ export const CommandList = forwardRef<CommandListHandle, CommandListProps>(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<HTMLElement>(
LIST_ITEM_INTERACTIVE_SELECTOR,
)
clickableElement?.click()
}
},
}
},
[
childContainerElement?.children,
enableChildContainerPointerEvents,
focusInputElement,
focusListElement,
handleGetTopIndex,
itemIndices,
setActiveIndex,
],
)
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/components/commandList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface CommandListHandle {
focusListElement: () => void
getTopIndex: () => number
scrollToIndex: (index: number) => void
selectCurrent: () => void
}

/** @internal */
Expand Down

0 comments on commit d3bb098

Please sign in to comment.