Fix save record table cell when clicking outside table (#8230)

This commit is contained in:
Baptiste Devessier 2024-10-31 11:45:40 +01:00 committed by GitHub
parent f5bde0251b
commit d0dc8bae4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 35 deletions

View File

@ -1,19 +1,26 @@
import { Key } from 'ts-key-enum'; import { Key } from 'ts-key-enum';
import { RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID } from '@/object-record/record-table/constants/RecordTableClickOutsideListenerId';
import { useLeaveTableFocus } from '@/object-record/record-table/hooks/internal/useLeaveTableFocus';
import { useRecordTable } from '@/object-record/record-table/hooks/useRecordTable'; import { useRecordTable } from '@/object-record/record-table/hooks/useRecordTable';
import { TableHotkeyScope } from '@/object-record/record-table/types/TableHotkeyScope'; import { TableHotkeyScope } from '@/object-record/record-table/types/TableHotkeyScope';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { useListenClickOutsideByClassName } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside'; import { useListenClickOutsideV2 } from '@/ui/utilities/pointer-event/hooks/useListenClickOutsideV2';
type RecordTableInternalEffectProps = { type RecordTableInternalEffectProps = {
recordTableId: string; recordTableId: string;
tableBodyRef: React.RefObject<HTMLDivElement>;
}; };
export const RecordTableInternalEffect = ({ export const RecordTableInternalEffect = ({
recordTableId, recordTableId,
tableBodyRef,
}: RecordTableInternalEffectProps) => { }: RecordTableInternalEffectProps) => {
const { leaveTableFocus, resetTableRowSelection, useMapKeyboardToSoftFocus } = const leaveTableFocus = useLeaveTableFocus(recordTableId);
useRecordTable({ recordTableId });
const { resetTableRowSelection, useMapKeyboardToSoftFocus } = useRecordTable({
recordTableId,
});
useMapKeyboardToSoftFocus(); useMapKeyboardToSoftFocus();
@ -25,9 +32,9 @@ export const RecordTableInternalEffect = ({
TableHotkeyScope.Table, TableHotkeyScope.Table,
); );
useListenClickOutsideByClassName({ useListenClickOutsideV2({
classNames: ['entity-table-cell'], listenerId: RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID,
excludeClassNames: ['bottom-bar', 'action-menu-dropdown', 'command-menu'], refs: [tableBodyRef],
callback: () => { callback: () => {
leaveTableFocus(); leaveTableFocus();
}, },

View File

@ -87,7 +87,10 @@ export const RecordTableWithWrappers = ({
onDragSelectionChange={setRowSelected} onDragSelectionChange={setRowSelected}
/> />
</StyledTableInternalContainer> </StyledTableInternalContainer>
<RecordTableInternalEffect recordTableId={recordTableId} /> <RecordTableInternalEffect
tableBodyRef={tableBodyRef}
recordTableId={recordTableId}
/>
</StyledTableContainer> </StyledTableContainer>
</StyledTableWithHeader> </StyledTableWithHeader>
</RecordUpdateContext.Provider> </RecordUpdateContext.Provider>

View File

@ -0,0 +1 @@
export const RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID = 'record-table';

View File

@ -1,19 +1,13 @@
import { useRecoilCallback } from 'recoil'; import { useRecoilCallback } from 'recoil';
import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates'; import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates';
import { currentHotkeyScopeState } from '@/ui/utilities/hotkey/states/internal/currentHotkeyScopeState';
import { getSnapshotValue } from '@/ui/utilities/recoil-scope/utils/getSnapshotValue'; import { getSnapshotValue } from '@/ui/utilities/recoil-scope/utils/getSnapshotValue';
import { TableHotkeyScope } from '../../types/TableHotkeyScope';
import { useResetTableRowSelection } from '@/object-record/record-table/hooks/internal/useResetTableRowSelection'; import { useResetTableRowSelection } from '@/object-record/record-table/hooks/internal/useResetTableRowSelection';
import { useCloseCurrentTableCellInEditMode } from './useCloseCurrentTableCellInEditMode';
import { useDisableSoftFocus } from './useDisableSoftFocus'; import { useDisableSoftFocus } from './useDisableSoftFocus';
export const useLeaveTableFocus = (recordTableId?: string) => { export const useLeaveTableFocus = (recordTableId?: string) => {
const disableSoftFocus = useDisableSoftFocus(recordTableId); const disableSoftFocus = useDisableSoftFocus(recordTableId);
const closeCurrentCellInEditMode =
useCloseCurrentTableCellInEditMode(recordTableId);
const { isSoftFocusActiveState } = useRecordTableStates(recordTableId); const { isSoftFocusActiveState } = useRecordTableStates(recordTableId);
@ -27,28 +21,14 @@ export const useLeaveTableFocus = (recordTableId?: string) => {
isSoftFocusActiveState, isSoftFocusActiveState,
); );
const currentHotkeyScope = snapshot
.getLoadable(currentHotkeyScopeState)
.getValue();
resetTableRowSelection(); resetTableRowSelection();
if (!isSoftFocusActive) { if (!isSoftFocusActive) {
return; return;
} }
if (currentHotkeyScope?.scope === TableHotkeyScope.Table) {
return;
}
closeCurrentCellInEditMode();
disableSoftFocus(); disableSoftFocus();
}, },
[ [disableSoftFocus, isSoftFocusActiveState, resetTableRowSelection],
closeCurrentCellInEditMode,
disableSoftFocus,
isSoftFocusActiveState,
resetTableRowSelection,
],
); );
}; };

View File

@ -4,10 +4,19 @@ import { FieldInput } from '@/object-record/record-field/components/FieldInput';
import { FieldContext } from '@/object-record/record-field/contexts/FieldContext'; import { FieldContext } from '@/object-record/record-field/contexts/FieldContext';
import { useIsFieldReadOnly } from '@/object-record/record-field/hooks/useIsFieldReadOnly'; import { useIsFieldReadOnly } from '@/object-record/record-field/hooks/useIsFieldReadOnly';
import { FieldInputEvent } from '@/object-record/record-field/types/FieldInputEvent'; import { FieldInputEvent } from '@/object-record/record-field/types/FieldInputEvent';
import { RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID } from '@/object-record/record-table/constants/RecordTableClickOutsideListenerId';
import { RecordTableContext } from '@/object-record/record-table/contexts/RecordTableContext'; import { RecordTableContext } from '@/object-record/record-table/contexts/RecordTableContext';
import { getRecordFieldInputId } from '@/object-record/utils/getRecordFieldInputId'; import { getRecordFieldInputId } from '@/object-record/utils/getRecordFieldInputId';
import { useClickOustideListenerStates } from '@/ui/utilities/pointer-event/hooks/useClickOustideListenerStates';
import { useSetRecoilState } from 'recoil';
export const RecordTableCellFieldInput = () => { export const RecordTableCellFieldInput = () => {
const { getClickOutsideListenerIsActivatedState } =
useClickOustideListenerStates(RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID);
const setClickOutsideListenerIsActivated = useSetRecoilState(
getClickOutsideListenerIsActivatedState,
);
const { onUpsertRecord, onMoveFocus, onCloseTableCell } = const { onUpsertRecord, onMoveFocus, onCloseTableCell } =
useContext(RecordTableContext); useContext(RecordTableContext);
@ -40,6 +49,8 @@ export const RecordTableCellFieldInput = () => {
}; };
const handleClickOutside: FieldInputEvent = (persistField) => { const handleClickOutside: FieldInputEvent = (persistField) => {
setClickOutsideListenerIsActivated(false);
onUpsertRecord({ onUpsertRecord({
persistField, persistField,
recordId, recordId,

View File

@ -21,6 +21,8 @@ import { getSnapshotValue } from '@/ui/utilities/state/utils/getSnapshotValue';
import { isDefined } from '~/utils/isDefined'; import { isDefined } from '~/utils/isDefined';
import { RecordIndexRootPropsContext } from '@/object-record/record-index/contexts/RecordIndexRootPropsContext'; import { RecordIndexRootPropsContext } from '@/object-record/record-index/contexts/RecordIndexRootPropsContext';
import { RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID } from '@/object-record/record-table/constants/RecordTableClickOutsideListenerId';
import { useClickOustideListenerStates } from '@/ui/utilities/pointer-event/hooks/useClickOustideListenerStates';
import { useContext } from 'react'; import { useContext } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { TableHotkeyScope } from '../../types/TableHotkeyScope'; import { TableHotkeyScope } from '../../types/TableHotkeyScope';
@ -42,6 +44,9 @@ export type OpenTableCellArgs = {
}; };
export const useOpenRecordTableCellV2 = (tableScopeId: string) => { export const useOpenRecordTableCellV2 = (tableScopeId: string) => {
const { getClickOutsideListenerIsActivatedState } =
useClickOustideListenerStates(RECORD_TABLE_CLICK_OUTSIDE_LISTENER_ID);
const { indexIdentifierUrl } = useContext(RecordIndexRootPropsContext); const { indexIdentifierUrl } = useContext(RecordIndexRootPropsContext);
const moveEditModeToTableCellPosition = const moveEditModeToTableCellPosition =
useMoveEditModeToTableCellPosition(tableScopeId); useMoveEditModeToTableCellPosition(tableScopeId);
@ -65,7 +70,7 @@ export const useOpenRecordTableCellV2 = (tableScopeId: string) => {
const navigate = useNavigate(); const navigate = useNavigate();
const openTableCell = useRecoilCallback( const openTableCell = useRecoilCallback(
({ snapshot }) => ({ snapshot, set }) =>
({ ({
initialValue, initialValue,
cellPosition, cellPosition,
@ -80,6 +85,8 @@ export const useOpenRecordTableCellV2 = (tableScopeId: string) => {
return; return;
} }
set(getClickOutsideListenerIsActivatedState, false);
const isFirstColumnCell = cellPosition.column === 0; const isFirstColumnCell = cellPosition.column === 0;
const fieldValue = getSnapshotValue( const fieldValue = getSnapshotValue(
@ -137,17 +144,18 @@ export const useOpenRecordTableCellV2 = (tableScopeId: string) => {
} }
}, },
[ [
getClickOutsideListenerIsActivatedState,
setDragSelectionStartEnabled, setDragSelectionStartEnabled,
moveEditModeToTableCellPosition,
initDraftValue,
toggleClickOutsideListener, toggleClickOutsideListener,
leaveTableFocus, leaveTableFocus,
setHotkeyScope, navigate,
initDraftValue, indexIdentifierUrl,
moveEditModeToTableCellPosition,
openRightDrawer,
setViewableRecordId, setViewableRecordId,
setViewableRecordNameSingular, setViewableRecordNameSingular,
indexIdentifierUrl, openRightDrawer,
navigate, setHotkeyScope,
], ],
); );