Files
twenty/front/src/modules/ui/data-table/table-cell/components/TableCellSoftFocusMode.tsx
T
gitstart-twenty 77a1840611 Chore(front): Create a custom eslint rule for Props naming (#1904)
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>
Co-authored-by: bosiraphael <raphael.bosi@gmail.com>
2023-10-09 16:31:13 +02:00

77 lines
1.7 KiB
TypeScript

import { PropsWithChildren, useEffect, useRef } from 'react';
import { useIsFieldInputOnly } from '@/ui/field/hooks/useIsFieldInputOnly';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { isNonTextWritingKey } from '@/ui/utilities/hotkey/utils/isNonTextWritingKey';
import { TableHotkeyScope } from '../../types/TableHotkeyScope';
import { useTableCell } from '../hooks/useTableCell';
import { TableCellDisplayContainer } from './TableCellDisplayContainer';
type TableCellSoftFocusModeProps = PropsWithChildren<unknown>;
export const TableCellSoftFocusMode = ({
children,
}: TableCellSoftFocusModeProps) => {
const { openTableCell } = useTableCell();
const isFieldInputOnly = useIsFieldInputOnly();
const scrollRef = useRef<HTMLDivElement>(null);
useEffect(() => {
scrollRef.current?.scrollIntoView({ block: 'nearest' });
}, []);
useScopedHotkeys(
'enter',
() => {
openTableCell();
},
TableHotkeyScope.TableSoftFocus,
[openTableCell],
{
enabled: !isFieldInputOnly,
},
);
useScopedHotkeys(
'*',
(keyboardEvent) => {
const isWritingText =
!isNonTextWritingKey(keyboardEvent.key) &&
!keyboardEvent.ctrlKey &&
!keyboardEvent.metaKey;
if (!isWritingText) {
return;
}
openTableCell();
},
TableHotkeyScope.TableSoftFocus,
[openTableCell],
{
preventDefault: false,
enabled: !isFieldInputOnly,
},
);
const handleClick = () => {
if (!isFieldInputOnly) {
openTableCell();
}
};
return (
<TableCellDisplayContainer
onClick={handleClick}
softFocus
scrollRef={scrollRef}
>
{children}
</TableCellDisplayContainer>
);
};