-
-
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.
[docs] New recipe of a read-only field (#14606)
Signed-off-by: Flavien DELANGLE <flaviendelangle@gmail.com> Co-authored-by: Lukas Tyla <llukas.tyla@gmail.com>
- Loading branch information
1 parent
c2cff49
commit b1a8dd4
Showing
26 changed files
with
331 additions
and
146 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
docs/data/date-pickers/custom-field/custom-behavior/ReadOnlyMaterialTextField.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,67 @@ | ||
import * as React from 'react'; | ||
|
||
import TextField from '@mui/material/TextField'; | ||
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, | ||
useParsedFormat, | ||
usePickersContext, | ||
} from '@mui/x-date-pickers/hooks'; | ||
import { CalendarIcon } from '@mui/x-date-pickers/icons'; | ||
|
||
function ReadOnlyDateField(props) { | ||
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date'); | ||
const { value, timezone, format } = internalProps; | ||
const { InputProps, slotProps, slots, ...other } = forwardedProps; | ||
|
||
const pickersContext = usePickersContext(); | ||
|
||
const parsedFormat = useParsedFormat(internalProps); | ||
const { hasValidationError } = useValidation({ | ||
validator: validateDate, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
const handleTogglePicker = (event) => { | ||
if (pickersContext.open) { | ||
pickersContext.onClose(event); | ||
} else { | ||
pickersContext.onOpen(event); | ||
} | ||
}; | ||
|
||
return ( | ||
<TextField | ||
{...other} | ||
value={value == null ? '' : value.format(format)} | ||
placeholder={parsedFormat} | ||
InputProps={{ | ||
...InputProps, | ||
readOnly: true, | ||
endAdornment: <CalendarIcon color="action" />, | ||
sx: { cursor: 'pointer', '& *': { cursor: 'inherit' } }, | ||
}} | ||
error={hasValidationError} | ||
onClick={handleTogglePicker} | ||
/> | ||
); | ||
} | ||
|
||
function ReadOnlyFieldDatePicker(props) { | ||
return ( | ||
<DatePicker {...props} slots={{ ...props.slots, field: ReadOnlyDateField }} /> | ||
); | ||
} | ||
|
||
export default function ReadOnlyMaterialTextField() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ReadOnlyFieldDatePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
71 changes: 71 additions & 0 deletions
71
docs/data/date-pickers/custom-field/custom-behavior/ReadOnlyMaterialTextField.tsx
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,71 @@ | ||
import * as React from 'react'; | ||
import { Dayjs } from 'dayjs'; | ||
import TextField from '@mui/material/TextField'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { | ||
DatePicker, | ||
DatePickerProps, | ||
DatePickerFieldProps, | ||
} from '@mui/x-date-pickers/DatePicker'; | ||
import { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
import { | ||
useSplitFieldProps, | ||
useParsedFormat, | ||
usePickersContext, | ||
} from '@mui/x-date-pickers/hooks'; | ||
import { CalendarIcon } from '@mui/x-date-pickers/icons'; | ||
|
||
function ReadOnlyDateField(props: DatePickerFieldProps<Dayjs, false>) { | ||
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date'); | ||
const { value, timezone, format } = internalProps; | ||
const { InputProps, slotProps, slots, ...other } = forwardedProps; | ||
|
||
const pickersContext = usePickersContext(); | ||
|
||
const parsedFormat = useParsedFormat(internalProps); | ||
const { hasValidationError } = useValidation({ | ||
validator: validateDate, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
const handleTogglePicker = (event: React.UIEvent) => { | ||
if (pickersContext.open) { | ||
pickersContext.onClose(event); | ||
} else { | ||
pickersContext.onOpen(event); | ||
} | ||
}; | ||
|
||
return ( | ||
<TextField | ||
{...other} | ||
value={value == null ? '' : value.format(format)} | ||
placeholder={parsedFormat} | ||
InputProps={{ | ||
...InputProps, | ||
readOnly: true, | ||
endAdornment: <CalendarIcon color="action" />, | ||
sx: { cursor: 'pointer', '& *': { cursor: 'inherit' } }, | ||
}} | ||
error={hasValidationError} | ||
onClick={handleTogglePicker} | ||
/> | ||
); | ||
} | ||
|
||
function ReadOnlyFieldDatePicker(props: DatePickerProps<Dayjs>) { | ||
return ( | ||
<DatePicker {...props} slots={{ ...props.slots, field: ReadOnlyDateField }} /> | ||
); | ||
} | ||
|
||
export default function ReadOnlyMaterialTextField() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ReadOnlyFieldDatePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
docs/data/date-pickers/custom-field/custom-behavior/ReadOnlyMaterialTextField.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 @@ | ||
<ReadOnlyFieldDatePicker /> |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
docs/data/date-pickers/experimentation/CustomField.tsx.preview
This file was deleted.
Oops, something went wrong.
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.