Skip to content
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

Merged
merged 6 commits into from
Dec 10, 2024

Conversation

subodhr258
Copy link
Contributor

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:

  • Replaced the reliance on currentValue with a direct check on value?.name to determine the selected option and its hint.
  • The renderSelectedValueHint function now properly reflects changes when the font size is reset, and the dropdown updates immediately.

Testing Instructions

  1. Add font size presets.
  2. Go to block editor.
  3. Add a paragraph to the document.
  4. Open the Typography panel.
  5. Change the font size.
  6. Click “reset” to clear the font size.

Testing Instructions for Keyboard

Screenshots or screencast

Screencast of the fixed issue:

Screen.Recording.2024-12-09.at.3.51.28.PM.mov

@subodhr258 subodhr258 requested a review from ajitbohra as a code owner December 9, 2024 10:35
Copy link

github-actions bot commented Dec 9, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: subodhr258 <subodhrajpopat@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: sarthaknagoshe2002 <sarthaknagoshe2002@git.wordpress.org>
Co-authored-by: Rishit30G <rishit30g@git.wordpress.org>
Co-authored-by: inc2734 <inc2734@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Dec 9, 2024
Copy link

github-actions bot commented Dec 9, 2024

👋 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.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Feature] UI Components Impacts or related to the UI component system labels Dec 9, 2024
@Mamaduka Mamaduka requested a review from a team December 9, 2024 11:20
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing! Please add a changelog, too.

I also noticed a related bug, but let's deal with it separately (#67759).

@@ -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;
Copy link
Member

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:

Suggested change
?.find( ( { name } ) => value?.name === name )?.hint;
?.find( ( { name } ) => store.getState().value === name )?.hint;


return (
<Styled.SelectedExperimentalHintWrapper>
{ currentValue }
{ value?.name }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ value?.name }
{ store.getState().value }

@sarthaknagoshe2002
Copy link
Contributor

I think the argument to the function getDescribedBy on line 208 should also be same as the value in function renderSelectedValueHint to ensure consistency in the selected font and the font name in the screen reader text

@mirka
Copy link
Member

mirka commented Dec 9, 2024

I think the argument to the function getDescribedBy on line 208 should also be same as the value in function renderSelectedValueHint to ensure consistency in the selected font and the font name in the screen reader text

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.

@subodhr258
Copy link
Contributor Author

Hi @mirka!
I have applied the suggested change to get the fresh state on each call, and also updated the changelog. Self-tested the change to see if the issue was fixed and the CustomSelectControl was indeed updated immediately on reset.
Thanks!

@subodhr258 subodhr258 changed the title CustomSelectControl: Prioritized props value over store CustomSelectControl: Update Value from Fresh State Dec 10, 2024
@sarthaknagoshe2002
Copy link
Contributor

sarthaknagoshe2002 commented Dec 10, 2024

I’ve tested this, and the PR LGTM!
Thanks @subodhr258 for the fix! 🚀

Copy link
Member

@mirka mirka left a 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)).
Copy link
Member

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.

@mirka mirka enabled auto-merge (squash) December 10, 2024 17:45
@mirka mirka added [Package] Components /packages/components and removed [Feature] UI Components Impacts or related to the UI component system labels Dec 10, 2024
@mirka mirka merged commit 9387206 into WordPress:trunk Dec 10, 2024
65 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.9 milestone Dec 10, 2024
yogeshbhutkar added a commit to yogeshbhutkar/gutenberg that referenced this pull request Dec 11, 2024
yogeshbhutkar pushed a commit to yogeshbhutkar/gutenberg that referenced this pull request Dec 18, 2024
* CustomSelectControl: Prioritized props value over store

* CustomSelectControl: Refactored Codebase to fix failing testcases

* docs: Add changelog entry for CustomSelectControl font size reset fix

* CustomSelectControl: Fetch the fresh state on each call instead of stale state

* Tweak changelog text

---------

Co-authored-by: subodhr258 <subodhrajpopat@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: sarthaknagoshe2002 <sarthaknagoshe2002@git.wordpress.org>
Co-authored-by: Rishit30G <rishit30g@git.wordpress.org>
Co-authored-by: inc2734 <inc2734@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Select box display does not update when font size is reset
4 participants