-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pickers] Clean the validation internals DX to prepare for publication
- Loading branch information
1 parent
48b1714
commit aed4748
Showing
104 changed files
with
806 additions
and
618 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,52 @@ | ||
import * as React from 'react'; | ||
import dayjs from 'dayjs'; | ||
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 TextField from '@mui/material/TextField'; | ||
|
||
import { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
|
||
import { splitFieldInternalAndForwardedProps } from '@mui/x-date-pickers/internals'; | ||
|
||
function ReadOnlyField(props) { | ||
const { internalProps, forwardedProps } = splitFieldInternalAndForwardedProps( | ||
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,73 @@ | ||
import * as React from 'react'; | ||
import dayjs, { Dayjs } from 'dayjs'; | ||
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 TextField from '@mui/material/TextField'; | ||
import { | ||
BaseSingleInputFieldProps, | ||
DateValidationError, | ||
FieldSection, | ||
} from '@mui/x-date-pickers/models'; | ||
import { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
import { UseDateFieldProps } from '@mui/x-date-pickers/DateField'; | ||
import { | ||
BaseDateValidationProps, | ||
DefaultizedProps, | ||
splitFieldInternalAndForwardedProps, | ||
} from '@mui/x-date-pickers/internals'; | ||
|
||
interface ReadOnlyDateFieldProps | ||
extends DefaultizedProps< | ||
UseDateFieldProps<Dayjs, false>, | ||
keyof BaseDateValidationProps<Dayjs> | ||
>, | ||
BaseSingleInputFieldProps< | ||
Dayjs | null, | ||
Dayjs, | ||
FieldSection, | ||
false, | ||
DateValidationError | ||
> {} | ||
|
||
function ReadOnlyField(props: ReadOnlyDateFieldProps) { | ||
const { internalProps, forwardedProps } = splitFieldInternalAndForwardedProps< | ||
typeof props, | ||
keyof UseDateFieldProps<any, any> | ||
>(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
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.