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

fix onChange with placeholder on IE #5004

Closed
wants to merge 6 commits into from
Closed
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
25 changes: 24 additions & 1 deletion src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function forceUpdateIfMounted() {
}
}

function hasPlaceholder(props) {
return 'placeholder' in props;
}

/**
* Implements an <input> native component that allows setting these optional
* props: `checked`, `value`, `defaultChecked`, and `defaultValue`.
Expand Down Expand Up @@ -74,12 +78,17 @@ var ReactDOMInput = {
initialChecked: props.defaultChecked || false,
initialValue: defaultValue != null ? defaultValue : null,
onChange: _handleChange.bind(inst),
_currentValue: '',
};
},

mountReadyWrapper: function(inst) {
// Can't be in mountWrapper or else server rendering leaks.
instancesByReactID[inst._rootNodeID] = inst;
var rootNode = ReactMount.getNode(inst._rootNodeID);
if (hasPlaceholder(inst._currentElement.props)) {
inst._wrapperState._currentValue = rootNode.value;
}
},

unmountWrapper: function(inst) {
Expand All @@ -103,18 +112,32 @@ var ReactDOMInput = {
if (value != null) {
// Cast `value` to a string to ensure the value is set correctly. While
// browsers typically do this as necessary, jsdom doesn't.
value = '' + value;
ReactDOMIDOperations.updatePropertyByID(
inst._rootNodeID,
'value',
'' + value
value
);
if (hasPlaceholder(props)) {
inst._wrapperState._currentValue = value;
}
}
},
};

function _handleChange(event) {
var props = this._currentElement.props;

// #5004: IE fires input event for placeholder wrongly
if (hasPlaceholder(props)) {
var value = event.target.value;
if (value === this._wrapperState._currentValue) {
event.stopPropagation();
return undefined;
}
this._wrapperState._currentValue = value;
}

var returnValue = LinkedValueUtils.executeOnChange(props, event);

// Here we use asap to wait until all updates have propagated, which
Expand Down
14 changes: 14 additions & 0 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,18 @@ describe('ReactDOMInput', function() {
<input type="checkbox" checkedLink={link} valueLink={emptyFunction} />;
expect(() => ReactDOM.render(instance, node)).toThrow();
});

it('will not fire onChange when render with placeholder', function() {
var node = document.createElement('div');
document.body.appendChild(node);
var onChange = mocks.getMockFunction();
var parentOnChange = mocks.getMockFunction();
var instance = <div onChange={parentOnChange}><input placeholder="中文" onChange={onChange} /></div>;
// can not use renderIntoDocument
ReactDOM.render(instance, node);
expect(onChange.mock.calls.length).toBe(0);
expect(parentOnChange.mock.calls.length).toBe(0);
ReactDOM.unmountComponentAtNode(node);
document.body.removeChild(node);
});
});