Skip to content

Commit

Permalink
fix(Search): make clear button in sync with props (carbon-design-syst…
Browse files Browse the repository at this point in the history
  • Loading branch information
asudoh authored and joshblack committed Jul 30, 2018
1 parent dcfbccb commit 52bdcc6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
"validate-commit-msg": "^2.10.1",
"whatwg-fetch": "^2.0.3"
},
"resolutions": {
"enzyme-adapter-react-16/react-test-renderer": "^16.4.0"
},
"release": {
"branch": "master"
},
Expand Down
33 changes: 33 additions & 0 deletions src/components/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,36 @@ describe('SearchSkeleton Small', () => {
});
});
});

describe('Detecting change in value from props', () => {
it('changes the hasContent state upon change in props', () => {
const wrapper = shallow(
<Search
id="test"
className="extra-class"
label="Search Field"
labelText="testlabel"
value="foo"
/>
);
expect(wrapper.state().hasContent).toBeTruthy();
wrapper.setProps({ value: '' });
expect(wrapper.state().hasContent).toBeFalsy();
});

it('avoids change the hasContent state upon setting props, unless the value actually changes', () => {
const wrapper = shallow(
<Search
id="test"
className="extra-class"
label="Search Field"
labelText="testlabel"
value="foo"
/>
);
expect(wrapper.state().hasContent).toBeTruthy();
wrapper.setState({ hasContent: false });
wrapper.setProps({ value: 'foo' });
expect(wrapper.state().hasContent).toBeFalsy();
});
});
11 changes: 11 additions & 0 deletions src/components/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ export default class Search extends Component {

state = {
hasContent: this.props.value || this.props.defaultValue || false,
prevValue: this.props.value,
};

static getDerivedStateFromProps({ value }, state) {
const { prevValue } = state || {};
return state && prevValue === value
? null
: {
hasContent: !!value,
prevValue: value,
};
}

clearInput = evt => {
if (!this.props.value) {
this.input.value = '';
Expand Down

0 comments on commit 52bdcc6

Please sign in to comment.