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 controlled input values out of sync with dom #1929

Closed
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion mangle.json
Expand Up @@ -39,7 +39,8 @@
"$_render": "__r",
"$_hook": "__h",
"$_catchError": "__e",
"$_unmount": "_e"
"$_unmount": "_e",
"$_lastValue": "__s"
}
}
}
4 changes: 4 additions & 0 deletions src/diff/index.js
Expand Up @@ -255,6 +255,10 @@ function diffElementNodes(dom, newVNode, oldVNode, context, isSvg, excessDomChil
// (as above, don't diff props during hydration)
if (!isHydrating) {
if (('value' in newProps) && newProps.value!==undefined && newProps.value !== dom.value) dom.value = newProps.value==null ? '' : newProps.value;
// preact/#1899
// We need this value for input masking.
dom._lastValue = dom.value;
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved

if (('checked' in newProps) && newProps.checked!==undefined && newProps.checked !== dom.checked) dom.checked = newProps.checked;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/diff/props.js
Expand Up @@ -122,5 +122,6 @@ function setProperty(dom, name, value, oldValue, isSvg) {
* @private
*/
function eventProxy(e) {
return this._listeners[e.type](options.event ? options.event(e) : e);
this._listeners[e.type](options.event ? options.event(e) : e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to still capture the return value from the handler and return it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we do. We attach eventProxy like a standard event handler and those don't return anything:

dom.addEventListener("input"; e => {
  // No need to return anything here
});

if (this._lastValue!=this.value) this.value = this._lastValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic will apply for any event added to an input element, right? So if I add onFocus, I'll need to also make it a controlled input in order for the input to work? Just checking if I understand how this works.

Copy link
Member

@developit developit Sep 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhhh good catch. Maybe this should guard and only apply for onChange/onInput? Though right now it means this works with either, and guards against state mutations in focus/blur (maybe never happens?)

}
41 changes: 35 additions & 6 deletions test/browser/render.test.js
Expand Up @@ -25,6 +25,12 @@ function sortAttributes(html) {
});
}

function fireEvent(on, type) {
let e = document.createEvent('Event');
e.initEvent(type, true, true);
on.dispatchEvent(e);
}

describe('render()', () => {
let scratch, rerender;

Expand Down Expand Up @@ -274,6 +280,35 @@ describe('render()', () => {
expect(root.children[3]).to.have.property('value', '');
});

it('should sync controlled input value', () => {
class Controlled extends Component {
constructor(props) {
super(props);
this.state = { v: '' };
this.onInput = e => {
this.setState({ v: e.target.value.slice(0, 3) });
};
}

render() {
return <input value={this.state.v} onInput={this.onInput} />;
}
}

render(<Controlled />, scratch);

const input = scratch.firstChild;
input.value = 'abc';
fireEvent(input, 'input');
rerender();

input.value = 'abcde';
fireEvent(input, 'input');
expect(input.value).to.equal('abc');
rerender();
expect(input.value).to.equal('abc');
});

it('should set value inside the specified range', () => {
render(
<input type="range" value={0.5} min="0" max="1" step="0.05" />,
Expand Down Expand Up @@ -509,12 +544,6 @@ describe('render()', () => {
describe('event handling', () => {
let proto;

function fireEvent(on, type) {
let e = document.createEvent('Event');
e.initEvent(type, true, true);
on.dispatchEvent(e);
}

beforeEach(() => {
proto = document.createElement('div').constructor.prototype;

Expand Down