Skip to content

The open alternative to Salesforce, designed for AI.

Fix date picker wrong on certain timezones (#5972)

Lucas Bordeau9e08445bff7a
diff --git a/packages/twenty-front/src/modules/object-record/record-field/components/FieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/components/FieldInput.tsx
index ce1240e6449..f13e59e3abe 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/components/FieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/components/FieldInput.tsx
@@ -121,6 +121,7 @@ export const FieldInput = ({
onEscape={onEscape}
onClickOutside={onClickOutside}
onClear={onSubmit}
+ onSubmit={onSubmit}
/>
) : isFieldDate(fieldDefinition) ? (
<DateFieldInput
@@ -128,6 +129,7 @@ export const FieldInput = ({
onEscape={onEscape}
onClickOutside={onClickOutside}
onClear={onSubmit}
+ onSubmit={onSubmit}
/>
) : isFieldNumber(fieldDefinition) ? (
<NumberFieldInput
diff --git a/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateFieldInput.tsx
index acc4dfff175..4776642e91c 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateFieldInput.tsx
@@ -13,6 +13,7 @@ export type DateFieldInputProps = {
onEnter?: FieldInputEvent;
onEscape?: FieldInputEvent;
onClear?: FieldInputEvent;
+ onSubmit?: FieldInputEvent;
};
export const DateFieldInput = ({
@@ -20,6 +21,7 @@ export const DateFieldInput = ({
onEscape,
onClickOutside,
onClear,
+ onSubmit,
}: DateFieldInputProps) => {
const { fieldValue, setDraftValue } = useDateField();
@@ -39,6 +41,10 @@ export const DateFieldInput = ({
onEnter?.(() => persistDate(newDate));
};
+ const handleSubmit = (newDate: Nullable<Date>) => {
+ onSubmit?.(() => persistDate(newDate));
+ };
+
const handleEscape = (newDate: Nullable<Date>) => {
onEscape?.(() => persistDate(newDate));
};
@@ -69,6 +75,7 @@ export const DateFieldInput = ({
clearable
onChange={handleChange}
onClear={handleClear}
+ onSubmit={handleSubmit}
/>
);
};
diff --git a/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateTimeFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateTimeFieldInput.tsx
index 15c6280ee9a..6a57490084a 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateTimeFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/DateTimeFieldInput.tsx
@@ -12,6 +12,7 @@ export type DateTimeFieldInputProps = {
onEnter?: FieldInputEvent;
onEscape?: FieldInputEvent;
onClear?: FieldInputEvent;
+ onSubmit?: FieldInputEvent;
};
export const DateTimeFieldInput = ({
@@ -19,6 +20,7 @@ export const DateTimeFieldInput = ({
onEscape,
onClickOutside,
onClear,
+ onSubmit,
}: DateTimeFieldInputProps) => {
const { fieldValue, setDraftValue } = useDateTimeField();
@@ -57,6 +59,10 @@ export const DateTimeFieldInput = ({
onClear?.(() => persistDate(null));
};
+ const handleSubmit = (newDate: Nullable<Date>) => {
+ onSubmit?.(() => persistDate(newDate));
+ };
+
const dateValue = fieldValue ? new Date(fieldValue) : null;
return (
@@ -69,6 +75,7 @@ export const DateTimeFieldInput = ({
onChange={handleChange}
isDateTimeInput
onClear={handleClear}
+ onSubmit={handleSubmit}
/>
);
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellContainer.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellContainer.tsx
index 48b3f5ce25f..3ef55faf103 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellContainer.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellContainer.tsx
@@ -142,10 +142,9 @@ export const RecordTableCellContainer = ({
[styles.cellBaseContainerSoftFocus]: hasSoftFocus,
})}
>
- {isInEditMode && (
+ {isInEditMode ? (
<RecordTableCellEditMode>{editModeContent}</RecordTableCellEditMode>
- )}
- {hasSoftFocus ? (
+ ) : hasSoftFocus ? (
<RecordTableCellSoftFocusMode
editModeContent={editModeContent}
nonEditModeContent={nonEditModeContent}
diff --git a/packages/twenty-front/src/modules/ui/field/input/components/DateInput.tsx b/packages/twenty-front/src/modules/ui/field/input/components/DateInput.tsx
index 4b2131ad5ae..1e74e447b5a 100644
--- a/packages/twenty-front/src/modules/ui/field/input/components/DateInput.tsx
+++ b/packages/twenty-front/src/modules/ui/field/input/components/DateInput.tsx
@@ -33,6 +33,7 @@ export type DateInputProps = {
onChange?: (newDate: Nullable<Date>) => void;
isDateTimeInput?: boolean;
onClear?: () => void;
+ onSubmit?: (newDate: Nullable<Date>) => void;
};
export const DateInput = ({
@@ -44,6 +45,7 @@ export const DateInput = ({
onChange,
isDateTimeInput,
onClear,
+ onSubmit,
}: DateInputProps) => {
const [internalValue, setInternalValue] = useState(value);
@@ -59,6 +61,11 @@ export const DateInput = ({
onClear?.();
};
+ const handleMouseSelect = (newDate: Date | null) => {
+ setInternalValue(newDate);
+ onSubmit?.(newDate);
+ };
+
const { closeDropdown } = useDropdown(MONTH_AND_YEAR_DROPDOWN_ID);
const { closeDropdown: closeDropdownMonthSelect } = useDropdown(
MONTH_AND_YEAR_DROPDOWN_MONTH_SELECT_ID,
@@ -86,9 +93,7 @@ export const DateInput = ({
<InternalDatePicker
date={internalValue ?? new Date()}
onChange={handleChange}
- onMouseSelect={(newDate: Date | null) => {
- onEnter(newDate);
- }}
+ onMouseSelect={handleMouseSelect}
clearable={clearable ? clearable : false}
isDateTimeInput={isDateTimeInput}
onEnter={onEnter}
diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimeInput.tsx b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimeInput.tsx
index fb17e19b9fa..239f8050922 100644
--- a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimeInput.tsx
+++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimeInput.tsx
@@ -57,11 +57,25 @@ export const DateTimeInput = ({
(date: any) => {
const dateParsed = DateTime.fromJSDate(date);
- const formattedDate = dateParsed.toFormat(parsingFormat);
+ const dateWithoutTime = DateTime.fromJSDate(date)
+ .toLocal()
+ .set({
+ day: date.getUTCDate(),
+ month: date.getUTCMonth() + 1,
+ year: date.getUTCFullYear(),
+ hour: 0,
+ minute: 0,
+ second: 0,
+ millisecond: 0,
+ });
+
+ const formattedDate = isDateTimeInput
+ ? dateParsed.toFormat(parsingFormat)
+ : dateWithoutTime.toFormat(parsingFormat);
return formattedDate;
},
- [parsingFormat],
+ [parsingFormat, isDateTimeInput],
);
const parseStringToDate = (str: string) => {
diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/InternalDatePicker.tsx b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/InternalDatePicker.tsx
index a45855074b3..89f13fef9a5 100644
--- a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/InternalDatePicker.tsx
+++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/InternalDatePicker.tsx
@@ -383,19 +383,35 @@ export const InternalDatePicker = ({
onChange?.(newDate);
};
+ const dateWithoutTime = DateTime.fromJSDate(date)
+ .toLocal()
+ .set({
+ day: date.getUTCDate(),
+ month: date.getUTCMonth() + 1,
+ year: date.getUTCFullYear(),
+ hour: 0,
+ minute: 0,
+ second: 0,
+ millisecond: 0,
+ })
+ .toJSDate();
+
+ const dateToUse = isDateTimeInput ? date : dateWithoutTime;
+
return (
<StyledContainer onKeyDown={handleKeyDown}>
<div className={clearable ? 'clearable ' : ''}>
<ReactDatePicker
open={true}
- selected={internalDate}
- openToDate={internalDate}
+ selected={dateToUse}
+ openToDate={dateToUse}
+ disabledKeyboardNavigation
onChange={(newDate) => {
onChange?.(newDate);
}}
customInput={
<DateTimeInput
- date={internalDate}
+ date={dateToUse}
isDateTimeInput={isDateTimeInput}
onChange={onChange}
/>
@@ -424,13 +440,13 @@ export const InternalDatePicker = ({
options={months}
disableBlur
onChange={handleChangeMonth}
- value={date.getMonth()}
+ value={date.getUTCMonth()}
fullWidth
/>
<Select
dropdownId={MONTH_AND_YEAR_DROPDOWN_YEAR_SELECT_ID}
onChange={handleChangeYear}
- value={date.getFullYear()}
+ value={date.getUTCFullYear()}
options={years}
disableBlur
fullWidth
@@ -450,16 +466,24 @@ export const InternalDatePicker = ({
</StyledCustomDatePickerHeader>
</>
)}
- onSelect={(date: Date, event) => {
- const dateUTC = DateTime.fromJSDate(date, {
- zone: 'utc',
- }).toJSDate();
-
- if (event?.type === 'click') {
- handleMouseSelect?.(dateUTC);
- } else {
- onChange?.(dateUTC);
- }
+ onSelect={(date: Date) => {
+ const dateParsedWithoutTime = DateTime.fromObject(
+ {
+ day: date.getDate(),
+ month: date.getMonth() + 1,
+ year: date.getFullYear(),
+ hour: 0,
+ minute: 0,
+ second: 0,
+ },
+ { zone: 'utc' },
+ ).toJSDate();
+
+ const dateForUpdate = isDateTimeInput
+ ? date
+ : dateParsedWithoutTime;
+
+ handleMouseSelect?.(dateForUpdate);
}}
/>
</div>