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

Revert handle event #4584

Merged
merged 7 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 1 addition & 2 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ function createEventProxy(useCapture) {
} else if (e._dispatched < eventHandler._attached) {
return;
}
if (options.event) e = options.event(e);
return "handleEvent" in eventHandler ? eventHandler.handleEvent(e) : eventHandler(e);
return eventHandler(options.event ? options.event(e) : e);
}
};
}
Expand Down
12 changes: 3 additions & 9 deletions src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,9 @@ export namespace JSXInternal {
export type TargetedPictureInPictureEvent<Target extends EventTarget> =
TargetedEvent<Target, PictureInPictureEvent>;

export type EventHandlerObject<E extends TargetedEvent> = {
handleEvent(e: E): unknown;
};

export type EventHandler<E extends TargetedEvent> =
| {
bivarianceHack(event: E): void;
}['bivarianceHack']
| EventHandlerObject<E>;
export type EventHandler<E extends TargetedEvent> = {
bivarianceHack(event: E): void;
}['bivarianceHack'];

export type AnimationEventHandler<Target extends EventTarget> = EventHandler<
TargetedAnimationEvent<Target>
Expand Down
48 changes: 0 additions & 48 deletions test/browser/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,52 +235,4 @@ describe('event handling', () => {
.to.have.been.calledTwice.and.to.have.been.calledWith('focusin')
.and.calledWith('focusout');
});

it('should register EventListenerObject as handler', () => {
let handler = { handleEvent() {} }

render(<div onClick={handler} />, scratch);

expect(scratch.childNodes[0].attributes.length).to.equal(0);

expect(proto.addEventListener).to.have.been.calledOnce;
});

it('should call registered EventListenerObject through the .handleEvent() method', () => {
let onclick = sinon.spy();

let handler = {
onclick,
handleEvent() {
this.onclick()
}
}

render(<div onClick={handler} />, scratch);

fireEvent(scratch.childNodes[0], 'click');

expect(onclick).to.have.been.calledOnce;
});

it('should keep the registered EventListenerObject referentially identical when calling', () => {
let onclick = sinon.spy();

// if the handler object was destructured or otherwise copied, this will fail
let handler = new class {
onclick = onclick
#onClick() {
this.onclick()
}
handleEvent() {
this.#onClick()
}
}

render(<div onClick={handler} />, scratch);

fireEvent(scratch.childNodes[0], 'click');

expect(onclick).to.have.been.calledOnce;
});
});
15 changes: 14 additions & 1 deletion test/ts/preact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
FunctionalComponent,
AnyComponent,
h,
createRef
createRef,
JSX
} from '../../';

interface DummyProps {
Expand Down Expand Up @@ -381,3 +382,15 @@ h('form', { onSubmit: onSubmit });

h('option', { value: 'foo' });
createElement('option', { value: 'foo' });

function Checkbox({ onChange }: JSX.HTMLAttributes<HTMLInputElement>) {
function handleChange(
this: void,
event: JSX.TargetedEvent<HTMLInputElement>
) {
onChange?.call(this, event);
// ^ Error here
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
}

return <input onChange={handleChange} />;
}