using useFieldContext with an Object
Hey, couldn't find any info on this, is this the correct/a possible way to work with objects? I know I could do
This is my component:
And this is the Type definition:
name="object.property"name="object.property", however I probably want to be able to change multiple properties of the same object in 1 componentThis is my component:
import { useFieldContext } from "@/hooks/form";
import type { HeldTalent } from "@/schemas/held.schema";
import { TableCell, TableRow } from "./ui/table";
import { talentByName } from "@/lib/talente";
import { Input } from "./ui/input";
export default function TalentRow() {
const field = useFieldContext<HeldTalent>();
const talent = talentByName(field.state.value.talentName);
if(!talent) return;
return (
<TableRow>
<TableCell>{talent.name}</TableCell>
<TableCell>
<Input
type="number"
id={field.name}
value={field.state.value.TaW}
onChange={(e) => field.handleChange({...field.state.value, TaW: e.target.valueAsNumber})}
>
</Input>
</TableCell>
</TableRow>
)
}import { useFieldContext } from "@/hooks/form";
import type { HeldTalent } from "@/schemas/held.schema";
import { TableCell, TableRow } from "./ui/table";
import { talentByName } from "@/lib/talente";
import { Input } from "./ui/input";
export default function TalentRow() {
const field = useFieldContext<HeldTalent>();
const talent = talentByName(field.state.value.talentName);
if(!talent) return;
return (
<TableRow>
<TableCell>{talent.name}</TableCell>
<TableCell>
<Input
type="number"
id={field.name}
value={field.state.value.TaW}
onChange={(e) => field.handleChange({...field.state.value, TaW: e.target.valueAsNumber})}
>
</Input>
</TableCell>
</TableRow>
)
}And this is the Type definition:
export const HeldTalentSchema = z.object({
talentName: z.string(),
TaW: z.number().int(),
spEXP: z.number().int()
});
export type HeldTalent = z.infer<typeof HeldTalentSchema>;export const HeldTalentSchema = z.object({
talentName: z.string(),
TaW: z.number().int(),
spEXP: z.number().int()
});
export type HeldTalent = z.infer<typeof HeldTalentSchema>;