-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
CustomSelectControl: Update Value from Fresh State #67733
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @subodhr258! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
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.
@@ -154,11 +154,11 @@ function CustomSelectControl< T extends CustomSelectOption >( | |||
const renderSelectedValueHint = () => { | |||
const selectedOptionHint = options | |||
?.map( applyOptionDeprecations ) | |||
?.find( ( { name } ) => currentValue === name )?.hint; | |||
?.find( ( { name } ) => value?.name === name )?.hint; |
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 is not "prioritizing the value from the props", it's replacing the value from store with the value from props. The failing unit test tells us that it's a breaking change.
The problem has to do with stale state, due to this component (CustomSelectControl
) not re-rendering after a selection change when in uncontrolled mode. Since renderSelectedValueHint
does get re-executed when _CustomSelect
itself re-renders, we can change the code to get fresh state on each call:
?.find( ( { name } ) => value?.name === name )?.hint; | |
?.find( ( { name } ) => store.getState().value === name )?.hint; |
|
||
return ( | ||
<Styled.SelectedExperimentalHintWrapper> | ||
{ currentValue } | ||
{ value?.name } |
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.
{ value?.name } | |
{ store.getState().value } |
I think the argument to the function |
Right, though in the interest of getting this PR merged as soon as possible, I extracted that issue out to #67759 because it likely requires a separate solution and testing. |
Hi @mirka! |
I’ve tested this, and the PR LGTM! |
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, thank you!
@@ -44,6 +43,7 @@ | |||
### Bug Fixes | |||
|
|||
- `ResizableBox`: Make drag handles focusable ([#67305](https://github.com/WordPress/gutenberg/pull/67305)). | |||
- `CustomSelectControl`: Update correctly when `showSelectedHint` is enabled ([#67733](https://github.com/WordPress/gutenberg/pull/67733)). |
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.
Tweaked the text a bit so it describes the change to any consumer of this component, not a user of the Gutenberg app.
Fixes: #67721
What?
This PR fixes the issue where the font size select box display was not updating immediately when the font size was reset to the default state (fontsize was being set to undefined). The showSelectedHint logic in the CustomSelectControl was interfering with the immediate update of the displayed value.
Why?
When the font size was reset, the showSelectedHint function attempted to render additional UI elements conditionally. In the case when the value was set to undefined, the dropdown display was not updated properly.
How?
This bug is now fixed by prioritizing the value from the props instead of using the state or passing it as an argument. The
renderSelectedValueHint
function was adjusted to handle the value from props:currentValue
with a direct check onvalue?.name
to determine the selected option and its hint.renderSelectedValueHint
function now properly reflects changes when the font size is reset, and the dropdown updates immediately.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast
Screencast of the fixed issue:
Screen.Recording.2024-12-09.at.3.51.28.PM.mov