Skip to content

Commit c746ecf

Browse files
Ensure both onchange and oninput callbacks are executes when typing (#3562)
* ensure both onchange and oninput callbacks are attached to the event listener * remove custom hook and combiner callbacks in one * update implementation and unit test * remove only on tests and improve error handling * cover scenario changing order * allow uncaught error in unit test * use event hook to trigger oncompatchange handler * use oninputCapture rather than oninput when both callbacks are defined Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>
1 parent 0b9a927 commit c746ecf

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

compat/src/render.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ options.vnode = vnode => {
161161
value = undefined;
162162
}
163163

164+
// Add support for onInput and onChange, see #3561
165+
// if we have an oninput prop already change it to oninputCapture
166+
if (/^oninput/i.test(i)) {
167+
i = i.toLowerCase();
168+
if (normalizedProps[i]) {
169+
i = 'oninputCapture';
170+
}
171+
}
172+
164173
normalizedProps[i] = value;
165174
}
166175

compat/test/browser/render.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ describe('compat render', () => {
158158
expect(scratch.firstElementChild.value).to.equal('0');
159159
});
160160

161+
it('should call onChange and onInput when input event is dispatched', () => {
162+
const onChange = sinon.spy();
163+
const onInput = sinon.spy();
164+
165+
render(<input onChange={onChange} onInput={onInput} />, scratch);
166+
167+
scratch.firstChild.dispatchEvent(createEvent('input'));
168+
169+
expect(onChange).to.be.calledOnce;
170+
expect(onInput).to.be.calledOnce;
171+
172+
onChange.resetHistory();
173+
onInput.resetHistory();
174+
175+
// change props order
176+
render(<input onInput={onInput} onChange={onChange} />, scratch);
177+
178+
scratch.firstChild.dispatchEvent(createEvent('input'));
179+
180+
expect(onChange).to.be.calledOnce;
181+
expect(onInput).to.be.calledOnce;
182+
});
183+
161184
it('should keep value of uncontrolled inputs using defaultValue', () => {
162185
// See https://github.com/preactjs/preact/issues/2391
163186

0 commit comments

Comments
 (0)