How Can I Pass Individual Array Item Using AppField
and useFieldContext
?
#1322
-
Hi I am trying to show a list of I understand export const {
fieldContext,
formContext,
useFieldContext,
} = createFormHookContexts();
export const {
useAppForm,
withForm,
} = createFormHook({
fieldComponents: {
ItemEdit,
},
formComponents: {},
fieldContext,
formContext,
});
interface Props {
users: Map<string, User>;
onRemove: () => void;
}
export function ItemEdit({ users, onRemove }: Props) {
const field = useFieldContext<Item>();
console.log(field.state.value);
return (...)
}
export const ItemsForm = withForm({
defaultValues: {
items: [],
},
render: function Render({ form, users }) {
...
}
}); Inside the render function I have tried two things but neither give be the item object inside <form.Field name="items" mode="array">
{(field) =>
field.state.value.map((_, i) => (
<form.AppField name={`items[${i}]`} key={i}> {/* name={`items[${i}]`} is invalid */}
{(subField) => (
<subField.ItemEdit
users={users}
onRemove={() => removeItem(i)}
/>
)}
</form.AppField>
))
}
</form.Field> <form.AppField name="items" mode="array">
{(field) =>
field.state.value.map((_, i) => (
<field.ItemEdit {/* ItemEdit receives the whole array since the context from AppField is an array */}
users={users}
onRemove={() => removeItem(i)}
/>
))
}
</form.AppField> another one I looked into is by making it item itself a subfield. but this still feels a little weird
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I have the exact same problem. A non-typesafe workaround until this is addressed would be to write: name={`segments[${i}]` as any} This way typescript won't complain and the code works as you would expect, the useFieldContext returns the field corresponding to that element. EDIT If the use-case is unclear I am happy to create a stackblitz to show what I mean. |
Beta Was this translation helpful? Give feedback.
-
This issue has been raised before in the discord but haven't really received any input on if array elements will ever be treated as fields themselves. Methods within the source code such as form.validate, form.getFieldValue actually work at runtime when doing: form.getFieldValue(`Array${i}` as DeepKeys<TFormData>) // type casting gets rid of the typescript error. Example: form/packages/form-core/src/FormApi.ts Lines 1952 to 1953 in ddbf5b7 I would highly suggest not doing this within your code since this is likely not supported on purpose. Other libraries do support array elements as fields themselves, hopefully we can get some insight about this! |
Beta Was this translation helpful? Give feedback.
This issue has been raised before in the discord but haven't really received any input on if array elements will ever be treated as fields themselves. Methods within the source code such as form.validate, form.getFieldValue actually work at runtime when doing:
Example:
form/packages/form-core/src/FormApi.ts
Lines 1952 to 1953 in ddbf5b7
I would highly suggest not doing this within your code since this is likely not …