Skip to content

Commit b264502

Browse files
committed
Merge pull request #3450 from theosherry/textFieldbug
[TextField] Fix defaultValue overlays floatingLabelText on mount
2 parents 8cfa6fa + 0f590af commit b264502

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/TextField/TextField.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ const TextField = React.createClass({
306306
errorText: this.props.errorText,
307307
hasValue: isValid(props.value) || isValid(props.defaultValue) ||
308308
(props.valueLink && isValid(props.valueLink.value)),
309+
isClean: true,
309310
muiTheme: this.context.muiTheme || getMuiTheme(),
310311
};
311312
},
@@ -348,7 +349,8 @@ const TextField = React.createClass({
348349
if (hasValueLinkProp) {
349350
newState.hasValue = isValid(nextProps.valueLink.value);
350351
} else if (hasValueProp) {
351-
newState.hasValue = isValid(nextProps.value);
352+
newState.hasValue = isValid(nextProps.value) ||
353+
(this.state.isClean && isValid(nextProps.defaultValue));
352354
}
353355

354356
if (newState) this.setState(newState);
@@ -377,7 +379,7 @@ const TextField = React.createClass({
377379
},
378380

379381
_handleInputChange(event) {
380-
this.setState({hasValue: isValid(event.target.value)});
382+
this.setState({hasValue: isValid(event.target.value), isClean: false});
381383
if (this.props.onChange) this.props.onChange(event);
382384
},
383385

@@ -457,7 +459,6 @@ const TextField = React.createClass({
457459
</TextFieldLabel>
458460
);
459461

460-
461462
const inputProps = {
462463
id: inputId,
463464
ref: (elem) => this.input = elem,

test/unit/TextField.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
import {shallow} from 'enzyme';
3+
import {assert} from 'chai';
4+
import TextField from 'src/TextField/TextField';
5+
import TextFieldLabel from 'src/TextField/TextFieldLabel';
6+
7+
describe('<TextField />', () => {
8+
it('shrinks TextFieldLabel when defaultValue is set and value is null', () => {
9+
const wrapper = shallow(
10+
<TextField
11+
floatingLabelText="floating label text"
12+
defaultValue="default value"
13+
value={null}
14+
/>
15+
);
16+
17+
assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel');
18+
19+
// set a new prop to trigger componentWillReceiveProps
20+
wrapper.setProps({id: '1'});
21+
assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel');
22+
});
23+
24+
it(`unshrinks TextFieldLabel when defaultValue is set, the component has had input change,
25+
and value is re-set to null`, () => {
26+
const wrapper = shallow(
27+
<TextField
28+
floatingLabelText="floating label text"
29+
defaultValue="default value"
30+
value={null}
31+
/>
32+
);
33+
assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel');
34+
35+
// make input change
36+
const input = wrapper.find('input');
37+
input.simulate('change', {target: {value: 'foo'}});
38+
assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel');
39+
40+
// set value to null again, which should unshrink the TextFieldLabel, even though TextField's isClean
41+
// state propety is false.
42+
wrapper.setProps({value: null});
43+
assert.equal(wrapper.state().isClean, false);
44+
assert.equal(wrapper.find(TextFieldLabel).props().shrink, false, 'should shrink TextFieldLabel');
45+
});
46+
});

0 commit comments

Comments
 (0)