diff --git a/.changeset/clean-spies-clap.md b/.changeset/clean-spies-clap.md new file mode 100644 index 00000000..5081164b --- /dev/null +++ b/.changeset/clean-spies-clap.md @@ -0,0 +1,5 @@ +--- +"@conform-to/react": patch +--- + +fix: reset registered input value on key changes diff --git a/packages/conform-react/integrations.ts b/packages/conform-react/integrations.ts index df326640..cb5c58b6 100644 --- a/packages/conform-react/integrations.ts +++ b/packages/conform-react/integrations.ts @@ -19,8 +19,8 @@ export function getFieldElements( const elements = !field ? [] : field instanceof Element - ? [field] - : Array.from(field.values()); + ? [field] + : Array.from(field.values()); return elements.filter( ( @@ -306,8 +306,26 @@ export function useControl< change(value); }; + const refCallback: RefCallback< + HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | undefined + > = (element) => { + register(element); + + if (!element) { + return; + } + + const prevKey = element.dataset.conform; + const nextKey = `${meta.key ?? ''}`; + + if (prevKey !== nextKey) { + element.dataset.conform = nextKey; + updateFieldValue(element, value ?? ''); + } + }; + return { - register, + register: refCallback, value, change: handleChange, focus,