-
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
RangeControl: clamp initialPosition between min and max values #42571
Conversation
// Value should be clamped on initial load | ||
expect( numberInput?.value ).toBe( '100' ); | ||
expect( rangeInput?.value ).toBe( '100' ); | ||
|
||
fireChangeEvent( numberInput, '50' ); |
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 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
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.
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 ), |
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.
The ?? null
is to fix a type mismatch, since:
initial
can benumber | undefined
floatClamp
acceptsnumber | null
as its first argument
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.
I verified the new unit test fails without d193482 and I could not reproduce the bug testing manually in Storybook. The code 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.
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! 🎉
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.
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.
What?
Clamp the value of
initialPosition
betweenmin
andmax
, so that theRangeControl
's value (and in particular, the value displayed in theNumberControl
) is withinmin
/max
bounds.Why?
This fixes a bug noticed in #40535 (review)
How?
Wrapped
initialPosition
in afloatClamp
call inside theuseControlledRangeValue
hook.Testing Instructions