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

NumberControl: Allow empty values #33485

Merged
merged 4 commits into from
Jul 27, 2021
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
8 changes: 8 additions & 0 deletions packages/components/src/number-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ The position of the label (`top`, `side`, `bottom`, or `edge`).
- Type: `String`
- Required: No

### required

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

- Type: `Boolean`
- Required: No
- Default: `false`

### shiftStep

Amount to increment by when the `SHIFT` key is held down. This shift value is a multiplier to the `step` value. For example, if the `step` value is `5`, and `shiftStep` is `10`, each jump would increment/decrement by `50`.
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/number-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function NumberControl(
label,
max = Infinity,
min = -Infinity,
required = false,
shiftStep = 10,
step = 1,
type: typeProp = 'number',
Expand Down Expand Up @@ -155,7 +156,11 @@ export function NumberControl(
type === inputControlActionTypes.PRESS_ENTER ||
type === inputControlActionTypes.COMMIT
) {
state.value = roundClamp( currentValue, min, max, step );
const applyEmptyValue = required === false && currentValue === '';

state.value = applyEmptyValue
? currentValue
: roundClamp( currentValue, min, max, step );
}

return state;
Expand All @@ -174,6 +179,7 @@ export function NumberControl(
max={ max }
min={ min }
ref={ ref }
required={ required }
step={ jumpStep }
type={ typeProp }
value={ valueProp }
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/number-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function Example() {
min: number( 'min', 0 ),
max: number( 'max', 100 ),
placeholder: text( 'placeholder', 0 ),
required: boolean( 'required', false ),
shiftStep: number( 'shiftStep', 10 ),
step: number( 'step', 1 ),
};
Expand Down
48 changes: 46 additions & 2 deletions packages/components/src/number-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,58 @@ describe( 'NumberControl', () => {
expect( input.value ).toBe( '0' );
} );

it( 'should parse to number value on ENTER keypress', () => {
render( <NumberControl value={ 5 } /> );
it( 'should parse to number value on ENTER keypress when required', () => {
render( <NumberControl value={ 5 } required={ true } /> );

const input = getInput();
input.focus();
fireEvent.change( input, { target: { value: '10 abc' } } );
fireKeyDown( { keyCode: ENTER } );

expect( input.value ).toBe( '0' );
} );

it( 'should parse to empty string on ENTER keypress when not required', () => {
render( <NumberControl value={ 5 } required={ false } /> );

const input = getInput();
input.focus();
fireEvent.change( input, { target: { value: '10 abc' } } );
fireKeyDown( { keyCode: ENTER } );

expect( input.value ).toBe( '' );
} );

it( 'should accept empty string on ENTER keypress for optional field', () => {
render( <NumberControl value={ 5 } required={ false } /> );

const input = getInput();
input.focus();
fireEvent.change( input, { target: { value: '' } } );
fireKeyDown( { keyCode: ENTER } );

expect( input.value ).toBe( '' );
} );

it( 'should not enforce numerical value for empty string when required is omitted', () => {
render( <NumberControl value={ 5 } /> );

const input = getInput();
input.focus();
fireEvent.change( input, { target: { value: '' } } );
fireKeyDown( { keyCode: ENTER } );

expect( input.value ).toBe( '' );
} );

it( 'should enforce numerical value for empty string when required', () => {
render( <NumberControl value={ 5 } required={ true } /> );

const input = getInput();
input.focus();
fireEvent.change( input, { target: { value: '' } } );
fireKeyDown( { keyCode: ENTER } );

expect( input.value ).toBe( '0' );
} );
} );
Expand Down