Skip to content

Commit

Permalink
fix: Incorrect onToggle event type (#4615)
Browse files Browse the repository at this point in the history
* fix: Incorrect onToggle event type

* refactor: Backport ToggleEvent
  • Loading branch information
rschristian authored Dec 20, 2024
1 parent 3972db1 commit 3618771
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ type Defaultize<Props, Defaults> =

type Booleanish = boolean | 'true' | 'false';

// Remove when bumping TS minimum to >5.2

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/ToggleEvent) */
interface ToggleEvent extends Event {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/ToggleEvent/newState) */
readonly newState: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/ToggleEvent/oldState) */
readonly oldState: string;
}

declare var ToggleEvent: {
prototype: ToggleEvent;
new(type: string, eventInitDict?: ToggleEventInit): ToggleEvent;
};

interface ToggleEventInit extends EventInit {
newState?: string;
oldState?: string;
}

// End TS >5.2

export namespace JSXInternal {
export type LibraryManagedAttributes<Component, Props> = Component extends {
defaultProps: infer Defaults;
Expand Down Expand Up @@ -508,6 +530,10 @@ export namespace JSXInternal {
Target,
TouchEvent
>;
export type TargetedToggleEvent<Target extends EventTarget> = TargetedEvent<
Target,
ToggleEvent
>;
export type TargetedTransitionEvent<Target extends EventTarget> =
TargetedEvent<Target, TransitionEvent>;
export type TargetedUIEvent<Target extends EventTarget> = TargetedEvent<
Expand Down Expand Up @@ -536,6 +562,9 @@ export namespace JSXInternal {
export type DragEventHandler<Target extends EventTarget> = EventHandler<
TargetedDragEvent<Target>
>;
export type ToggleEventHandler<Target extends EventTarget> = EventHandler<
TargetedToggleEvent<Target>
>;
export type FocusEventHandler<Target extends EventTarget> = EventHandler<
TargetedFocusEvent<Target>
>;
Expand Down Expand Up @@ -597,7 +626,7 @@ export namespace JSXInternal {
onCompositionUpdateCapture?: CompositionEventHandler<Target> | undefined;

// Details Events
onToggle?: GenericEventHandler<Target> | undefined;
onToggle?: ToggleEventHandler<Target> | undefined;

// Dialog Events
onClose?: GenericEventHandler<Target> | undefined;
Expand Down
12 changes: 10 additions & 2 deletions test/ts/preact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ const acceptsStringAsLength = <div style={{ marginTop: '20px' }} />;

const ReturnNull: FunctionalComponent = () => null;

// Should accept arbitrary properties outside of JSX.HTMLAttributes
h('option', { x: 'foo' });
createElement('option', { value: 'foo' });

// Refs should work on elements
const ref = createRef<HTMLDivElement>();
createElement('div', { ref: ref }, 'hi');
Expand All @@ -375,13 +379,17 @@ const onBeforeInput = (e: h.JSX.TargetedInputEvent<HTMLInputElement>) => {};
createElement('input', { onBeforeInput: onBeforeInput });
h('input', { onBeforeInput: onBeforeInput });

// Should accept onSubmit
const onSubmit = (e: h.JSX.TargetedSubmitEvent<HTMLFormElement>) => {};
<form onSubmit={e => e.currentTarget.elements} />;
createElement('form', { onSubmit: onSubmit });
h('form', { onSubmit: onSubmit });

h('option', { value: 'foo' });
createElement('option', { value: 'foo' });
// Should accept onToggle
const onToggle = (e: h.JSX.TargetedToggleEvent<HTMLDetailsElement>) => {};
<dialog onToggle={(e) => ({ newState: e.newState, oldState: e.oldState }) } />;
createElement('dialog', { onToggle: onToggle });
h('dialog', { onToggle: onToggle });

// Should default to correct event target element for the attribute interface
h<JSX.InputHTMLAttributes>('input', { onClick: e => e.currentTarget.capture });
Expand Down

0 comments on commit 3618771

Please sign in to comment.