import { useCallback, useEffect, useState } from 'react'; import { ariaDescribedByIds, dateRangeOptions, parseDateString, toDateString, getDateElementProps, titleId, DateObject, type DateElementFormat, FormContextType, RJSFSchema, StrictRJSFSchema, TranslatableString, WidgetProps, } from '@rjsf/utils'; import { Flex, Box, Group, Button, Select, Input } from '@mantine/core'; function readyForChange(state: DateObject) { return Object.values(state).every((value) => value !== -1); } /** The `AltDateWidget` is an alternative widget for rendering date properties. * @param props - The `WidgetProps` for this component */ function AltDateWidget( props: WidgetProps ) { const { id, value, required, disabled, readonly, label, hideLabel, rawErrors, options, onChange, showTime = false, registry, } = props; const { translateString } = registry; const [lastValue, setLastValue] = useState(value); const [state, setState] = useState(parseDateString(value, showTime)); useEffect(() => { const stateValue = toDateString(state, showTime); if (lastValue !== value) { // We got a new value in the props setLastValue(value); setState(parseDateString(value, showTime)); } else if (readyForChange(state) && stateValue !== value) { // Selected date is ready to be submitted onChange(stateValue); setLastValue(stateValue); } }, [showTime, value, onChange, state, lastValue]); const handleChange = useCallback((property: keyof DateObject, nextValue: any) => { setState((prev) => ({ ...prev, [property]: typeof nextValue === 'undefined' ? -1 : nextValue })); }, []); const handleSetNow = useCallback(() => { if (!disabled && !readonly) { const nextState = parseDateString(new Date().toJSON(), showTime); onChange(toDateString(nextState, showTime)); } }, [disabled, readonly, showTime]); const handleClear = useCallback(() => { if (!disabled && !readonly) { onChange(''); } }, [disabled, readonly, onChange]); return ( <> {!hideLabel && !!label && ( (id)} required={required}> {label} )} {getDateElementProps( state, showTime, options.yearsRange as [number, number] | undefined, options.format as DateElementFormat | undefined ).map((elemProps, i) => { const elemId = id + '_' + elemProps.type; return (