Skip to content

Commit

Permalink
Revert handle event (#4584)
Browse files Browse the repository at this point in the history
* Revert "Add object interface to JSX types"

This reverts commit 9c267a1.

* Revert "add tests"

This reverts commit 4f39d2d.

* Revert "prevent extracting hadleEvent method as an intermediate value"

This reverts commit f7a89c8.

* Revert "Update props.js"

This reverts commit 5b99b99.

* Add test for the future

* Update test/ts/preact.tsx
  • Loading branch information
JoviDeCroock authored Nov 29, 2024
1 parent 99e5e04 commit b9ff1b7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 62 deletions.
5 changes: 1 addition & 4 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,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;
});
});
14 changes: 13 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,14 @@ 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);
}

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

0 comments on commit b9ff1b7

Please sign in to comment.