Skip to content
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

fix: prevent datetime from validating early #1121

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
Expand Up @@ -15,7 +15,7 @@ import { Timepicker } from './parts/Timepicker';

import { formFieldClasses, prefixId } from '../Util';
import { sanitizeDateTimePickerValue } from '../util/sanitizerUtil';
import { parseIsoTime, serializeDate, serializeDateTime, serializeTime } from '../util/dateTimeUtil';
import { parseIsoTime, serializeDate, serializeDateTime, serializeTime, getNullDateTime, isValidDate, isValidTime } from '../util/dateTimeUtil';

const type = 'datetime';

Expand Down Expand Up @@ -49,14 +49,9 @@ export function Datetime(props) {
const { formId } = useContext(FormContext);
const dateTimeGroupRef = useRef();

const getNullDateTime = () => ({ date: new Date(Date.parse(null)), time: null });

const [ dateTime, setDateTime ] = useState(getNullDateTime());
const [ dateTimeUpdateRequest, setDateTimeUpdateRequest ] = useState(null);

const isValidDate = (date) => date && !isNaN(date.getTime());
const isValidTime = (time) => !isNaN(parseInt(time));

const useDatePicker = useMemo(() => subtype === DATETIME_SUBTYPES.DATE || subtype === DATETIME_SUBTYPES.DATETIME, [ subtype ]);
const useTimePicker = useMemo(() => subtype === DATETIME_SUBTYPES.TIME || subtype === DATETIME_SUBTYPES.DATETIME, [ subtype ]);

Expand Down Expand Up @@ -113,9 +108,13 @@ export function Datetime(props) {
newDateTimeValue = serializeDateTime(date, time, timeSerializingFormat);
}

if (value === newDateTimeValue) {
return;
}

onChange({ value: newDateTimeValue, field });

}, [ field, onChange, subtype, timeSerializingFormat ]);
}, [ value, field, onChange, subtype, timeSerializingFormat ]);

useEffect(() => {
if (dateTimeUpdateRequest) {
Expand Down
15 changes: 15 additions & 0 deletions packages/form-js-viewer/src/render/components/util/dateTimeUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ export function isInvalidDateString(value) {
return isNaN(new Date(Date.parse(value)).getTime());
}

export function getNullDateTime() {
return {
date: new Date(Date.parse(null)),
time: null
};
}

export function isValidDate(date) {
return date && !isNaN(date.getTime());
}

export function isValidTime(time) {
return !isNaN(parseInt(time));
}

function _getSignedPaddedHours(minutes) {
if (minutes > 0) {
return '-' + _getZeroPaddedString(Math.floor(minutes / 60));
Expand Down
Loading