Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextField] Standardize onChange callback and remove valueLink #3699

Merged
merged 1 commit into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/TextField/TextField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ const TextField = React.createClass({
return {
isFocused: false,
errorText: this.props.errorText,
hasValue: isValid(props.value) || isValid(props.defaultValue) ||
(props.valueLink && isValid(props.valueLink.value)),
hasValue: isValid(props.value) || isValid(props.defaultValue),
isClean: true,
muiTheme: this.context.muiTheme || getMuiTheme(),
};
Expand Down Expand Up @@ -344,12 +343,7 @@ const TextField = React.createClass({
nextProps = nextProps.children.props;
}

const hasValueLinkProp = nextProps.hasOwnProperty('valueLink');
const hasValueProp = nextProps.hasOwnProperty('value');

if (hasValueLinkProp) {
newState.hasValue = isValid(nextProps.valueLink.value);
} else if (hasValueProp) {
if (nextProps.hasOwnProperty('value')) {
newState.hasValue = isValid(nextProps.value) ||
(this.state.isClean && isValid(nextProps.defaultValue));
}
Expand Down Expand Up @@ -393,7 +387,7 @@ const TextField = React.createClass({

_handleInputChange(event) {
this.setState({hasValue: isValid(event.target.value), isClean: false});
if (this.props.onChange) this.props.onChange(event);
if (this.props.onChange) this.props.onChange(event, event.target.value);
},

_handleInputFocus(event) {
Expand All @@ -415,8 +409,7 @@ const TextField = React.createClass({
},

_isControlled() {
return this.props.hasOwnProperty('value') ||
this.props.hasOwnProperty('valueLink');
return this.props.hasOwnProperty('value');
},

render() {
Expand Down Expand Up @@ -475,18 +468,15 @@ const TextField = React.createClass({
const inputProps = {
id: inputId,
ref: (elem) => this.input = elem,
disabled: this.props.disabled,
onBlur: this._handleInputBlur,
onChange: this._handleInputChange,
onFocus: this._handleInputFocus,
disabled: this.props.disabled,
onKeyDown: this._handleInputKeyDown,
};

const inputStyleMerged = Object.assign(styles.input, inputStyle);

if (!this.props.hasOwnProperty('valueLink')) {
inputProps.onChange = this._handleInputChange;
}

let inputElement;
if (this.props.children) {
inputElement = React.cloneElement(this.props.children,
Expand Down
14 changes: 14 additions & 0 deletions test/unit/TextField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import TextField from 'src/TextField/TextField';
import TextFieldLabel from 'src/TextField/TextFieldLabel';

describe('<TextField />', () => {
it('passes event and value to the onChange callback', (done) => {
const wrapper = shallow(
<TextField
onChange={(event, value) => {
assert.strictEqual(event.target.value, 'woof');
assert.strictEqual(value, 'woof', 'should pass the value as the 2nd arg');
done();
}}
/>
);

wrapper.find('input').simulate('change', {target: {value: 'woof'}});
});

it('shrinks TextFieldLabel when defaultValue is set and value is null', () => {
const wrapper = shallow(
<TextField
Expand Down