<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateField label="Basic date field" />
</LocalizationProvider>
<LocalizationProvider dateAdapter={AdapterDateFns}>
<Stack spacing={2} direction="row">
<DateField label="Uncontrolled date field" defaultValue={new Date()} />
<DateField
label="Controlled date field"
value={value}
onChange={(newValue) => setValue(newValue)}
/>
</Stack>
</LocalizationProvider>
Date validation
The DateField
component supports all the date validation props described in the validation page
TODO: Add link to the future standalone validation doc page
TODO: Add time validation examples when supported
When is onChange
called
The DateField
component has an internal state to update the visible date.
It will only call the onChange
callback when the modified date is valid.
In the demo below, you can see that the component reacts to an external date update (when pressing "Set to today"). And that when debouncing the state (for instance if you have a server side persistence) do not affect the rendering of the field.
Value outside the field: 09/02/2022
<Typography>
Value outside the field: {value == null ? 'null' : format(value, 'P')}
</Typography>
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateField
value={value}
onChange={(newValue) => debounceSetValue(newValue)}
/>
</LocalizationProvider>
<Button
onClick={() => setValue(today)}
disabled={!!value && isEqual(value, today)}
>
Set to today
</Button>