Skip to content

Derik/b/2321 #2984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DatePicker } from '@/components/antd';
import moment, { isMoment } from 'moment';
import React, { FC, useMemo } from 'react';
import React, { FC, useEffect, useMemo, useState } from 'react';
import ReadOnlyDisplayFormItem from '@/components/readOnlyDisplayFormItem';
import { useForm, useGlobalState, useMetadata } from '@/providers';
import { getStyle } from '@/providers/form/utils';
Expand All @@ -10,13 +10,14 @@ import { IDateFieldProps, RangePickerChangeEvent, TimePickerChangeEvent } from '
import { DATE_TIME_FORMATS, disabledDate, disabledTime, getFormat } from './utils';
import { asPropertiesArray } from '@/interfaces/metadata';

const MIDNIGHT_MOMENT = moment('00:00:00', 'HH:mm:ss');

const { RangePicker } = DatePicker;

export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {
const { properties: metaProperties } = useMetadata(false)?.metadata ?? {};
const properties = asPropertiesArray(metaProperties, []);
const [MIDNIGHT_MOMENT, SET_MIDNIGHT_MOMENT] = useState(moment('00:00:00', 'HH:mm:ss'));
const [momentValue, setMomentValue] = useState<moment.Moment | null>(null);
const [rangeMomentValue, setRangeMomentValue] = useState<any>(null);

const { globalState } = useGlobalState();

Expand Down Expand Up @@ -73,7 +74,6 @@ export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {
return;
}
const newValue = convertValue(localValue);

(onChange as TimePickerChangeEvent)(newValue, dateString);
};

Expand All @@ -89,12 +89,38 @@ export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {

const handleOnOk = (value: moment.Moment | null) => handleDatePickerChange(value, value?.format(pickerFormat));

const evaluatedStyle = { width: '100%', ...getStyle(style, formData, globalState) };
const getCurrentTime = () => {
// Reset the state to null
setMomentValue(null);
setRangeMomentValue(null);

if (defaultToMidnight) {
SET_MIDNIGHT_MOMENT(moment('00:00:00', 'HH:mm:ss'));
} else {
// Get the current system time as a Moment object and format it to 'HH:mm:ss'
const MIDNIGHT = moment().set({
hour: moment().hour(),
minute: moment().minute(),
second: moment().second(),
millisecond: 0,
});
SET_MIDNIGHT_MOMENT(moment(MIDNIGHT.format('HH:mm:ss')));
}
};

const momentValue = useMemo(() => getMoment(value, pickerFormat), [value, pickerFormat]);
const rangeMomentValue = useMemo(() => getRangeMoment(value, pickerFormat), [value, pickerFormat]);
const evaluatedStyle = { width: '100%', ...getStyle(style, formData, globalState) };
const defaultMomentValue = useMemo(() => getRangeMoment(defaultValue, pickerFormat), [defaultValue, pickerFormat]);

useEffect(() => {
if (range) {
const newRangeMomentValue = getRangeMoment(value, pickerFormat);
setRangeMomentValue(newRangeMomentValue);
} else {
const newMomentValue = getMoment(value, pickerFormat);
setMomentValue(newMomentValue);
}
}, [value, pickerFormat]);

if (range) {
return (
<RangePicker
Expand All @@ -107,11 +133,12 @@ export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {
defaultValue={defaultMomentValue}
{...rest}
picker={picker}
showTime={showTime ? (defaultToMidnight ? { defaultValue: [MIDNIGHT_MOMENT, MIDNIGHT_MOMENT] } : true) : false}
showTime={showTime ? { defaultOpenValue: [MIDNIGHT_MOMENT, MIDNIGHT_MOMENT] } : false}
disabled={readOnly}
style={evaluatedStyle}
allowClear
variant={hideBorder ? 'borderless' : undefined}
onClick={getCurrentTime}
/>
);
}
Expand All @@ -129,14 +156,15 @@ export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {
onChange={handleDatePickerChange}
onOk={handleOnOk}
variant={hideBorder ? 'borderless' : undefined}
showTime={showTime ? (defaultToMidnight ? { defaultValue: MIDNIGHT_MOMENT } : true) : false}
showTime={showTime ? { defaultOpenValue: MIDNIGHT_MOMENT } : false}
showNow={showNow}
picker={picker}
format={pickerFormat}
style={evaluatedStyle}
{...rest}
value={momentValue}
allowClear
onClick={getCurrentTime}
/>
);
};