mirror of
https://github.com/twentyhq/twenty.git
synced 2026-05-19 05:53:23 -04:00
Refactor input focus management in selectable lists
- Removed the `SELECTABLE_LIST_BLURRED_ATTRIBUTE` constant and replaced its usage with the new `isSelectableListGridFocusedState` atom for managing input focus state. - Updated `useInputFocusWithoutScrollOnMount` to utilize the new state for determining focus behavior. - Modified `useSelectableListHotKeys` to set the focus state atom when blurring and refocusing inputs. These changes streamline the focus management logic and improve the overall user experience in selectable lists.
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import { useStore } from 'jotai';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { SELECTABLE_LIST_BLURRED_ATTRIBUTE } from '@/ui/layout/selectable-list/constants/SelectableListBlurredAttribute';
|
||||
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) &&
|
||||
inputRef.current.dataset[SELECTABLE_LIST_BLURRED_ATTRIBUTE] !== 'true'
|
||||
!store.get(isSelectableListGridFocusedState.atom)
|
||||
) {
|
||||
inputRef.current.focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export const SELECTABLE_LIST_BLURRED_ATTRIBUTE = 'selectableListBlurred';
|
||||
@@ -3,7 +3,7 @@ import { useStore } from 'jotai';
|
||||
import { useCallback, useRef } from 'react';
|
||||
import { Key } from 'ts-key-enum';
|
||||
|
||||
import { SELECTABLE_LIST_BLURRED_ATTRIBUTE } from '@/ui/layout/selectable-list/constants/SelectableListBlurredAttribute';
|
||||
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';
|
||||
@@ -24,8 +24,7 @@ export const useSelectableListHotKeys = (
|
||||
const blurActiveInputIfNeeded = () => {
|
||||
if (document.activeElement instanceof HTMLInputElement) {
|
||||
lastBlurredInputRef.current = document.activeElement;
|
||||
document.activeElement.dataset[SELECTABLE_LIST_BLURRED_ATTRIBUTE] =
|
||||
'true';
|
||||
store.set(isSelectableListGridFocusedState.atom, true);
|
||||
document.activeElement.blur();
|
||||
}
|
||||
};
|
||||
@@ -34,9 +33,7 @@ export const useSelectableListHotKeys = (
|
||||
if (!lastBlurredInputRef.current) {
|
||||
return;
|
||||
}
|
||||
delete lastBlurredInputRef.current.dataset[
|
||||
SELECTABLE_LIST_BLURRED_ATTRIBUTE
|
||||
];
|
||||
store.set(isSelectableListGridFocusedState.atom, false);
|
||||
lastBlurredInputRef.current.focus();
|
||||
lastBlurredInputRef.current = null;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';
|
||||
|
||||
export const isSelectableListGridFocusedState = createAtomState<boolean>({
|
||||
key: 'isSelectableListGridFocusedState',
|
||||
defaultValue: false,
|
||||
});
|
||||
Reference in New Issue
Block a user