attempt 57 at getting edit form fields to prefill with existing data because i don't know what else to write anymore
This commit is contained in:
parent
0e66f2323f
commit
1f93dd1595
1 changed files with 26 additions and 0 deletions
|
|
@ -490,6 +490,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
|||
handleSubmit,
|
||||
control,
|
||||
reset,
|
||||
setValue, // ← added
|
||||
getValues, // ← added
|
||||
formState: { isSubmitting },
|
||||
} = useForm<any>({
|
||||
defaultValues: {
|
||||
|
|
@ -541,6 +543,30 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
|||
}
|
||||
}, [isEdit, edit?.initialValues, reset]);
|
||||
|
||||
// After reset, force RHF values for selects so the browser matches options when they hydrate
|
||||
useEffect(() => {
|
||||
if (!isEdit || !current) return;
|
||||
|
||||
const fieldNames = [
|
||||
"laser_soft",
|
||||
"mat",
|
||||
"mat_coat",
|
||||
"mat_color",
|
||||
"mat_opacity",
|
||||
"source",
|
||||
"lens",
|
||||
] as const;
|
||||
|
||||
const values = getValues();
|
||||
fieldNames.forEach((name) => {
|
||||
const cur = (current as any)[name];
|
||||
const now = (values as any)[name];
|
||||
if (cur && (now == null || now === "")) {
|
||||
setValue(name as any, cur, { shouldDirty: false, shouldValidate: false });
|
||||
}
|
||||
});
|
||||
}, [isEdit, current, getValues, setValue]);
|
||||
|
||||
function num(v: any) {
|
||||
return v === "" || v == null ? null : Number(v);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue