-
-
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
[pickers] Use usePickerContext()
and usePickerActionsContext()
instead of passing props to the shortcuts
and toolbar
slots
#15948
base: master
Are you sure you want to change the base?
Conversation
Deploy preview: https://deploy-preview-15948--material-ui-x.netlify.app/ Updated pages: |
2f4fb12
to
6fe5cfa
Compare
layout
slotusePickerContext()
and usePickerActionsContext()
to get the propspassed to the shortcuts
and toolbar
slots
usePickerContext()
and usePickerActionsContext()
to get the propspassed to the shortcuts
and toolbar
slotsusePickerContext()
and usePickerActionsContext()
instead of passing props to the shortcuts
and toolbar
slots
59ca6f1
to
bfb0786
Compare
/** | ||
* Returns a function to check if a value is valid according to the validation props passed to the parent picker. | ||
*/ | ||
export function useIsValidValue<TValue extends PickerValidValue>() { |
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.
For now it's a super dumb hook.
It re-renders everytime the picker render and just calls the function that the picker gives him (which calls the validation).
But introducing this hook will allow us in the future to add it to the Date Calendar and Digital Clock as well and potentially to add some caching mechanism to avoid testing several times the same date.
For now it's only needed for the shortcut slot because we were passing this isValid
prop.
When the view and the field will use it, it will probably impact the shape of the context(s) related to validation, but I'm pretty sure we will still have a hook with this exact signature 👌
bfb0786
to
aed6a76
Compare
* The shortcut that triggered this change. | ||
* Should not be defined if the change does not come from a shortcut. | ||
*/ | ||
shortcut?: PickersShortcutsItemContext; |
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.
In the Base UI version we won't have a notion of shortcut, so this field will probably only live in the MUI version (since some people needed it).
The other will be handled by the Base UI layer.
aed6a76
to
dcc6d9e
Compare
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.
Looks good i general ... only concern is that we could simplify all places where we check for value == null || !utils.isValid(value)
into utils.isValid(value)
... agree?
// This contains a small behavior change. | ||
// If the picker receives an invalid date, | ||
// the old value would equal `null`. | ||
// the new value would equal the invalid date received. |
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.
this feels like something is missing, no?
// This contains a small behavior change. | ||
// If the picker receives an invalid date, | ||
// the old value would equal `null`. | ||
// the new value would equal the invalid date received. |
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.
same as above
const start = range[0] == null || !utils.isValid(range[0]) ? null : range[0]; | ||
const end = range[1] == null || !utils.isValid(range[1]) ? null : range[1]; |
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.
couldn't we reverse the condition here? I did not check, but I think isValid
on the adapters already include a if (value == null) return false
, no?
const start = range[0] == null || !utils.isValid(range[0]) ? null : range[0]; | |
const end = range[1] == null || !utils.isValid(range[1]) ? null : range[1]; | |
const start = utils.isValid(range[0]) ? range[0] : null; | |
const end = utils.isValid(range[1]) ? range[1] : null; |
const translations = usePickerTranslations(); | ||
const ownerState = useToolbarOwnerState(); | ||
const classes = useUtilityClasses(classesProp); | ||
|
||
const dateText = React.useMemo(() => { | ||
if (!value) { | ||
if (value == null || !utils.isValid(value)) { |
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.
same as above ☝🏼
Closes #15495 ( 🙏 )
What?
shortcuts
,layout
andtoolbar
slotsuseIsValidValue
hook to avoid passing something tousePickerContext
that updates on every rendervalue
tousePickerContext
setValue
tousePickerContext
andusePickerActionContext
. This should eventually be the only way for parts of the UI to update the value (it will replace theonChange
prop passed to the views and to the field in the future).PickerShortcutChangeImportance
intoPickerChangeImportance
since it's not only used for the shortcut anymore.Changelog
Breaking changes
The component passed to the
layout
slot no longer receives thevalue
,onChange
andonSelectShortcut
props — Learn more.The component passed to the
toolbar
slot no longer receives thevalue
,onChange
andisLandscape
props — Learn more.The component passed to the
shortcuts
slot no longer receives theonChange
,isValid
andisLandscape
props — Learn more.The
PickerShortcutChangeImportance
type has been renamedPickerChangeImportance
— Learn more.