Skip to content

Commit

Permalink
Fixed date input fields (#9442)
Browse files Browse the repository at this point in the history
- Fixed date input fields : proper hotkey management, like other fields
- Removed DropdownUnmountEffect which was causing many bugs.
  • Loading branch information
lucasbordeau authored Jan 8, 2025
1 parent 3198748 commit 00a9646
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 64 deletions.
4 changes: 4 additions & 0 deletions packages/twenty-front/src/hooks/useHotkeyScopeOnMount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { useEffect } from 'react';

/**
* @deprecated This hook uses useEffect
* Use event handlers and imperative code to manage hotkey scope changes.
*/
export const useHotkeyScopeOnMount = (hotkeyScope: string) => {
const {
goBackToPreviousHotkeyScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const DateFieldInput = ({
onClear,
onSubmit,
}: DateFieldInputProps) => {
const { fieldValue, setDraftValue } = useDateField();
const { fieldValue, setDraftValue, hotkeyScope } = useDateField();

const persistField = usePersistField();

Expand Down Expand Up @@ -77,6 +77,7 @@ export const DateFieldInput = ({
onChange={handleChange}
onClear={handleClear}
onSubmit={handleSubmit}
hotkeyScope={hotkeyScope}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const DateTimeFieldInput = ({
onClear,
onSubmit,
}: DateTimeFieldInputProps) => {
const { fieldValue, setDraftValue } = useDateTimeField();
const { fieldValue, setDraftValue, hotkeyScope } = useDateTimeField();

const persistField = usePersistField();

Expand Down Expand Up @@ -80,6 +80,7 @@ export const DateTimeFieldInput = ({
isDateTimeInput
onClear={handleClear}
onSubmit={handleSubmit}
hotkeyScope={hotkeyScope}
/>
);
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useRef, useState } from 'react';
import { Nullable } from 'twenty-ui';

import { useRegisterInputEvents } from '@/object-record/record-field/meta-types/input/hooks/useRegisterInputEvents';
import {
InternalDatePicker,
MONTH_AND_YEAR_DROPDOWN_ID,
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 { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';

export type DateInputProps = {
value: Nullable<Date>;
Expand All @@ -24,6 +24,7 @@ export type DateInputProps = {
onClear?: () => void;
onSubmit?: (newDate: Nullable<Date>) => void;
hideHeaderInput?: boolean;
hotkeyScope: string;
};

export const DateInput = ({
Expand All @@ -37,6 +38,7 @@ export const DateInput = ({
onClear,
onSubmit,
hideHeaderInput,
hotkeyScope,
}: DateInputProps) => {
const [internalValue, setInternalValue] = useState(value);

Expand Down Expand Up @@ -65,17 +67,39 @@ export const DateInput = ({
MONTH_AND_YEAR_DROPDOWN_YEAR_SELECT_ID,
);

useListenClickOutside({
refs: [wrapperRef],
listenerId: 'DateInput',
callback: (event) => {
event.stopImmediatePropagation();

closeDropdownYearSelect();
closeDropdownMonthSelect();
closeDropdown();
onClickOutside(event, internalValue);
},
const handleEnter = () => {
closeDropdownYearSelect();
closeDropdownMonthSelect();
closeDropdown();

onEnter?.(internalValue);
};

const handleEscape = () => {
closeDropdownYearSelect();
closeDropdownMonthSelect();
closeDropdown();

onEscape?.(internalValue);
};

const handleClickOutside = (event: MouseEvent | TouchEvent) => {
event.stopImmediatePropagation();

closeDropdownYearSelect();
closeDropdownMonthSelect();
closeDropdown();

onClickOutside(event, internalValue);
};

useRegisterInputEvents({
inputRef: wrapperRef,
inputValue: internalValue,
onEnter: handleEnter,
onEscape: handleEscape,
onClickOutside: handleClickOutside,
hotkeyScope: hotkeyScope,
});

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styled from '@emotion/styled';
import { DateTime } from 'luxon';
import ReactDatePicker from 'react-datepicker';
import { Key } from 'ts-key-enum';
import {
IconCalendarX,
MenuItemLeftContent,
Expand Down Expand Up @@ -305,11 +304,8 @@ export const InternalDatePicker = ({
date,
onChange,
onMouseSelect,
onEnter,
onEscape,
clearable = true,
isDateTimeInput,
keyboardEventsDisabled,
onClear,
isRelative,
relativeDate,
Expand Down Expand Up @@ -345,31 +341,6 @@ export const InternalDatePicker = ({
onMouseSelect?.(newDate);
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (isDefined(keyboardEventsDisabled) && keyboardEventsDisabled) {
return;
}

switch (event.key) {
case Key.Enter: {
event.stopPropagation();
event.preventDefault();

closeDropdowns();
onEnter?.(internalDate);
break;
}
case Key.Escape: {
event.stopPropagation();
event.preventDefault();

closeDropdowns();
onEscape?.(internalDate);
break;
}
}
};

const handleChangeMonth = (month: number) => {
const newDate = new Date(internalDate);
newDate.setMonth(month);
Expand Down Expand Up @@ -469,7 +440,7 @@ export const InternalDatePicker = ({
const selectedDates = isRelative ? highlightedDates : [dateToUse];

return (
<StyledContainer onKeyDown={handleKeyDown} calendarDisabled={isRelative}>
<StyledContainer calendarDisabled={isRelative}>
<div className={clearable ? 'clearable ' : ''}>
<ReactDatePicker
open={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Keys } from 'react-hotkeys-hook';
import { useDropdown } from '../hooks/useDropdown';

import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { DropdownUnmountEffect } from '@/ui/layout/dropdown/components/DropdownUnmountEffect';
import { DropdownComponentInstanceContext } from '@/ui/layout/dropdown/contexts/DropdownComponeInstanceContext';
import { dropdownHotkeyComponentState } from '@/ui/layout/dropdown/states/dropdownHotkeyComponentState';
import { dropdownMaxHeightComponentStateV2 } from '@/ui/layout/dropdown/states/dropdownMaxHeightComponentStateV2';
Expand Down Expand Up @@ -165,7 +164,6 @@ export const Dropdown = ({
/>
</>
</DropdownScope>
<DropdownUnmountEffect dropdownId={dropdownId} />
</DropdownComponentInstanceContext.Provider>
);
};

This file was deleted.

0 comments on commit 00a9646

Please sign in to comment.