Skip to content

Commit

Permalink
fix(NumberInput): Custom increment/decrement and thresholds (#7159)
Browse files Browse the repository at this point in the history
* fix(NumberInput): Custom increment/decrement and thresholds

* apply focus on text input only
  • Loading branch information
jenny-s51 committed Apr 8, 2022
1 parent d0e29f0 commit 28853de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface NumberInputProps extends React.HTMLProps<HTMLDivElement> {
onMinus?: (event: React.MouseEvent, name?: string) => void;
/** Callback for the text input changing */
onChange?: (event: React.FormEvent<HTMLInputElement>) => void;
/** Callback function when text input is blurred (focus leaves) */
onBlur?: (event?: any) => void;
/** Callback for the plus button */
onPlus?: (event: React.MouseEvent, name?: string) => void;
/** Adds the given unit to the number input */
Expand Down Expand Up @@ -66,6 +68,7 @@ export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
isDisabled = false,
onMinus = () => {},
onChange,
onBlur,
onPlus = () => {},
unit,
unitPosition = 'after',
Expand Down Expand Up @@ -116,6 +119,7 @@ export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
aria-label={inputAriaLabel}
{...(isDisabled && { disabled: isDisabled })}
{...(onChange && { onChange })}
{...(onBlur && { onBlur })}
{...(!onChange && { readOnly: true })}
{...inputProps}
onKeyDown={keyDownHandler}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ class CustomStepNumberInput extends React.Component {
};

this.onChange = event => {
const newValue = isNaN(event.target.value) ? 0 : Number(event.target.value);
this.setState({
value: newValue
});
};

this.onBlur = event => {
const newValue = isNaN(event.target.value) ? 0 : Number(event.target.value);
this.setState({
value: newValue > this.maxValue ? this.maxValue : newValue < this.minValue ? this.minValue : newValue
Expand All @@ -463,6 +470,7 @@ class CustomStepNumberInput extends React.Component {
max={this.maxValue}
onMinus={this.stepper(-3)}
onChange={this.onChange}
onBlur={this.onBlur}
onPlus={this.stepper(3)}
inputName="input"
inputAriaLabel="number input"
Expand Down

0 comments on commit 28853de

Please sign in to comment.