forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pickers] Allow custom fields to validate the value (mui#14486)
- Loading branch information
1 parent
3967d21
commit 5666dd6
Showing
64 changed files
with
584 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import dayjs from 'dayjs'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DemoContainer } from '@mui/x-date-pickers/internals/demo'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { DatePicker } from '@mui/x-date-pickers/DatePicker'; | ||
import { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks'; | ||
|
||
function ReadOnlyField(props) { | ||
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date'); | ||
|
||
const { value, timezone, format } = internalProps; | ||
const { InputProps, slotProps, slots, ...other } = forwardedProps; | ||
|
||
const { hasValidationError } = useValidation({ | ||
validator: validateDate, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
return ( | ||
<TextField | ||
{...other} | ||
value={value == null ? '' : value.format(format)} | ||
InputProps={{ ...InputProps, readOnly: true }} | ||
error={hasValidationError} | ||
/> | ||
); | ||
} | ||
|
||
export default function CustomField() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<DemoContainer components={['DatePicker']}> | ||
<DatePicker | ||
label="Date Picker" | ||
slots={{ field: ReadOnlyField }} | ||
maxDate={dayjs('2022-04-17')} | ||
defaultValue={dayjs('2022-04-18')} | ||
/> | ||
</DemoContainer> | ||
</LocalizationProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import dayjs, { Dayjs } from 'dayjs'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DemoContainer } from '@mui/x-date-pickers/internals/demo'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { DatePicker } from '@mui/x-date-pickers/DatePicker'; | ||
import { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks'; | ||
import { DateFieldInPickerProps } from '@mui/x-date-pickers/DateField'; | ||
|
||
function ReadOnlyField(props: DateFieldInPickerProps<Dayjs, false>) { | ||
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date'); | ||
|
||
const { value, timezone, format } = internalProps; | ||
const { InputProps, slotProps, slots, ...other } = forwardedProps; | ||
|
||
const { hasValidationError } = useValidation({ | ||
validator: validateDate, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
return ( | ||
<TextField | ||
{...other} | ||
value={value == null ? '' : value.format(format)} | ||
InputProps={{ ...InputProps, readOnly: true }} | ||
error={hasValidationError} | ||
/> | ||
); | ||
} | ||
|
||
export default function CustomField() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<DemoContainer components={['DatePicker']}> | ||
<DatePicker | ||
label="Date Picker" | ||
slots={{ field: ReadOnlyField }} | ||
maxDate={dayjs('2022-04-17')} | ||
defaultValue={dayjs('2022-04-18')} | ||
/> | ||
</DemoContainer> | ||
</LocalizationProvider> | ||
); | ||
} |
6 changes: 6 additions & 0 deletions
6
docs/data/date-pickers/experimentation/CustomField.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<DatePicker | ||
label="Date Picker" | ||
slots={{ field: ReadOnlyField }} | ||
maxDate={dayjs('2022-04-17')} | ||
defaultValue={dayjs('2022-04-18')} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
productId: x-date-pickers | ||
--- | ||
|
||
# Date and Time Pickers experimentation | ||
|
||
<p class="description">Demos not accessible through the navbar of the doc</p> | ||
|
||
## Custom field | ||
|
||
{{"demo": "CustomField.js"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import * as pageProps from 'docsx/data/date-pickers/experimentation/experimentation.md?muiMarkdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs {...pageProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.