Skip to content

Commit 2c43b56

Browse files
authored
Merge pull request #144 from grucloud/fix-event-types
Fix event types
2 parents 34c21c5 + 6deb5e5 commit 2c43b56

File tree

6 files changed

+46
-71
lines changed

6 files changed

+46
-71
lines changed

bau-ui/examples/bau-storybook/src/pages/form/form-simple-state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export default (context: Context) => {
3636
name: "check",
3737
required: true,
3838
value: inputState,
39-
oninput: (event: any) => (inputState.val = event.target.value),
39+
oninput: ({ target }: { target: HTMLInputElement }) =>
40+
(inputState.val = target.value),
4041
})
4142
)
4243
),

bau-ui/radioButtonGroup/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ declare module "@grucloud/bau-ui/radioButtonGroup" {
1111
export type RadioButtonGroupProps = {
1212
name: string;
1313
value?: any;
14-
oninput?: (event) => any;
14+
oninput?: ({ target }: { target: HTMLInputElement }) => void;
1515
radios: RadioItem[];
1616
} & DefaultDesignProps;
1717

18-
type Component = import("../bau-ui").Component<RadioButtonGroupProps>;
18+
type Component = import("../bau-ui").Component<
19+
RadioButtonGroupProps,
20+
HTMLInputElement
21+
>;
1922

2023
export default function (context: any, option?: ComponentOption): Component;
2124
}

bau/bau.d.ts

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -42,63 +42,32 @@ export type BindAttributeFunc<TElement extends HTMLElement> = (input: {
4242
element: TElement;
4343
}) => Primitive | Function;
4444

45-
type EventListener = (
46-
event:
47-
| InputEvent
48-
| SubmitEvent
49-
| Event
50-
| AnimationEvent
51-
| BeforeUnloadEvent
52-
| BlobEvent
53-
| ClipboardEvent
54-
| CloseEvent
55-
| CompositionEvent
56-
| DeviceMotionEvent
57-
| DeviceOrientationEvent
58-
| DragEvent
59-
| ErrorEvent
60-
| FocusEvent
61-
| FontFaceSetLoadEvent
62-
| FormDataEvent
63-
| GamepadEvent
64-
| HashChangeEvent
65-
| IDBVersionChangeEvent
66-
| InputEvent
67-
| KeyboardEvent
68-
| MessageEvent
69-
| MouseEvent
70-
| OfflineAudioCompletionEvent
71-
| PageTransitionEvent
72-
| PaymentRequestUpdateEvent
73-
| PointerEvent
74-
| PopStateEvent
75-
| ProgressEvent
76-
| RTCDataChannelEvent
77-
| RTCPeerConnectionIceEvent
78-
| StorageEvent
79-
| SubmitEvent
80-
| TouchEvent
81-
| TrackEvent
82-
| TransitionEvent
83-
| UIEvent
84-
| WebGLContextEvent
85-
| WheelEvent
86-
) => any;
87-
8845
export type Props<TElement extends HTMLElement> = {
8946
readonly [key: string]:
9047
| PropValue
91-
| EventListener
9248
| StateView<PropValue>
9349
| BindAttributeFunc<TElement>
9450
| DerivedProp;
9551
};
9652

53+
// This helper will replace the Event parameter of an event handler
54+
// with something else (technically it takes the union of the Event and ReplaceWith)
55+
type ReplaceEventHandlerParameter<EventHandler, ReplaceWith> =
56+
EventHandler extends (...args: infer Params) => infer R
57+
? (...args: {[K in keyof Params]: Params[K] extends Event ? Params[K] & ReplaceWith : Params[K]}) => R
58+
: never;
59+
9760
export type PropsHTMLElement<TElement extends HTMLElement> = {
98-
readonly [key in keyof TElement]?:
99-
| PropValue
100-
| StateView<PropValue>
101-
| BindAttributeFunc<TElement>;
61+
readonly [
62+
key in keyof TElement as key extends `on${string}`
63+
? key
64+
: never
65+
]?: key extends 'oninput'
66+
// we use InputEvent for 'oninput'
67+
// this is not 100% type correct, but it is convenient and backwards compatible
68+
? ReplaceEventHandlerParameter<ReplaceEventHandlerParameter<TElement[key], { target: TElement}>, InputEvent>
69+
// for all other events (and also for oninput) we set the target to TElement
70+
: ReplaceEventHandlerParameter<TElement[key], { target: TElement}>
10271
};
10372

