Skip to content

Commit

Permalink
fix(text-field): apply props value when defined and nullish (0, empty…
Browse files Browse the repository at this point in the history
… string, ...)
  • Loading branch information
dackmin committed May 29, 2019
1 parent 0f5813a commit 5bd55ca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';

import { inject } from './style';
import { omit, classNames } from './utils';
import { omit, exists, classNames } from './utils';
import styles from './theme/components/TextField.styl';

class TextField extends React.Component {
Expand Down Expand Up @@ -50,10 +50,10 @@ class TextField extends React.Component {
input = null

state = {
dirty: !!this.props.value,
dirty: false,
focused: false,
valid: this.props.valid || true,
value: this.props.value || '',
valid: exists(this.props.valid) ? this.props.valid : true,
value: exists(this.props.value) ? this.props.value : '',
};

constructor(props) {
Expand All @@ -65,7 +65,7 @@ class TextField extends React.Component {
if (this.props.value !== prevProps.value) {
this.setState({
value: this.props.value,
dirty: !!this.props.value,
dirty: exists(this.props.value),
});
}
}
Expand All @@ -88,7 +88,7 @@ class TextField extends React.Component {

this.setState({
focused: true,
dirty: !!value,
dirty: exists(value),
});
return true;
}
Expand All @@ -112,7 +112,7 @@ class TextField extends React.Component {

this.setState({
focused: false,
dirty: !!value,
dirty: exists(value),
});

return true;
Expand Down Expand Up @@ -141,8 +141,8 @@ class TextField extends React.Component {
reset() {
this.setState({
focused: false,
value: this.props.value || '',
valid: this.props.valid || true,
value: exists(this.props.value) ? this.props.value : '',
valid: exists(this.props.valid) ? this.props.valid : true,
}, () => {
this.props.onChange({});
});
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,9 @@ export const classNames = (...args) => {

return classes.join(' ');
};

export const exists = v => !isNull(v) && !isUndefined(v);

export const isUndefined = v => typeof v === 'undefined';

export const isNull = v => v === null;
51 changes: 51 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
stringifyColor,
getContainerNode,
classNames,
isNull,
isUndefined,
exists,
} from '../src/utils';

describe('injectStyles(styles, options = {})', () => {
Expand Down Expand Up @@ -356,3 +359,51 @@ describe('classNames(...classes)', () => {
});

});

describe('isNull(value)', () => {

it('should return true when value is null', () => {
expect(isNull(null)).toBe(true);
});

it('should return false when value is undefined', () => {
expect(isNull()).toBe(false);
});

it('should return false when value is defined and not null', () => {
expect(isNull(true)).toBe(false);
});

});

describe('isUndefined(value)', () => {

it('should return true when value is undefined', () => {
expect(isUndefined()).toBe(true);
});

it('should return false when value is null', () => {
expect(isUndefined(null)).toBe(false);
});

it('should return false when value is defined and not null', () => {
expect(isUndefined(true)).toBe(false);
});

});

describe('exists(value)', () => {

it('should return true when value is defined and not null', () => {
expect(exists(0)).toBe(true);
});

it('should return false when value is null', () => {
expect(exists(null)).toBe(false);
});

it('should return false when value is not defined', () => {
expect(exists()).toBe(false);
});

});

0 comments on commit 5bd55ca

Please sign in to comment.