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

RangeControl: clamp initialPosition between min and max values #42571

Merged
merged 4 commits into from
Jul 21, 2022

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Jul 20, 2022

What?

Clamp the value of initialPosition between min and max, so that the RangeControl's value (and in particular, the value displayed in the NumberControl) is within min/max bounds.

Why?

This fixes a bug noticed in #40535 (review)

How?

Wrapped initialPosition in a floatClamp call inside the useControlledRangeValue hook.

Testing Instructions

@ciampo ciampo requested a review from ajitbohra as a code owner July 20, 2022 18:57
@ciampo ciampo self-assigned this Jul 20, 2022
@ciampo ciampo added [Type] Bug An existing feature does not function as intended [Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components labels Jul 20, 2022
Comment on lines +218 to +222
// Value should be clamped on initial load
expect( numberInput?.value ).toBe( '100' );
expect( rangeInput?.value ).toBe( '100' );

fireChangeEvent( numberInput, '50' );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This unit test is intentionally written using fireEvent and checking directly the input's value, so that it aligns to the style used for the rest of RangeControl's unit tests.

It would be great to refactor these tests to use user-event, but I currently don't have capacity

Copy link
Contributor

Choose a reason for hiding this comment

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

It works well now and until/unless user-event adds any sort of feature to support range inputs, I'd say this suite should be last on the list for such a refactor.

@@ -50,7 +50,10 @@ export function useControlledRangeValue(
const { min, max, value: valueProp, initial } = settings;
const [ state, setInternalState ] = useControlledState(
floatClamp( valueProp, min, max ),
{ initial, fallback: null }
{
initial: floatClamp( initial ?? null, min, max ),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ?? null is to fix a type mismatch, since:

  • initial can be number | undefined
  • floatClamp accepts number | null as its first argument

Copy link
Contributor

@stokesman stokesman left a comment

Choose a reason for hiding this comment

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

I verified the new unit test fails without d193482 and I could not reproduce the bug testing manually in Storybook. The code LGTM.

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

This is working nicely for me, too, and confirmed that it resolves the bug reported in the other review in Storybook, and I didn't encounter any issues while smoke testing RangeControl components in the editor.

Totally optional, but is it worth updating the wording in the readme for the initialPosition prop? Currently it says:

If no value exists this prop contains the slider starting position.

Perhaps:

If no value exists this prop contains the slider starting position. The initialPosition will be clamped between the provided min and max prop values.

LGTM! 🎉

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

I might be a bit late to this party but thanks for following up on this one @ciampo 👍

The fix and the doc updates LGTM!

✅ Could replicate the original problem
✅ Unit tests pass, and no typing errors
✅ This PR fixes the issue
✅ Component functions well in storybook and editors.

@ciampo ciampo merged commit b346cd4 into trunk Jul 21, 2022
@ciampo ciampo deleted the fix/range-control-clamp-reset-position branch July 21, 2022 09:39
@github-actions github-actions bot added this to the Gutenberg 13.8 milestone Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [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.

4 participants