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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `CustomSelectControl`: Fix font size and hover/focus style inconsistencies with `SelectControl` ([#42460](https://github.com/WordPress/gutenberg/pull/42460/)).
- `AnglePickerControl`: Fix gap between elements in RTL mode ([#42534](https://github.com/WordPress/gutenberg/pull/42534)).
- `RangeControl`: clamp initialPosition between min and max values ([#42571](https://github.com/WordPress/gutenberg/pull/42571)).

### Enhancements

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/range-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ An icon to be shown above the slider next to its container title.

### `initialPosition`: `number`

If no value exists this prop contains the slider starting position.
The slider starting position, used when no `value` is passed. The `initialPosition` will be clamped between the provided `min` and `max` prop values.

- Required: No
- Platform: Web | Mobile
Expand Down
30 changes: 30 additions & 0 deletions packages/components/src/range-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,36 @@ describe( 'RangeControl', () => {

expect( rangeInput?.value ).toBe( '10' );
} );

it( 'should clamp initialPosition between min and max on first render, and on reset', () => {
render(
<RangeControl
initialPosition={ 200 }
min={ 0 }
max={ 100 }
allowReset
/>
);

const numberInput = getNumberInput();
const rangeInput = getRangeInput();
const resetButton = getResetButton();

// Value should be clamped on initial load
expect( numberInput?.value ).toBe( '100' );
expect( rangeInput?.value ).toBe( '100' );

fireChangeEvent( numberInput, '50' );
Comment on lines +218 to +222
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.


expect( numberInput?.value ).toBe( '50' );
expect( rangeInput?.value ).toBe( '50' );

// Value should be clamped after resetting
fireEvent.click( resetButton as Element );

expect( numberInput?.value ).toBe( '100' );
expect( rangeInput?.value ).toBe( '100' );
} );
} );

describe( 'input field', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/range-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ export type RangeControlProps< IconProps = unknown > = Pick<
*/
icon?: string;
/**
* If no value exists this prop contains the slider starting position.
* The slider starting position, used when no `value` is passed.
* The `initialPosition` will be clamped between the provided `min`
* and `max` prop values.
*/
initialPosition?: number;
/**
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/range-control/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

fallback: null,
}
);

const setState = useCallback(
Expand Down