-
Notifications
You must be signed in to change notification settings - Fork 143
Description
Describe the bug and the expected behavior
The new "future" getFieldValue function is very useful and helpful, but the example in the documentation when coupled with useFormData doesn't quite work.
Specifically, the datatype of the formData that's passed into the selector function can be null. The example will produce a type error, and will crash on a first-run.
A simplistic coding fix using the current getFieldValue when combined with useFormData is to just detect the null, by doing something like:
const address = useFormData(form.id, (formData) => {
if (!formData) {
return null;
}
return getFieldValue(formData, fields.address.name, { type: 'object' });
});
It would be much nicer tho to be able to use things as per the example in the docs, and not need this check.
A nice added bonus would be to allow the options object to specify a default value to return when the field value cannot be read. Again, one can potentially work around this, but the code becomes slightly inelegant, especially if one wishes to tighten up typing. For example with the current release one needs to do something like:
const addresses = useFormData(form.id, (formData) => {
if (!formData) {
return [] as Addresses[];
}
return (getFieldValue(formData, 'path.to.addresses', { type: 'object', array: true, optional: true }) || []) as Addresses[];
});
It feels like it should be possible to do something a bit more elegant, maybe something a bit like:
const addresses: Addresses[] = useFormData(
form.id,
(formData) => getFieldValue(
formData,
'path.to.addresses',
{ type: 'object', array: true, optional: true, defaultValue: [] },
),
);
(altho perhaps one would need to use as Addresses[] instead?)
Conform version
v1.15.1
Steps to Reproduce the Bug or Issue
As per description above, following the example in the docs will produce an error. The code there reads:
const address = useFormData(form.id, (formData) =>
getFieldValue(formData, fields.address.name, { type: 'object' }),
);
What browsers are you seeing the problem on?
No response
Screenshots or Videos
No response
Additional context
No response