10473
export type PropsLifecycle<TElement extends HTMLElement> = {
@@ -112,9 +81,8 @@ export type PropsLifecycle<TElement extends HTMLElement> = {
11281
};
11382

11483
export type PropsAll<TElement extends HTMLElement> =
115-
| PropsHTMLElement<TElement>
116-
| PropsLifecycle<TElement>
117-
| Props<TElement>
84+
PropsLifecycle<TElement>
85+
| (Props<TElement> & PropsHTMLElement<TElement>)
11886
| ChildDom;
11987

12088
export type BindElementFunc = (input?: {

bau/examples/bau-ts-test/src/main.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,7 @@ const TestDerived = () => {
506506
input({
507507
placeholder: "Enter username",
508508
value: inputState,
509-
oninput: ({ target }: { target: HTMLInputElement }) =>
510-
(inputState.val = target.value),
509+
oninput: ({ target }) => (inputState.val = target.value),
511510
}),
512511
button(
513512
{
@@ -532,8 +531,7 @@ const TestDerivedSideEffect = () => {
532531
input({
533532
placeholder: "Enter username",
534533
value: inputState,
535-
oninput: ({ target }: { target: HTMLInputElement }) =>
536-
(inputState.val = target.value),
534+
oninput: ({ target }) => (inputState.val = target.value),
537535
})
538536
);
539537
};
@@ -753,8 +751,7 @@ const TestInputOninput = () => {
753751
input({
754752
placeholder: "Enter username",
755753
value: inputState,
756-
oninput: ({ target }: { target: HTMLInputElement }) =>
757-
(inputState.val = target.value),
754+
oninput: ({ target }) => (inputState.val = target.value),
758755
}),
759756
button(
760757
{
@@ -776,8 +773,7 @@ const TestInputSearch = () => {
776773
type: "search",
777774
placeholder: "Search...",
778775
value: inputState,
779-
oninput: ({ target }: { target: HTMLInputElement }) =>
780-
(inputState.val = target.value),
776+
oninput: ({ target }) => (inputState.val = target.value),
781777
}),
782778
button(
783779
{
@@ -820,7 +816,7 @@ const TestEventHandlingKeyUp = () => {
820816
input({
821817
type: "search",
822818
size: 25,
823-
onkeyup: ({ target, key }: { key: string; target: HTMLInputElement }) => {
819+
onkeyup: ({ target, key }) => {
824820
if (key == "Enter") {
825821
alert(target.value);
826822
}
@@ -838,8 +834,7 @@ const TestInputCheckboxOninput = () => {
838834
input({
839835
type: "checkbox",
840836
checked: checkedState,
841-
oninput: ({ target }: { target: HTMLInputElement }) =>
842-
(checkedState.val = target.checked),
837+
oninput: ({ target }) => (checkedState.val = target.checked),
843838
}),
844839
div("Is checked: ", () => (checkedState.val ? "Checked" : "Not Checked"))
845840
);
@@ -1044,5 +1039,16 @@ const App = ({}) => {
10441039
);
10451040
};
10461041

1042+
input({
1043+
type: "text",
1044+
oninput: (event) => {
1045+
console.log(event.data);
1046+
console.log(event.target);
1047+
},
1048+
onchange: (event) => {
1049+
console.log(event.target.value);
1050+
},
1051+
});
1052+
10471053
const app = document.getElementById("app");
10481054
app?.replaceChildren(App({}));

doc/BauDerive.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const TestDerived = () => {
4646
input({
4747
placeholder: "Enter username",
4848
value: inputState,
49-
oninput: ({ target }: { target: HTMLInputElement }) =>
50-
(inputState.val = target.value),
49+
oninput: ({ target }) => (inputState.val = target.value),
5150
}),
5251
button(
5352
{

doc/BauEventHandling.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ const TestInputOninput = () => {
9191
input({
9292
placeholder: "Enter username",
9393
value: inputState,
94-
oninput: ({ target }: { target: HTMLInputElement }) =>
95-
(inputState.val = target.value),
94+
oninput: ({ target }) => (inputState.val = target.value),
9695
}),
9796
button(
9897
{
@@ -149,8 +148,7 @@ const TestInputCheckboxOninput = () => {
149148
input({
150149
type: "checkbox",
151150
checked: checkedState,
152-
oninput: ({ target }: { target: HTMLInputElement }) =>
153-
(checkedState.val = target.checked),
151+
oninput: ({ target }) => (checkedState.val = target.checked),
154152
}),
155153
div("Is checked: ", () => (checkedState.val ? "Checked" : "Not Checked"))
156154
);

0 commit comments

Comments
 (0)