:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(lemon-ui): no-results message and keyboard nav in LemonSearchableSelect (#70130)
Thiago Salvatorefd7f80b0ccb4
diff --git a/frontend/src/lib/lemon-ui/LemonSelect/LemonSearchableSelect.tsx b/frontend/src/lib/lemon-ui/LemonSelect/LemonSearchableSelect.tsxindex 9468e65e0b33..10bce7180fd2 100644--- a/frontend/src/lib/lemon-ui/LemonSelect/LemonSearchableSelect.tsx+++ b/frontend/src/lib/lemon-ui/LemonSelect/LemonSearchableSelect.tsx@@ -1,3 +1,4 @@+import clsx from 'clsx'import { useMemo, useState } from 'react'import { LemonInput } from '@posthog/lemon-ui'@@ -20,6 +21,8 @@ export interface LemonSearchableSelectPropsBase<T> extends LemonSelectPropsBase<searchKeys?: string[]/** `data-attr` for the search input, so its usage can be tracked per call site. */searchInputDataAttr?: string+ /** Message shown when a search term matches no options. */+ noResultsMessage?: string}export interface LemonSearchableSelectPropsClearable<T>@@ -88,20 +91,95 @@ function filterOptions<T>(return options.map((item) => filterStructure(item, matchedOptions)).filter(Boolean) as LemonSelectOptions<T>}+// A searchable option is a selectable leaf rendered as a plain menu-item button — not a custom+// control (e.g. a labelInMenu render function) and not a hidden option.+function getSearchableLeaves<T>(options: LemonSelectOptions<T>): LemonSelectOption<T>[] {+ return flattenOptions(options).filter(+ (option) => 'value' in option && typeof option.labelInMenu !== 'function' && !option.hidden+ )+}++// Rebuild the option tree with the active option carrying the standard LemonButton highlight class,+// so keyboard navigation highlights it the same way hovering does.+function highlightActiveOption<T>(+ options: LemonSelectOptions<T>,+ activeOption: LemonSelectOption<T> | null+): LemonSelectOptions<T> {+ if (!activeOption) {+ return options+ }+ const mark = (item: LemonSelectOption<T> | LemonSelectSection<T>): typeof item => {+ if ('options' in item) {+ return { ...item, options: item.options.map(mark) } as typeof item+ }+ return item === activeOption ? { ...item, className: clsx(item.className, 'LemonButton--active') } : item+ }+ return options.map(mark) as LemonSelectOptions<T>+}++const POPOVER_BOX_SELECTOR = '.Popover__box'+const MENU_ITEM_SELECTOR = 'button[role="menuitem"]'+export function LemonSearchableSelect<T extends string | number | boolean | null>({searchPlaceholder,searchKeys = ['label'],searchInputDataAttr = 'lemon-searchable-select-search',+ noResultsMessage = 'No results',onChange,onSelect,...selectProps}: LemonSearchableSelectProps<T>): JSX.Element {const [searchTerm, setSearchTerm] = useState('')+ // Index into `navigableOptions` of the row highlighted via arrow keys, or -1 for the search box.+ const [activeIndex, setActiveIndex] = useState(-1)const filteredOptions = useMemo(() => {return filterOptions(selectProps.options, searchTerm, searchKeys)}, [selectProps.options, searchTerm, searchKeys])+ const navigableOptions = useMemo(() => getSearchableLeaves(filteredOptions), [filteredOptions])+ const activeOption =+ activeIndex >= 0 && activeIndex < navigableOptions.length ? navigableOptions[activeIndex] : null++ const handleSearchChange = (newSearchTerm: string): void => {+ setSearchTerm(newSearchTerm)+ setActiveIndex(-1)+ }++ const scrollMenuItemIntoView = (fromElement: HTMLElement, index: number): void => {+ const menuItems = fromElement.closest(POPOVER_BOX_SELECTOR)?.querySelectorAll(MENU_ITEM_SELECTOR)+ menuItems?.[index]?.scrollIntoView({ block: 'nearest' })+ }++ const handleSearchKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => {+ if (e.key === 'ArrowDown') {+ // Stop propagation so an enclosing form (e.g. LemonFormDialog) doesn't also act on the key.+ e.preventDefault()+ e.stopPropagation()+ const nextIndex = Math.min(activeIndex + 1, navigableOptions.length - 1)+ setActiveIndex(nextIndex)+ scrollMenuItemIntoView(e.currentTarget, nextIndex)+ } else if (e.key === 'ArrowUp') {+ e.preventDefault()+ e.stopPropagation()+ const nextIndex = Math.max(activeIndex - 1, -1)+ setActiveIndex(nextIndex)+ if (nextIndex >= 0) {+ scrollMenuItemIntoView(e.currentTarget, nextIndex)+ }+ } else if (e.key === 'Enter') {+ // Keep Enter scoped to the dropdown so it never submits an enclosing form (e.g. LemonFormDialog),+ // even when no result is highlighted yet.+ e.preventDefault()+ e.stopPropagation()+ if (activeOption) {+ // Click the highlighted menu item so it goes through the same select-and-close path as a mouse click.+ const menuItems = e.currentTarget.closest(POPOVER_BOX_SELECTOR)?.querySelectorAll(MENU_ITEM_SELECTOR)+ ;(menuItems?.[activeIndex] as HTMLElement | undefined)?.click()+ }+ }+ }+// Add search input as first menu itemconst optionsWithSearch = useMemo(() => {const searchMenuItem: LemonSelectOption<T> = {@@ -111,7 +189,8 @@ export function LemonSearchableSelect<T extends string | number | boolean | nullplaceholder={searchPlaceholder || 'Search'}autoFocusvalue={searchTerm}- onChange={setSearchTerm}+ onChange={handleSearchChange}+ onKeyDown={handleSearchKeyDown}fullWidthonClick={(e) => e.stopPropagation()}className="mb-1"@@ -121,13 +200,22 @@ export function LemonSearchableSelect<T extends string | number | boolean | nullcustom: true,} as any- return [searchMenuItem, ...filteredOptions] as LemonSelectOptions<T>- }, [searchPlaceholder, searchTerm, filteredOptions, searchInputDataAttr])+ const hasNoResults = searchTerm.length > 0 && filteredOptions.length === 0+ const emptyMenuItem: LemonSelectOption<T> = {+ label: () => <div className="px-2 py-1.5 text-secondary">{noResultsMessage}</div>,+ custom: true,+ } as any++ const listOptions = hasNoResults ? [emptyMenuItem] : highlightActiveOption(filteredOptions, activeOption)+ return [searchMenuItem, ...listOptions] as LemonSelectOptions<T>+ // handleSearchKeyDown/handleSearchChange are stable enough for this memo; activeOption drives the highlight.+ }, [searchPlaceholder, searchTerm, filteredOptions, noResultsMessage, searchInputDataAttr, activeOption]) // eslint-disable-line react-hooks/exhaustive-depsconst handleChange = (newValue: T | null): void => {// Cast to `any` because `onChange` is a union type (T vs T | null) and TS can't infer it here.onChange?.(newValue as any)setSearchTerm('')+ setActiveIndex(-1)}const handleOnSelect = (newValue: T | null): void => {@@ -149,6 +237,7 @@ export function LemonSearchableSelect<T extends string | number | boolean | null// selected option filtered out and the trigger falls back to rendering the raw value.if (!visible) {setSearchTerm('')+ setActiveIndex(-1)}},}}