Skip to content

Commit

Permalink
fix: prevent datetime from validating early
Browse files Browse the repository at this point in the history
Closes #1099
  • Loading branch information
Skaiir committed Apr 5, 2024
1 parent 032d9a8 commit 7c939d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
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

0 comments on commit 7c939d3

Please sign in to comment.