The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Keep the first keystroke typed into a dropdown search (#27148)
Raphaël Bosi5f5ff323a0a4
diff --git a/packages/twenty-ui/src/components/navigation/Dropdown/__stories__/Dropdown.picker.stories.tsx b/packages/twenty-ui/src/components/navigation/Dropdown/__stories__/Dropdown.picker.stories.tsxindex 43a29354724..e393cf24fe1 100644--- a/packages/twenty-ui/src/components/navigation/Dropdown/__stories__/Dropdown.picker.stories.tsx+++ b/packages/twenty-ui/src/components/navigation/Dropdown/__stories__/Dropdown.picker.stories.tsx@@ -17,8 +17,14 @@ const onSelectDisabledOption = fn();const onOpenChange = fn();const onSearchChange = fn();-const SearchablePicker = ({ multiple = false }: { multiple?: boolean }) => {- const [search, setSearch] = useState('');+const SearchablePicker = ({+ multiple = false,+ initialSearch = '',+}: {+ multiple?: boolean;+ initialSearch?: string;+}) => {+ const [search, setSearch] = useState(initialSearch);const [selectedPeople, setSelectedPeople] = useState<string[]>([]);const results = PEOPLE.filter((person) =>person.toLowerCase().includes(search.toLowerCase()),@@ -179,6 +185,90 @@ export const HighlightFollowsSearch: Story = {},};+export const TypingRightAfterOpening: Story = {+ tags: ['!dev'],+ render: () => <SearchablePicker />,+ play: async ({ canvasElement }) => {+ const { userEvent: trustedUserEvent } = await import('vitest/browser');+ const body = within(canvasElement.ownerDocument.body);+ const search = await openAssignees(canvasElement);++ await trustedUserEvent.keyboard('gra');++ expect(search).toHaveValue('gra');+ expect(+ body.queryByRole('button', { name: 'Ada Lovelace' }),+ ).not.toBeInTheDocument();+ const grace = body.getByRole('button', { name: 'Grace Hopper' });++ await waitFor(() => expect(grace).toHaveAttribute('data-highlighted'));+ expect(search).toHaveAttribute('aria-activedescendant', grace.id);++ await trustedUserEvent.keyboard('{Enter}');+ expect(onSelectPerson).toHaveBeenCalledOnce();+ expect(onSelectPerson).toHaveBeenCalledWith('Grace Hopper');+ },+};++export const BackspaceClearingSearch: Story = {+ tags: ['!dev'],+ render: () => <SearchablePicker initialSearch="m" />,+ play: async ({ canvasElement }) => {+ const { userEvent: trustedUserEvent } = await import('vitest/browser');+ const body = within(canvasElement.ownerDocument.body);+ const search = await openAssignees(canvasElement);+ const margaret = body.getByRole('button', { name: 'Margaret Hamilton' });++ await waitFor(() => expect(margaret).toHaveAttribute('data-highlighted'));++ await trustedUserEvent.keyboard('{Backspace}');++ expect(search).toHaveValue('');+ expect(body.getByRole('button', { name: 'Ada Lovelace' })).toBeVisible();+ await waitFor(() =>+ expect(search).not.toHaveAttribute('aria-activedescendant'),+ );+ expect(margaret).not.toHaveAttribute('data-highlighted');+ },+};++export const HighlightFollowsTypedSearch: Story = {+ tags: ['!dev'],+ render: () => <SearchablePicker />,+ play: async ({ canvasElement }) => {+ const { userEvent: trustedUserEvent } = await import('vitest/browser');+ const body = within(canvasElement.ownerDocument.body);+ const search = await openAssignees(canvasElement);+ const ada = body.getByRole('button', { name: 'Ada Lovelace' });++ await trustedUserEvent.keyboard('a');++ expect(search).toHaveValue('a');+ await waitFor(() => expect(ada).toHaveAttribute('data-highlighted'));+ expect(search).toHaveAttribute('aria-activedescendant', ada.id);++ await trustedUserEvent.keyboard('{Backspace}');++ expect(search).toHaveValue('');+ await waitFor(() =>+ expect(search).not.toHaveAttribute('aria-activedescendant'),+ );+ expect(ada).not.toHaveAttribute('data-highlighted');++ await trustedUserEvent.keyboard('x');++ expect(search).toHaveValue('x');+ expect(body.getByRole('status')).toHaveTextContent('No people found');+ await waitFor(() =>+ expect(search).not.toHaveAttribute('aria-activedescendant'),+ );++ await trustedUserEvent.keyboard('{Enter}');+ expect(onSelectPerson).not.toHaveBeenCalled();+ expect(body.getByRole('dialog')).toBeVisible();+ },+};+export const FirstEnabledResult: Story = {render: () => (<Dropdown.Root type="picker">diff --git a/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownContent.tsx b/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownContent.tsxindex f3cdd104e00..4a3545880fc 100644--- a/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownContent.tsx+++ b/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownContent.tsx@@ -11,6 +11,7 @@ import { type DropdownContentProps } from '../types/DropdownContentProps';import { DropdownPageFocusEffect } from './DropdownPageFocusEffect';import { DropdownSearchTargetEffect } from './DropdownSearchTargetEffect';import { getDropdownFocusTarget } from './getDropdownFocusTarget';+import { getDropdownSearchTarget } from './getDropdownSearchTarget';import { isUnhandledModifierShortcut } from './isUnhandledModifierShortcut';import { useDropdownContext } from './useDropdownContext';import { useDropdownKeyboardNavigation } from './useDropdownKeyboardNavigation';@@ -29,6 +30,7 @@ export const DropdownContent = ({children,initialFocus,onKeyDown,+ onInput,onClick,onMouseDown,onPointerDown,@@ -45,6 +47,7 @@ export const DropdownContent = ({focusOnOpen,triggerId,titleId,+ setSearchTargetId,} = useDropdownContext();const contentRef = useRef<HTMLDivElement>(null);const [content, setContent] = useState<HTMLDivElement | null>(null);@@ -106,6 +109,10 @@ export const DropdownContent = ({event.stopPropagation();}}}+ onInput={(event) => {+ onInput?.(event);+ setSearchTargetId(getDropdownSearchTarget(event.currentTarget)?.id);+ }}onClick={(event) => {event.stopPropagation();onClick?.(event);diff --git a/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownSearchTargetEffect.tsx b/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownSearchTargetEffect.tsxindex 5470adf758d..3f098179468 100644--- a/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownSearchTargetEffect.tsx+++ b/packages/twenty-ui/src/components/navigation/Dropdown/internal/DropdownSearchTargetEffect.tsx@@ -32,11 +32,9 @@ export const DropdownSearchTargetEffect = ({attributes: true,attributeFilter: ['disabled', 'aria-disabled'],});- content.addEventListener('input', updateSearchTarget);return () => {observer.disconnect();- content.removeEventListener('input', updateSearchTarget);setSearchTargetId(undefined);};}, [content, setSearchTargetId]);diff --git a/packages/twenty-ui/src/primitives/input/Select/__stories__/Select.stories.tsx b/packages/twenty-ui/src/primitives/input/Select/__stories__/Select.stories.tsxindex 52088d777df..36dd840277b 100644--- a/packages/twenty-ui/src/primitives/input/Select/__stories__/Select.stories.tsx+++ b/packages/twenty-ui/src/primitives/input/Select/__stories__/Select.stories.tsx@@ -90,7 +90,7 @@ export const Multiple: Story = {await expect(trigger).toHaveTextContent('Cherry');await userEvent.keyboard('{Escape}');await waitFor(() => expect(popup).not.toBeVisible());- await expect(trigger).toHaveFocus();+ await waitFor(() => expect(trigger).toHaveFocus());},};