mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 11:23:50 +00:00
9e04a1dcfa
--------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me>
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { ChangeEvent, memo, useCallback, useEffect, useRef, useState } from 'react';
|
|
import { getHotkeyHandler } from '@mantine/hooks';
|
|
|
|
import { AutoTextArea } from '../../../common/components/input/auto-text-area/AutoTextArea';
|
|
|
|
interface EditableCellProps {
|
|
value: string;
|
|
handleUpdate: (newValue: string) => void;
|
|
}
|
|
|
|
const EditableCell = (props: EditableCellProps) => {
|
|
const { value: initialValue, handleUpdate } = props;
|
|
|
|
// We need to keep and update the state of the cell normally
|
|
const [value, setValue] = useState(initialValue);
|
|
const ref = useRef<HTMLAreaElement>();
|
|
const onChange = useCallback((event: ChangeEvent<HTMLTextAreaElement>) => setValue(event.target.value), []);
|
|
|
|
// We'll only update the external data when the input is blurred
|
|
const onBlur = useCallback(() => handleUpdate(value), [handleUpdate, value]);
|
|
|
|
//TODO: maybe we can unify this with `useReactiveTextInput`
|
|
const onKeyDown = getHotkeyHandler([
|
|
['mod + Enter', () => ref.current?.blur()],
|
|
[
|
|
'Escape',
|
|
() => {
|
|
setValue(initialValue);
|
|
setTimeout(() => ref.current?.blur());
|
|
},
|
|
],
|
|
]);
|
|
|
|
// If the initialValue is changed external, sync it up with our state
|
|
useEffect(() => {
|
|
setValue(initialValue);
|
|
}, [initialValue]);
|
|
|
|
return (
|
|
<AutoTextArea
|
|
size='sm'
|
|
value={value}
|
|
inputref={ref}
|
|
onChange={onChange}
|
|
onBlur={onBlur}
|
|
rows={1}
|
|
onKeyDown={onKeyDown}
|
|
transition='none'
|
|
spellCheck={false}
|
|
style={{ padding: 0 }}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default memo(EditableCell);
|