Type hints when declaring formComponent / fieldComponent
Is it possible to declare a
fieldComponent
/ formComponent
so that it is aware of the form / field it is used from?
For example a Textfield
component that error out if the field it is used in is not of type string
Or an example I did recently: I wanted a generic ArrayField
that displays a consistent field for array fields in form. I did it like this (look into message below)
but it would be really nice to be able to not tie it to current form (in here FormData
) but make it generic, preserving type safety for name
prop5 Replies
funny-blueOP•5mo ago
adverse-sapphire•5mo ago
not with context. However, you can make the following structure:
Usage:
you can also extend this to something like a select option:
Usage
If you want as little extra characters as possible, you could also go for this, though I don't like it very much:
Usage:
One thing to note, this should only be used for inference. Use field context in the component, not this passed property
funny-blueOP•5mo ago
Gotcha
So I assume I should do a similar thing with the formComponent, right?
adverse-sapphire•5mo ago
maybe? I‘ve never tried for form components. If you have a use case example, I could perhaps suggest one way
if the use case is some sort of subform (grouping multiple fields), that will unlikely have a good type safe solution.
funny-blueOP•5mo ago
I pasted the use case above: abstracting the array field to have consistent delete / add new controls in every part of the form