Files
twenty/packages/twenty-front/src/modules/ui/field/input/components/DateInput.tsx
Charles Bochet 17474a0e1e Remove cell hotkey scope (#11435)
Remove FieldContext hotkey scope as:
- each Field should set its own hotkey scope (ex:
emails-field-input-{recordId} or emails-field-input for now)
- while opening a fieldInput, we should synchronously set the
corresponding hotkey scope

To cut this refactoring in half, I'm allowing all input to use
TableHotkeyScope.CellEditMode
2025-04-08 11:18:23 +02:00

117 lines
2.9 KiB
TypeScript

import { useRef, useState } from 'react';
import { useRegisterInputEvents } from '@/object-record/record-field/meta-types/input/hooks/useRegisterInputEvents';
import {
DateTimePicker,
MONTH_AND_YEAR_DROPDOWN_MONTH_SELECT_ID,
MONTH_AND_YEAR_DROPDOWN_YEAR_SELECT_ID,
} from '@/ui/input/components/internal/date/components/InternalDatePicker';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { Nullable } from 'twenty-ui/utilities';
export type DateInputProps = {
value: Nullable<Date>;
onEnter: (newDate: Nullable<Date>) => void;
onEscape: (newDate: Nullable<Date>) => void;
onClickOutside: (
event: MouseEvent | TouchEvent,
newDate: Nullable<Date>,
) => void;
clearable?: boolean;
onChange?: (newDate: Nullable<Date>) => void;
isDateTimeInput?: boolean;
onClear?: () => void;
onSubmit?: (newDate: Nullable<Date>) => void;
hideHeaderInput?: boolean;
hotkeyScope: string;
};
export const DateInput = ({
value,
onEnter,
onEscape,
onClickOutside,
clearable,
onChange,
isDateTimeInput,
onClear,
onSubmit,
hideHeaderInput,
hotkeyScope,
}: DateInputProps) => {
const [internalValue, setInternalValue] = useState(value);
const wrapperRef = useRef<HTMLDivElement>(null);
const handleChange = (newDate: Date | null) => {
setInternalValue(newDate);
onChange?.(newDate);
};
const handleClear = () => {
setInternalValue(null);
onClear?.();
};
const handleClose = (newDate: Date | null) => {
setInternalValue(newDate);
onSubmit?.(newDate);
};
const { closeDropdown: closeDropdownMonthSelect } = useDropdown(
MONTH_AND_YEAR_DROPDOWN_MONTH_SELECT_ID,
);
const { closeDropdown: closeDropdownYearSelect } = useDropdown(
MONTH_AND_YEAR_DROPDOWN_YEAR_SELECT_ID,
);
const handleEnter = () => {
closeDropdownYearSelect();
closeDropdownMonthSelect();
onEnter(internalValue);
};
const handleEscape = () => {
closeDropdownYearSelect();
closeDropdownMonthSelect();
onEscape(internalValue);
};
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
event.stopImmediatePropagation();
closeDropdownYearSelect();
closeDropdownMonthSelect();
onEscape(internalValue);
onClickOutside(event, internalValue);
};
useRegisterInputEvents({
inputRef: wrapperRef,
inputValue: internalValue,
onEnter: handleEnter,
onEscape: handleEscape,
onClickOutside: handleClickOutside,
hotkeyScope: hotkeyScope,
});
return (
<div ref={wrapperRef}>
<DateTimePicker
date={internalValue ?? new Date()}
onChange={handleChange}
onClose={handleClose}
clearable={clearable ? clearable : false}
onEnter={onEnter}
onEscape={onEscape}
onClear={handleClear}
hideHeaderInput={hideHeaderInput}
isDateTimeInput={isDateTimeInput}
/>
</div>
);
};