77a1840611
Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: bosiraphael <raphael.bosi@gmail.com>
77 lines
1.7 KiB
TypeScript
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>
|
|
);
|
|
};
|