-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] New recipe of a read-only field #14606
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
ffda378
[docs] New recipe of a read-only field
flaviendelangle 81099d2
Fix
flaviendelangle 95075dd
Fix
flaviendelangle 92da0c7
Fix
flaviendelangle b79b5f4
Add onOpen prop
flaviendelangle c7a1032
Fix types
flaviendelangle 66574c3
Fix types
flaviendelangle 00c6329
Fix
flaviendelangle cb32485
Merge branch 'master' into readonly-field
flaviendelangle 15fbfec
Improve doc
flaviendelangle 5768e56
Merge branch 'master' into readonly-field
flaviendelangle 26a2135
Try to move onOpen to a context
flaviendelangle 676aa65
Remove TS changes
flaviendelangle 9f5f302
Fix
flaviendelangle 9837ebf
Fix
flaviendelangle c9b0906
Work
flaviendelangle 9ca0721
Move logic to usePicker
flaviendelangle 1f185cf
Improve typing
flaviendelangle 5f7f8e5
Work
flaviendelangle 7bd2d32
Review Arthur
flaviendelangle f345cba
Merge branch 'master' into readonly-field
flaviendelangle 299a96c
Work
flaviendelangle 164a1de
Work
flaviendelangle 7adf029
Fix
flaviendelangle 77ef242
Review Lukas
flaviendelangle a1bc87f
[pickers] Move the DateFieldInPickerProps interface to the picker fol…
flaviendelangle 3642e3c
Fix
flaviendelangle e6ea1a5
Merge branch 'master' into readonly-field
flaviendelangle cb91ec7
Merge
flaviendelangle 2365f69
Merge
flaviendelangle 07a0285
Review Lukas + JSDoc for useParsedFormat
flaviendelangle 9d5e300
Replace toggling method with onClose and onOpen
flaviendelangle 065dbfc
Merge branch 'master' into readonly-field
flaviendelangle 3e976f8
Update packages/x-date-pickers/src/internals/components/PickersProvid…
flaviendelangle f5b950e
Update packages/x-date-pickers/src/internals/components/PickersProvid…
flaviendelangle 80e16b4
Update packages/x-date-pickers/src/internals/hooks/usePicker/usePicke…
flaviendelangle 2fd6a5a
Update packages/x-date-pickers/src/hooks/useParsedFormat.ts
flaviendelangle 9443b65
Update packages/x-date-pickers/src/hooks/usePickersContext.ts
flaviendelangle 018158d
Improve demo
flaviendelangle 70b1bef
Fix
flaviendelangle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should have done this. 🙈
Now the keyboard navigation and a11y is borked. 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Tab sequence looks good to me, it does not focus the adornment anymore
I don't have the aria label on the
TextField
though, should probably add that (could be a nice addition in the context)