Skip to content

Commit

Permalink
Allow clearing of NumberControl
Browse files Browse the repository at this point in the history
Add new prop to determine if NumberControl should allow empty string value.
  • Loading branch information
aaronrobertshaw committed Jul 16, 2021
1 parent 17898a2 commit 3a8706e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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 @@ -27,6 +27,14 @@ const Example = () => {

## Props

### allowEmpty

If true, allows an empty string as a valid value.

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

### dragDirection

Determines the drag axis to increment/decrement the value.
Expand Down
7 changes: 6 additions & 1 deletion packages/components/src/number-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function NumberControl(
hideHTMLArrows = false,
isDragEnabled = true,
isShiftStepEnabled = true,
allowEmpty = false,
label,
max = Infinity,
min = -Infinity,
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 = allowEmpty && currentValue === '';

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

return state;
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/number-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ describe( 'NumberControl', () => {

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

it( 'should accept empty string on ENTER keypress', () => {
render( <NumberControl value={ 5 } allowEmpty={ true } /> );

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

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

describe( 'Key UP interactions', () => {
Expand Down

0 comments on commit 3a8706e

Please sign in to comment.