Skip to content

Commit

Permalink
Allow number knob to be empty (returns null instead of 0).
Browse files Browse the repository at this point in the history
Prevent uncontrolled to controlled warning when defaulting to null (or undefined).
  • Loading branch information
paradoxxxzero committed Jun 19, 2018
1 parent cc5de35 commit 85e4955
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions addons/knobs/src/components/types/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ const RangeWrapper = styled('div')({
class NumberType extends React.Component {
constructor(props) {
super(props);

this.state = {
value: props.knob.value,
};
let { value } = props.knob;
if (value === null || value === undefined) {
value = '';
}
this.state = { value };

this.onChange = debounce(props.onChange, 400);
}
Expand All @@ -58,7 +59,7 @@ class NumberType extends React.Component {

let parsedValue = Number(value);

if (Number.isNaN(parsedValue)) {
if (Number.isNaN(parsedValue) || value === '') {
parsedValue = null;
}

Expand Down Expand Up @@ -110,7 +111,7 @@ NumberType.propTypes = {
onChange: PropTypes.func,
};

NumberType.serialize = value => String(value);
NumberType.deserialize = value => parseFloat(value);
NumberType.serialize = value => (value === null || value === undefined ? '' : String(value));
NumberType.deserialize = value => (value === '' ? null : parseFloat(value));

export default NumberType;

0 comments on commit 85e4955

Please sign in to comment.