#useForm initial values with empty NumberInput
9 messages · Page 1 of 1 (latest)
use null instead of a number for initial values and have a placeholder of 0 for the number input
You can use undefined as initial value
Thanks. I'll play around with that a bit more. I ran into some TypeScript issues when declaring the type of the FormContext.
If I leave it undefined, won't I get React errors that an input component changed from uncontrolled to controlled?
No, it should not be the case with NumberInput
When calling createFormContext<TypeName>(), is there a way to use different types for the initialValues compared to the generic type that is passed in?
I want to do
type TypeName = {
val1: number;
val2: string;
}
createForcreateFormContextcreateForcreateFormContextmContextmContext<TypeName>()
but call
const form = useForm({ initialValues: {val2: ""} });
I could make TypeName use number | undefined, but then my submit handler isn't typed correctly.
I could make TypeName use number | undefined, but then my submit handler isn't typed correctly.
Why it is not typed correctly?
If I do
useFormContext<Partial<TypeName>()
Then the onSubmit handler will be typed as Partial<TypeName> and val1 would be number | undefined. However, after submitting, all of the values should be defined. I don't want the form onSubmit to be typed as a Partial<>.