From 2b82799e3d4dbd6c9a791e4b319ac85cd1c99ea6 Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Sun, 15 Dec 2024 11:37:06 -0600 Subject: [PATCH] refactor: Add default event target types for HTML attribute interfaces (#4606) * refactor: Assume default targets for HTML attribute interfaces * chore: Formatting * test: Add simple test case for `h` & `createElement` * test: Add simple test case for jsx --- src/jsx.d.ts | 109 ++++++++++++++++++++++++--------------------- test/ts/preact.tsx | 5 +++ 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/jsx.d.ts b/src/jsx.d.ts index 848f7dc6ad..f90939e6f2 100644 --- a/src/jsx.d.ts +++ b/src/jsx.d.ts @@ -1533,7 +1533,7 @@ export namespace JSXInternal { | '_top' | (string & {}); - interface AnchorHTMLAttributes + interface AnchorHTMLAttributes extends HTMLAttributes { download?: Signalish; href?: Signalish; @@ -1548,7 +1548,7 @@ export namespace JSXInternal { referrerPolicy?: Signalish; } - interface AreaHTMLAttributes + interface AreaHTMLAttributes extends HTMLAttributes { alt?: Signalish; coords?: Signalish; @@ -1564,21 +1564,21 @@ export namespace JSXInternal { target?: Signalish; } - interface AudioHTMLAttributes + interface AudioHTMLAttributes extends MediaHTMLAttributes {} - interface BaseHTMLAttributes + interface BaseHTMLAttributes extends HTMLAttributes { href?: Signalish; target?: Signalish; } - interface BlockquoteHTMLAttributes + interface BlockquoteHTMLAttributes extends HTMLAttributes { cite?: Signalish; } - interface ButtonHTMLAttributes + interface ButtonHTMLAttributes extends HTMLAttributes { disabled?: Signalish; form?: Signalish; @@ -1601,47 +1601,49 @@ export namespace JSXInternal { value?: Signalish; } - interface CanvasHTMLAttributes + interface CanvasHTMLAttributes extends HTMLAttributes { height?: Signalish; width?: Signalish; } - interface ColHTMLAttributes extends HTMLAttributes { + interface ColHTMLAttributes + extends HTMLAttributes { span?: Signalish; width?: Signalish; } - interface ColgroupHTMLAttributes + interface ColgroupHTMLAttributes extends HTMLAttributes { span?: Signalish; } - interface DataHTMLAttributes + interface DataHTMLAttributes extends HTMLAttributes { value?: Signalish; } - interface DelHTMLAttributes extends HTMLAttributes { + interface DelHTMLAttributes + extends HTMLAttributes { cite?: Signalish; datetime?: Signalish; dateTime?: Signalish; } - interface DetailsHTMLAttributes + interface DetailsHTMLAttributes extends HTMLAttributes { open?: Signalish; onToggle?: GenericEventHandler | undefined; } - interface DialogHTMLAttributes + interface DialogHTMLAttributes extends HTMLAttributes { onCancel?: GenericEventHandler | undefined; onClose?: GenericEventHandler | undefined; open?: Signalish; } - interface EmbedHTMLAttributes + interface EmbedHTMLAttributes extends HTMLAttributes { height?: Signalish; src?: Signalish; @@ -1649,14 +1651,14 @@ export namespace JSXInternal { width?: Signalish; } - interface FieldsetHTMLAttributes + interface FieldsetHTMLAttributes extends HTMLAttributes { disabled?: Signalish; form?: Signalish; name?: Signalish; } - interface FormHTMLAttributes + interface FormHTMLAttributes extends HTMLAttributes { 'accept-charset'?: Signalish; acceptCharset?: Signalish; @@ -1673,7 +1675,7 @@ export namespace JSXInternal { target?: Signalish; } - interface IframeHTMLAttributes + interface IframeHTMLAttributes extends HTMLAttributes { allow?: Signalish; allowFullScreen?: Signalish; @@ -1703,7 +1705,8 @@ export namespace JSXInternal { type HTMLAttributeCrossOrigin = 'anonymous' | 'use-credentials'; - interface ImgHTMLAttributes extends HTMLAttributes { + interface ImgHTMLAttributes + extends HTMLAttributes { alt?: Signalish; crossorigin?: Signalish; crossOrigin?: Signalish; @@ -1746,7 +1749,7 @@ export namespace JSXInternal { | 'week' | (string & {}); - interface InputHTMLAttributes + interface InputHTMLAttributes extends HTMLAttributes { accept?: Signalish; alt?: Signalish; @@ -1803,13 +1806,14 @@ export namespace JSXInternal { onChange?: GenericEventHandler | undefined; } - interface InsHTMLAttributes extends HTMLAttributes { + interface InsHTMLAttributes + extends HTMLAttributes { cite?: Signalish; datetime?: Signalish; dateTime?: Signalish; } - interface KeygenHTMLAttributes + interface KeygenHTMLAttributes extends HTMLAttributes { challenge?: Signalish; disabled?: Signalish; @@ -1819,18 +1823,19 @@ export namespace JSXInternal { name?: Signalish; } - interface LabelHTMLAttributes + interface LabelHTMLAttributes extends HTMLAttributes { for?: Signalish; form?: Signalish; htmlFor?: Signalish; } - interface LiHTMLAttributes extends HTMLAttributes { + interface LiHTMLAttributes + extends HTMLAttributes { value?: Signalish; } - interface LinkHTMLAttributes + interface LinkHTMLAttributes extends HTMLAttributes { as?: Signalish; crossorigin?: Signalish; @@ -1851,11 +1856,12 @@ export namespace JSXInternal { charSet?: Signalish; } - interface MapHTMLAttributes extends HTMLAttributes { + interface MapHTMLAttributes + extends HTMLAttributes { name?: Signalish; } - interface MarqueeHTMLAttributes + interface MarqueeHTMLAttributes extends HTMLAttributes { behavior?: Signalish<'scroll' | 'slide' | 'alternate' | undefined>; bgColor?: Signalish; @@ -1870,7 +1876,7 @@ export namespace JSXInternal { width?: Signalish; } - interface MediaHTMLAttributes + interface MediaHTMLAttributes extends HTMLAttributes { autoplay?: Signalish; autoPlay?: Signalish; @@ -1888,12 +1894,12 @@ export namespace JSXInternal { volume?: Signalish; } - interface MenuHTMLAttributes + interface MenuHTMLAttributes extends HTMLAttributes { type?: Signalish; } - interface MetaHTMLAttributes + interface MetaHTMLAttributes extends HTMLAttributes { charset?: Signalish; charSet?: Signalish; @@ -1904,7 +1910,7 @@ export namespace JSXInternal { media?: Signalish; } - interface MeterHTMLAttributes + interface MeterHTMLAttributes extends HTMLAttributes { form?: Signalish; high?: Signalish; @@ -1915,7 +1921,7 @@ export namespace JSXInternal { value?: Signalish; } - interface ObjectHTMLAttributes + interface ObjectHTMLAttributes extends HTMLAttributes { classID?: Signalish; data?: Signalish; @@ -1929,19 +1935,20 @@ export namespace JSXInternal { wmode?: Signalish; } - interface OlHTMLAttributes extends HTMLAttributes { + interface OlHTMLAttributes + extends HTMLAttributes { reversed?: Signalish; start?: Signalish; type?: Signalish<'1' | 'a' | 'A' | 'i' | 'I' | undefined>; } - interface OptgroupHTMLAttributes + interface OptgroupHTMLAttributes extends HTMLAttributes { disabled?: Signalish; label?: Signalish; } - interface OptionHTMLAttributes + interface OptionHTMLAttributes extends HTMLAttributes { disabled?: Signalish; label?: Signalish; @@ -1949,7 +1956,7 @@ export namespace JSXInternal { value?: Signalish; } - interface OutputHTMLAttributes + interface OutputHTMLAttributes extends HTMLAttributes { for?: Signalish; form?: Signalish; @@ -1957,24 +1964,24 @@ export namespace JSXInternal { name?: Signalish; } - interface ParamHTMLAttributes + interface ParamHTMLAttributes extends HTMLAttributes { name?: Signalish; value?: Signalish; } - interface ProgressHTMLAttributes + interface ProgressHTMLAttributes extends HTMLAttributes { max?: Signalish; value?: Signalish; } - interface QuoteHTMLAttributes + interface QuoteHTMLAttributes extends HTMLAttributes { cite?: Signalish; } - interface ScriptHTMLAttributes + interface ScriptHTMLAttributes extends HTMLAttributes { async?: Signalish; /** @deprecated */ @@ -1993,7 +2000,7 @@ export namespace JSXInternal { type?: Signalish; } - interface SelectHTMLAttributes + interface SelectHTMLAttributes extends HTMLAttributes { autocomplete?: Signalish; autoComplete?: Signalish; @@ -2008,12 +2015,12 @@ export namespace JSXInternal { onChange?: GenericEventHandler | undefined; } - interface SlotHTMLAttributes + interface SlotHTMLAttributes extends HTMLAttributes { name?: Signalish; } - interface SourceHTMLAttributes + interface SourceHTMLAttributes extends HTMLAttributes { height?: Signalish; media?: Signalish; @@ -2025,14 +2032,14 @@ export namespace JSXInternal { width?: Signalish; } - interface StyleHTMLAttributes + interface StyleHTMLAttributes extends HTMLAttributes { media?: Signalish; scoped?: Signalish; type?: Signalish; } - interface TableHTMLAttributes + interface TableHTMLAttributes extends HTMLAttributes { cellPadding?: Signalish; cellSpacing?: Signalish; @@ -2040,7 +2047,8 @@ export namespace JSXInternal { width?: Signalish; } - interface TdHTMLAttributes extends HTMLAttributes { + interface TdHTMLAttributes + extends HTMLAttributes { align?: Signalish< 'left' | 'center' | 'right' | 'justify' | 'char' | undefined >; @@ -2056,7 +2064,7 @@ export namespace JSXInternal { valign?: Signalish<'top' | 'middle' | 'bottom' | 'baseline' | undefined>; } - interface TextareaHTMLAttributes + interface TextareaHTMLAttributes extends HTMLAttributes { autocomplete?: Signalish; autoComplete?: Signalish; @@ -2079,7 +2087,8 @@ export namespace JSXInternal { onChange?: GenericEventHandler | undefined; } - interface ThHTMLAttributes extends HTMLAttributes { + interface ThHTMLAttributes + extends HTMLAttributes { align?: Signalish< 'left' | 'center' | 'right' | 'justify' | 'char' | undefined >; @@ -2092,13 +2101,13 @@ export namespace JSXInternal { abbr?: Signalish; } - interface TimeHTMLAttributes + interface TimeHTMLAttributes extends HTMLAttributes { datetime?: Signalish; dateTime?: Signalish; } - interface TrackHTMLAttributes + interface TrackHTMLAttributes extends MediaHTMLAttributes { default?: Signalish; kind?: Signalish; @@ -2107,7 +2116,7 @@ export namespace JSXInternal { srcLang?: Signalish; } - interface VideoHTMLAttributes + interface VideoHTMLAttributes extends MediaHTMLAttributes { height?: Signalish; poster?: Signalish; diff --git a/test/ts/preact.tsx b/test/ts/preact.tsx index ba617452de..0272fc3bef 100644 --- a/test/ts/preact.tsx +++ b/test/ts/preact.tsx @@ -383,6 +383,11 @@ h('form', { onSubmit: onSubmit }); h('option', { value: 'foo' }); createElement('option', { value: 'foo' }); +// Should default to correct event target element for the attribute interface +h('input', { onClick: e => e.currentTarget.capture }); +createElement('input', { onClick: e => e.currentTarget.capture }); + e.currentTarget.capture} />; + function Checkbox({ onChange }: JSX.HTMLAttributes) { function handleChange( this: void,