Skip to content

Commit

Permalink
fix: reset registered input value on key changes
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Jun 11, 2024
1 parent 1ccd227 commit 8d09bf6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/conform-react/integrations.ts
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit 8d09bf6

Please sign in to comment.