-
Notifications
You must be signed in to change notification settings - Fork 113
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
Type Definitions for React CustomEvents #296
Comments
EDIT: welp, not exactly what I expected. I'll see if I could create a fix for that and open a PR. |
Could it be because the So in this case it should be updated with the following? export function event<T>(customName?: string) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (protoOrDescriptor: any, name: string): any => {
const descriptor = {
get(this: HTMLElement) {
return dispatcher<T>(this, customName || name);
},
enumerable: true,
configurable: true,
};
Object.defineProperty(protoOrDescriptor, name, descriptor);
};
} and while declaring events Haven't tested yet so not sure if it will work though, just an idea. |
@AykutSarac is this issue still valid? |
Yes |
Apparently we can set event types like defined here: https://github.com/lit/lit/tree/main/packages/labs/react#typescript I'll try to prioritize this. |
That's awesome, let me know if anything that I can contribute! |
I quickly checked the complexity. Here is what I could find: For having simple event types, it looks easy to set them. Our component analyzer already parses event types. An example from Textarea component1: {
"kind": "class",
"description": "",
"name": "BlTextarea",
"events": [
{
"type": {
"text": "CustomEvent<string>"
},
"name": "bl-input"
},
{
"type": {
"text": "CustomEvent<string>"
},
"name": "bl-change"
},
{
"type": {
"text": "CustomEvent<EventDispatcher<ValidityState>>"
},
"name": "bl-invalid"
}
],
.... We can simply put export const BlTextarea = React.lazy(() =>
customElements.whenDefined("bl-textarea").then((elem) => ({
default: createComponent<BlTextareaType>({
react: React,
tagName: "bl-textarea",
elementClass: elem,
events: {
onInput: "bl-input" as EventName<CustomEvent<string>>,
onChange: "bl-change" as EventName<CustomEvent<string>>,
onInvalid: "bl-invalid" as EventName<CustomEvent<ValidityState>>,
},
}),
}))
); This can already improve the experience a lot, but currently, I used more advanced event types in my PR for select component and this approach will not be enough for setting types for that. Because Select will accept custom types for its values and its event will provide the selected item with its custom type. So we'll need to use Generic types together with React.lazy. Adding generic types to our Baklava React wrappers is the challenge here and I couldn't find a quick solution for now. Footnotes
|
I included this (in a better way, IMO) in my PR about event names: #449 @AykutSarac Please have a look if it works for you. |
Works very well, thank you! |
🎉 This issue has been resolved in version 2.0.0-beta.77 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 2.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Describe the bug
EventHandler properties are not shown for CustomEvents on React & TypeScript
To Reproduce
Steps to reproduce the behavior:
<BlSelect />
componentonSelect
onSelect={e => ...}
you won't be able to see custom properties of Event Handler referenced as "e" however it can be accessedVersions
Baklava v2.0.0-beta.38
React 18.2.0
The text was updated successfully, but these errors were encountered: