-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] Add new method onToggleOpening
to usePickerContext()
#15701
Open
flaviendelangle
wants to merge
1
commit into
mui:master
Choose a base branch
from
flaviendelangle:ctx-onToggleOpening
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+29
−113
Open
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as React from 'react'; | ||
import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; | ||
import useEventCallback from '@mui/utils/useEventCallback'; | ||
import { PickerOwnerState } from '../../../models'; | ||
import { PickerValueManager, UsePickerValueResponse } from './usePickerValue.types'; | ||
import { | ||
|
@@ -67,6 +68,14 @@ export function usePickerProvider<TValue extends PickerValidValue>( | |
const utils = useUtils(); | ||
const orientation = usePickerOrientation(views, props.orientation); | ||
|
||
const handleTogglePicker = useEventCallback((event: React.UIEvent) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be an action exposed by |
||
if (pickerValueResponse.open) { | ||
pickerValueResponse.actions.onClose(event); | ||
} else { | ||
pickerValueResponse.actions.onOpen(event); | ||
} | ||
}); | ||
|
||
const ownerState = React.useMemo<PickerOwnerState>( | ||
() => ({ | ||
isPickerValueEmpty: valueManager.areValuesEqual( | ||
|
@@ -96,6 +105,7 @@ export function usePickerProvider<TValue extends PickerValidValue>( | |
() => ({ | ||
onOpen: pickerValueResponse.actions.onOpen, | ||
onClose: pickerValueResponse.actions.onClose, | ||
onToggleOpening: handleTogglePicker, | ||
open: pickerValueResponse.open, | ||
disabled: props.disabled ?? false, | ||
readOnly: props.readOnly ?? false, | ||
|
@@ -105,6 +115,7 @@ export function usePickerProvider<TValue extends PickerValidValue>( | |
[ | ||
pickerValueResponse.actions.onOpen, | ||
pickerValueResponse.actions.onClose, | ||
handleTogglePicker, | ||
pickerValueResponse.open, | ||
variant, | ||
orientation, | ||
|
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.
WDYT about naming the method (renaming
onOpen
andonClose
accordingly) a bit differently to have a usualonClick={handleClick}
scenario? 🤔handleOpenToggle
handleToggleOpen
toggleOpenState
togglePickerOpen
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.
onOpen
andonClose
are present in v7 so it's a small BC.But if you think it's cleaner I'm fine doing it.
I'm not a fan of the
handleXXX
when they are provided by a hook (or as a prop).I use them when creating variables like:
But when it comes from a button it feels off to me:
Not sure why 😆
Maybe because
handleXXX
is a nomenclature used only for event handler and nothing prevents the user to use the methods from the hook in something else (in an effect, when a server request loads etc...).For this kind of hook, if we don't want to
onXXX
, I would take to prefer a naming likeopenPicker
,closePicker
andtogglePickerOpening
for example.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.
Great insights, especially regarding the possibility of this function being called in other places than the even handler. 👌
They are and didn't even cross my eyes until you started working on this change, then it became a bit more clear, that we can improve clarity here. 🙈
But they are present in the
valueResponse
.Couldn't we update the naming only in the context? 🤔
I would not mind having
openPicker
,closePicker
, andtogglePickerOpen
(ing) (theopening
word here sounds slightly strange 🙈 🤷 ).P.S. Have we explored going for
setPickerOpen
instead ofonOpen
andonClose
?Noticed such an approch here:
https://github.com/adobe/react-spectrum/blob/e94e36431149b6d3e9ed5900de30b538c0110584/packages/%40react-stately/datepicker/src/useDatePickerState.ts#L55-L56
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.
But the context was introduced in a later v7 release I think 😬
So renaming them is a BC
But probably almost nobody is using it for now, if we want to break it, let's do it before we document it.
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.
That's yet another nomenclature indeed.
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.
WDYT about the following:
setOpen
openPicker
closePicker
We keep the 3 methods for maximal flexibility, we don't have any "handle" or "on" prefix.
And IMHO
setOpen
is cleared that it's like asetState
that takes the new state,setPickerOpen
is slightly less clear to me (I would expect it to just open the picker).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.
Oh shoot, I forgot that we introduced it in v7 as well. 🙈 🥷
Yeah, I'm all for refining the naming and not documenting too much on v7.
Maybe we can also skimp on having a codemod for the renaming. 😆
Didn't we plan to add a
togglePickerOpen
function to ease the simple use-case examples?I had in mind if we considered having
setOpen
instead ofopenPicker
andclosePicker
combination. 🤔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.
Oh right, I got mixed up with my needs on the composition API here...
So if we start from the beginning, to modify the opening state of the picker, there are 4 potential methods:
Right now the context has 1. and 2.
I'm proposing to add 3. because it makes all the demos of custom fields easier to read.
And we will need 4. in the composition API because most modal / popover component take a prop like
onOpenChange
(Base UI and React Aria both do).So if I understand your comment correctly, you are proposing having 3 and 4 and dropping 1 and 2.
In which case maybe
toggleOpen
andsetOpen
would be good names.