Skip to content

Commit

Permalink
[DDW-543] Improve handling of empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikGuzei committed Jan 27, 2021
1 parent 26684e0 commit 83c8937
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
19 changes: 7 additions & 12 deletions source/components/NumericInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,13 @@ class NumericInputBase extends Component<NumericInputProps, State> {
return this.props.bigNumberFormat ?? BigNumber.config().FORMAT;
}

bigNumberToFormattedString(number: ?BigNumber.Instance) {
bigNumberToFormattedString(number: BigNumber.Instance) {
const { bigNumberFormat, decimalPlaces, roundingMode } = this.props;
return number === null
? ''
: number.toFormat(decimalPlaces, roundingMode, {
...BigNumber.config().FORMAT, // defaults
...bigNumberFormat, // custom overrides
});
const result = number.toFormat(decimalPlaces, roundingMode, {
...BigNumber.config().FORMAT, // defaults
...bigNumberFormat, // custom overrides
});
return result === 'NaN' ? null : result;
}

formattedValueToBigNumber(value: string) {
Expand Down Expand Up @@ -373,13 +372,9 @@ class NumericInputBase extends Component<NumericInputProps, State> {
...rest
} = this.props;

if (value != null && !BigNumber.isBigNumber(value)) {
throw new Error('Prop "value" must be of type BigNumber or null.');
}

const inputValue = this.state.fallbackInputValue
? this.state.fallbackInputValue
: this.bigNumberToFormattedString(value);
: this.bigNumberToFormattedString(new BigNumber(value));

return (
<Input
Expand Down
12 changes: 11 additions & 1 deletion stories/NumericInput.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,17 @@ storiesOf('NumericInput', module)
/>
))
)

.add(
'invalid value',
withState({ value: '' }, (store) => (
<NumericInput
value={store.state.value}
onChange={(value) => store.set({ value })}
placeholder="placeholder …"
label="Invalid values are ignored"
/>
))
)
.add(
'with error',
withState({ value: null }, (store) => (
Expand Down

0 comments on commit 83c8937

Please sign in to comment.