Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface NumberInputProps extends React.HTMLProps<HTMLDivElement> {
minusBtnProps?: ButtonProps;
/** Additional properties added to the plus button */
plusBtnProps?: ButtonProps;
/** Allow the user to clear out the input to an empty string (recommended) */
allowEmptyInput?: boolean;
}

type DefaultKeyDownHandlerArgs = Pick<NumberInputProps, 'inputName' | 'onMinus' | 'onPlus'>;
Expand Down Expand Up @@ -87,8 +89,12 @@ export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
inputProps,
minusBtnProps,
plusBtnProps,
allowEmptyInput = false,
...props
}: NumberInputProps) => {
if (!allowEmptyInput) {
value = value || 0;
}
const numberInputUnit = <div className={css(styles.numberInputUnit)}>{unit}</div>;
const keyDownHandler =
inputProps && inputProps.onKeyDown ? inputProps.onKeyDown : defaultKeyDownHandler({ inputName, onMinus, onPlus });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,15 @@ describe('numberInput', () => {
expect(input).toHaveDisplayValue('0');
});

test('renders nothing if null value passed', () => {
test('renders 0 if null value passed without allow empty', () => {
render(<NumberInput value={null} />);
const input = screen.getByRole('spinbutton');
expect(input).toHaveDisplayValue('0');
});

test('renders nothing if null value passed with allow empty', () => {
render(<NumberInput allowEmptyInput value={null} />);
const input = screen.getByRole('spinbutton');
expect(input).toHaveDisplayValue('');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const NumberInputCustomStep: React.FunctionComponent = () => {
inputAriaLabel="number input"
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
allowEmptyInput
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const NumberInputCustomStepAndThreshold: React.FunctionComponent = () =>
inputAriaLabel="number input"
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
allowEmptyInput
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const NumberInputDefault: React.FunctionComponent = () => {
inputAriaLabel="number input"
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
allowEmptyInput
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const NumberInputDisabled: React.FunctionComponent = () => {
plusBtnAriaLabel="plus"
unit="%"
isDisabled
allowEmptyInput
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const NumberInputUnit: React.FunctionComponent = () => {
minusBtnAriaLabel="minus 1"
plusBtnAriaLabel="plus 1"
unit="%"
allowEmptyInput
/>
<br />
<br />
Expand All @@ -51,6 +52,7 @@ export const NumberInputUnit: React.FunctionComponent = () => {
plusBtnAriaLabel="plus 0.01"
unit="$"
unitPosition="before"
allowEmptyInput
/>
</React.Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const NumberInputUnitThreshold: React.FunctionComponent = () => {
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
unit="%"
allowEmptyInput
/>
</React.Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const NumberInputVaryingSizes: React.FunctionComponent = () => {
minusBtnAriaLabel="input 2 minus"
plusBtnAriaLabel="input 2 plus"
widthChars={1}
allowEmptyInput
/>
<br />
<br />
Expand All @@ -48,6 +49,7 @@ export const NumberInputVaryingSizes: React.FunctionComponent = () => {
minusBtnAriaLabel="input 2 minus"
plusBtnAriaLabel="input 2 plus"
widthChars={10}
allowEmptyInput
/>
<br />
<br />
Expand All @@ -61,6 +63,7 @@ export const NumberInputVaryingSizes: React.FunctionComponent = () => {
minusBtnAriaLabel="input 3 minus"
plusBtnAriaLabel="input 3 plus"
widthChars={5}
allowEmptyInput
/>
<br />
<br />
Expand All @@ -74,6 +77,7 @@ export const NumberInputVaryingSizes: React.FunctionComponent = () => {
minusBtnAriaLabel="input 4 minus"
plusBtnAriaLabel="input 4 plus"
widthChars={5}
allowEmptyInput
/>
</React.Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const NumberInputWithStatus: React.FunctionComponent = () => {
inputAriaLabel="number input"
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
allowEmptyInput
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export const AlertGroupToastWithNotificationDrawer: React.FunctionComponent = ()
minusBtnAriaLabel="minus"
plusBtnAriaLabel="plus"
style={{ margin: '12px 0' }}
allowEmptyInput
/>
</PageSection>
<PageSection variant={PageSectionVariants.light}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class NumberInputDemo extends Component<NumberInputDemoState> {
plusBtnProps={{ id: 'plus-button' }}
unit="%"
widthChars={5}
allowEmptyInput
/>
<br />
<br />
Expand All @@ -108,6 +109,7 @@ export class NumberInputDemo extends Component<NumberInputDemoState> {
unit="$"
unitPosition="before"
isDisabled
allowEmptyInput
/>
<br />
<br />
Expand All @@ -129,6 +131,7 @@ export class NumberInputDemo extends Component<NumberInputDemoState> {
plusBtnProps={{ id: 'plus-button3' }}
unit="$"
unitPosition="before"
allowEmptyInput
/>
</React.Fragment>
);
Expand Down