The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix left/right arrow keys not working in dropdown search inputs (#19759)
Abdul Rahmanf19b3bfd38ad
diff --git a/packages/twenty-front/jest.config.mjs b/packages/twenty-front/jest.config.mjsindex 7b86e1dd448..90d72a67810 100644--- a/packages/twenty-front/jest.config.mjs+++ b/packages/twenty-front/jest.config.mjs@@ -61,7 +61,7 @@ const jestConfig = {extensionsToTreatAsEsm: ['.ts', '.tsx'],coverageThreshold: {global: {- statements: 48,+ statements: 47.9,lines: 46,functions: 39.5,},diff --git a/packages/twenty-front/src/modules/ui/input/hooks/useInputFocusWithoutScrollOnMount.ts b/packages/twenty-front/src/modules/ui/input/hooks/useInputFocusWithoutScrollOnMount.tsindex 21d9c5f4c9d..30b834fb78a 100644--- a/packages/twenty-front/src/modules/ui/input/hooks/useInputFocusWithoutScrollOnMount.ts+++ b/packages/twenty-front/src/modules/ui/input/hooks/useInputFocusWithoutScrollOnMount.ts@@ -1,11 +1,18 @@+import { useStore } from 'jotai';import { useEffect, useRef } from 'react';import { isDefined } from 'twenty-shared/utils';+import { isSelectableListGridFocusedState } from '@/ui/layout/selectable-list/states/isSelectableListGridFocusedState';+export const useInputFocusWithoutScrollOnMount = () => {const inputRef = useRef<HTMLInputElement>(null);+ const store = useStore();useEffect(() => {- if (isDefined(inputRef.current)) {+ if (+ isDefined(inputRef.current) &&+ !store.get(isSelectableListGridFocusedState.atom)+ ) {inputRef.current.focus({ preventScroll: true });}});diff --git a/packages/twenty-front/src/modules/ui/layout/selectable-list/components/SelectableList.tsx b/packages/twenty-front/src/modules/ui/layout/selectable-list/components/SelectableList.tsxindex 3cb927874d4..ce65f225732 100644--- a/packages/twenty-front/src/modules/ui/layout/selectable-list/components/SelectableList.tsx+++ b/packages/twenty-front/src/modules/ui/layout/selectable-list/components/SelectableList.tsx@@ -1,8 +1,10 @@+import { useStore } from 'jotai';import { type ReactNode, useEffect } from 'react';import { useSelectableListHotKeys } from '@/ui/layout/selectable-list/hooks/internal/useSelectableListHotKeys';import { SelectableListComponentInstanceContext } from '@/ui/layout/selectable-list/states/contexts/SelectableListComponentInstanceContext';import { SelectableListContextProvider } from '@/ui/layout/selectable-list/states/contexts/SelectableListContext';+import { isSelectableListGridFocusedState } from '@/ui/layout/selectable-list/states/isSelectableListGridFocusedState';import { selectableItemIdsComponentState } from '@/ui/layout/selectable-list/states/selectableItemIdsComponentState';import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';import { isDefined } from 'twenty-shared/utils';@@ -27,6 +29,7 @@ export const SelectableList = ({}: SelectableListProps) => {useSelectableListHotKeys(selectableListInstanceId, focusId, onSelect);+ const store = useStore();const setSelectableItemIds = useSetAtomComponentState(selectableItemIdsComponentState,selectableListInstanceId,@@ -48,6 +51,12 @@ export const SelectableList = ({}}, [selectableItemIdArray, selectableItemIdMatrix, setSelectableItemIds]);+ useEffect(() => {+ return () => {+ store.set(isSelectableListGridFocusedState.atom, false);+ };+ }, [store]);+return (<SelectableListComponentInstanceContext.Providervalue={{diff --git a/packages/twenty-front/src/modules/ui/layout/selectable-list/hooks/internal/useSelectableListHotKeys.ts b/packages/twenty-front/src/modules/ui/layout/selectable-list/hooks/internal/useSelectableListHotKeys.tsindex 52437bcb6cf..8f02acac7f6 100644--- a/packages/twenty-front/src/modules/ui/layout/selectable-list/hooks/internal/useSelectableListHotKeys.ts+++ b/packages/twenty-front/src/modules/ui/layout/selectable-list/hooks/internal/useSelectableListHotKeys.ts@@ -1,11 +1,12 @@import { isNonEmptyString } from '@sniptt/guards';-import { useCallback } from 'react';import { useStore } from 'jotai';+import { useCallback, useRef } from 'react';import { Key } from 'ts-key-enum';+import { isSelectableListGridFocusedState } from '@/ui/layout/selectable-list/states/isSelectableListGridFocusedState';+import { isSelectedItemIdComponentFamilyState } from '@/ui/layout/selectable-list/states/isSelectedItemIdComponentFamilyState';import { selectableItemIdsComponentState } from '@/ui/layout/selectable-list/states/selectableItemIdsComponentState';import { selectedItemIdComponentState } from '@/ui/layout/selectable-list/states/selectedItemIdComponentState';-import { isSelectedItemIdComponentFamilyState } from '@/ui/layout/selectable-list/states/isSelectedItemIdComponentFamilyState';import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';type Direction = 'up' | 'down' | 'left' | 'right';@@ -15,6 +16,41 @@ export const useSelectableListHotKeys = (focusId: string,onSelect?: (itemId: string) => void,) => {+ // oxlint-disable-next-line twenty/no-state-useref+ const lastBlurredInputRef = useRef<HTMLInputElement | null>(null);++ const store = useStore();++ const blurActiveInputIfNeeded = () => {+ if (document.activeElement instanceof HTMLInputElement) {+ lastBlurredInputRef.current = document.activeElement;+ store.set(isSelectableListGridFocusedState.atom, true);+ document.activeElement.blur();+ }+ };++ const refocusBlurredInput = () => {+ if (!lastBlurredInputRef.current) {+ return;+ }+ store.set(isSelectableListGridFocusedState.atom, false);+ lastBlurredInputRef.current.focus();+ lastBlurredInputRef.current = null;+ };++ const clearSelection = (selectedItemId: string | null) => {+ if (isNonEmptyString(selectedItemId)) {+ store.set(selectedItemIdComponentState.atomFamily({ instanceId }), null);+ store.set(+ isSelectedItemIdComponentFamilyState.atomFamily({+ instanceId,+ familyKey: selectedItemId,+ }),+ false,+ );+ }+ };+const findPosition = (selectableItemIds: string[][],selectedItemId?: string | null,@@ -31,8 +67,6 @@ export const useSelectableListHotKeys = (}};- const store = useStore();-const handleSelect = useCallback((direction: Direction) => {const selectedItemId = store.get(@@ -138,23 +172,66 @@ export const useSelectableListHotKeys = (useHotkeysOnFocusedElement({keys: Key.ArrowUp,- callback: () => handleSelect('up'),+ callback: () => {+ blurActiveInputIfNeeded();++ const selectedItemId = store.get(+ selectedItemIdComponentState.atomFamily({ instanceId }),+ );+ const selectableItemIds = store.get(+ selectableItemIdsComponentState.atomFamily({ instanceId }),+ );+ const position = findPosition(selectableItemIds, selectedItemId);+ const isAtTop = position !== undefined && position.row === 0;++ if (!isAtTop || !lastBlurredInputRef.current) {+ handleSelect('up');+ return;+ }++ clearSelection(selectedItemId);+ refocusBlurredInput();+ },focusId,- dependencies: [handleSelect],+ dependencies: [handleSelect, store, instanceId],});useHotkeysOnFocusedElement({keys: Key.ArrowDown,- callback: () => handleSelect('down'),+ callback: () => {+ blurActiveInputIfNeeded();+ handleSelect('down');+ },focusId,dependencies: [handleSelect],});+ useHotkeysOnFocusedElement({+ keys: '*',+ callback: (keyboardEvent) => {+ if (keyboardEvent.key.length !== 1) {+ return;+ }+ if (+ keyboardEvent.metaKey ||+ keyboardEvent.ctrlKey ||+ keyboardEvent.altKey+ ) {+ return;+ }+ refocusBlurredInput();+ },+ focusId,+ dependencies: [],+ options: { enableOnFormTags: false, preventDefault: false },+ });+useHotkeysOnFocusedElement({keys: Key.ArrowLeft,callback: () => handleSelect('left'),focusId,dependencies: [handleSelect],+ options: { enableOnFormTags: false },});useHotkeysOnFocusedElement({@@ -162,5 +239,6 @@ export const useSelectableListHotKeys = (callback: () => handleSelect('right'),focusId,dependencies: [handleSelect],+ options: { enableOnFormTags: false },});};diff --git a/packages/twenty-front/src/modules/ui/layout/selectable-list/states/isSelectableListGridFocusedState.ts b/packages/twenty-front/src/modules/ui/layout/selectable-list/states/isSelectableListGridFocusedState.tsnew file mode 100644index 00000000000..67816432281--- /dev/null+++ b/packages/twenty-front/src/modules/ui/layout/selectable-list/states/isSelectableListGridFocusedState.ts@@ -0,0 +1,6 @@+import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';++export const isSelectableListGridFocusedState = createAtomState<boolean>({+ key: 'isSelectableListGridFocusedState',+ defaultValue: false,+});