Skip to content

Commit d91e6a4

Browse files
committed
Use event.target.validity.valid in docs, storybook and unit tests
1 parent ee4336a commit d91e6a4

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

packages/components/src/number-control/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ The minimum `value` allowed.
9797
- Required: No
9898
- Default: `-Infinity`
9999

100+
### onChange
101+
102+
Callback fired whenever the value of the input changes.
103+
104+
The callback receives two arguments:
105+
106+
1. `newValue`: the new value of the input
107+
2. `extra`: an object containing, under the `event` key, the original browser event.
108+
109+
Note that the value received as the first argument of the callback is _not_ guaranteed to be a valid value (e.g. it could be outside of the range defined by the [`min`, `max`] props). In order to check the value's validity, check the `event.target.validity.valid` property from the callback's second argument.
110+
111+
- Type: `(newValue, extra) => void`
112+
- Required: No
113+
100114
### required
101115

102116
If `true` enforces a valid number within the control's min/max range. If `false` allows an empty string as a valid value.

packages/components/src/number-control/stories/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default {
2323

2424
function Example() {
2525
const [ value, setValue ] = useState( '0' );
26+
const [ isValidValue, setIsValidValue ] = useState( true );
2627

2728
const props = {
2829
disabled: boolean( 'disabled', false ),
@@ -32,18 +33,24 @@ function Example() {
3233
label: text( 'label', 'Number' ),
3334
min: number( 'min', 0 ),
3435
max: number( 'max', 100 ),
35-
placeholder: text( 'placeholder', 0 ),
36+
placeholder: text( 'placeholder', '0' ),
3637
required: boolean( 'required', false ),
3738
shiftStep: number( 'shiftStep', 10 ),
38-
step: text( 'step', 1 ),
39+
step: text( 'step', '1' ),
3940
};
4041

4142
return (
42-
<NumberControl
43-
{ ...props }
44-
value={ value }
45-
onChange={ ( v ) => setValue( v ) }
46-
/>
43+
<>
44+
<NumberControl
45+
{ ...props }
46+
value={ value }
47+
onChange={ ( v, extra ) => {
48+
setValue( v );
49+
setIsValidValue( extra.event.target.validity.valid );
50+
} }
51+
/>
52+
<p>Is valid? { isValidValue ? 'Yes' : 'No' }</p>
53+
</>
4754
);
4855
}
4956

packages/components/src/number-control/test/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ describe( 'NumberControl', () => {
9494
expect( spy ).toHaveBeenNthCalledWith( 2, 4 );
9595
} );
9696
} );
97+
98+
it( 'should call onChange callback when value is not valid', () => {
99+
const spy = jest.fn();
100+
render(
101+
<NumberControl
102+
value={ 5 }
103+
min={ 1 }
104+
max={ 10 }
105+
onChange={ ( v, extra ) =>
106+
spy( v, extra.event.target.validity.valid )
107+
}
108+
/>
109+
);
110+
111+
const input = getInput();
112+
input.focus();
113+
fireEvent.change( input, { target: { value: 14 } } );
114+
115+
expect( input.value ).toBe( '14' );
116+
117+
fireKeyDown( { keyCode: ENTER } );
118+
119+
expect( input.value ).toBe( '10' );
120+
121+
expect( spy ).toHaveBeenCalledTimes( 2 );
122+
123+
// First call: invalid, unclamped value
124+
expect( spy ).toHaveBeenNthCalledWith( 1, '14', false );
125+
// Second call: valid, clamped value
126+
expect( spy ).toHaveBeenNthCalledWith( 2, 10, true );
127+
} );
97128
} );
98129

99130
describe( 'Validation', () => {

0 commit comments

Comments
 (0)