-
Notifications
You must be signed in to change notification settings - Fork 4k
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
StrictTextAreaProps has a type that includes number and undefined for the "value" field #4464
Comments
👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you've completed all the fields in the issue template so we can best help. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. |
@skondrashov good call, feel free to submit a PR 😉 |
I looked into this in detail, and I think I understand the problem now, but I don't know what kind of solution would be acceptable. Semantic UI components take callbacks with a signature of:
Where event is some kind of event object, and data is generally just the props that were passed into the component reflected back through the callback. In the cases where the component has a value which updates, the data object has some fields added or overwritten, and the user is supposed to pull the value out of the data. From my perspective, this is a design oversight - in the case of TextArea, for example, the concept of a "value" which is passed into the component is different from the concept of the "value" which the component returns to the user when the user types something into it. Here is the relevant runtime code for TextArea:
It seems that conceptually it is a mistake to call this parameter "data" and to add things into it during callback execution. Instead, it makes sense to call this parameter "props", and add a third parameter called "data" or "value". It is possible to sort of fix this by creating a type that extends the props passed into the component and specifies the data coming back out of the element, but it makes much more sense to me to keep those things conceptually separate - there is an event that the user triggers, there is a props object that the parent code passed to the component, and there is data that the component returns to the parent code. Meshing the latter 2 into one object seems like a flaw, and that flaw is brought into focus in this instance. I'm happy to make/update my PR with whatever is best and to try to cover all of the components, but I know that my idea of creating a third parameter would be a breaking change, so I would like to know what makes sense. code bits for contextThe onChange callback for TextArea says that the data is just the props, and yet splices a value in that does not match the value of the props (what the OP is about):
The onChange callback for Checkbox also says that data is just props, and yet inserts two values into that object which are not explicitly defined in the types at all (they rely on a
the code that inserts new values:
And finally Dropdown does a similar thing, with the type looking like this:
and the insertion of data into the props looking like this:
which again clashes with an existing value prop:
which is a type for what the component can receive, not the type for what the component returns back out. to summarizeI'd like to change all callback type definitions to look like this, whenever the component has some internal value it needs to make available upstream:
and to change the corresponding js code to be like this:
but these would be breaking changes, pretty involved, and have some details to figure out. I'm ready to do it, but let me know what approach I should take! |
I updated my PR, it's not all of the changes I would need to make to go this route - I haven't touched dropdown yet and I would need to make changes in type definition files even for components that don't need js changes, but just to show what they would look like. |
PR updated and contains all the necessary changes, as far as I can see. They're breaking changes, but I think they're necessary (at least in some similar form) to make the API clean and easy to use with TS. |
Verificación de tipo (typeof): Usamos typeof para asegurarnos de que data.value sea una cadena. Si es un número, lo convertimos a string usando String(). |
Bug Report
Steps
Expected Result
No TypeScript errors.
Actual Result
Type 'string | number | undefined' is not assignable to type 'string'.
Version
2.1.5
Testcase
https://codesandbox.io/p/sandbox/semantic-ui-react-forked-5tf5yh
Notes
It doesn't appear to be possible to actually set the value of the textarea to a number or undefined, so it's not clear why I'd have to cast, and it's very inconvenient to cast undefined to a String since
String(undefined)
is"undefined"
instead of""
.The text was updated successfully, but these errors were encountered: