Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset registered input value on key changes #674

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-spies-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@conform-to/react": patch
---

fix: reset registered input value on key changes
24 changes: 21 additions & 3 deletions packages/conform-react/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand Down Expand Up @@ -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,
Expand Down
Loading