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(conform-react): stop updating value of input buttons #766

Merged
merged 2 commits into from
Sep 14, 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/afraid-owls-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@conform-to/react': patch
---

fix(conform-react): stop updating value of input buttons
9 changes: 7 additions & 2 deletions packages/conform-react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ export function useForm<
const next = stateSnapshot.key[element.name];
const defaultValue = stateSnapshot.initialValue[element.name];

if (prev === 'managed') {
// Skip fields managed by useInputControl()
if (
prev === 'managed' ||
element.type === 'submit' ||
element.type === 'reset' ||
element.type === 'button'
) {
// Skip buttons and fields managed by useInputControl()
continue;
}

Expand Down
6 changes: 6 additions & 0 deletions playground/app/routes/form-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export default function FormControl() {
>
Reset form
</button>
<input
type="submit"
className="rounded-md border p-2 hover:border-black"
name="example"
value="Submit"
/>
</div>
</Playground>
</Form>
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/form-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function getFieldset(form: Locator) {
clearMessage: form.locator('button:text("Clear message")'),
resetMessage: form.locator('button:text("Reset message")'),
resetForm: form.locator('button:text("Reset form")'),
inputButton: form.locator('input[type="submit"]'),
};
}

async function runValidationScenario(page: Page) {
const playground = getPlayground(page);
const fieldset = getFieldset(playground.container);

// Conform should not overwrite the value of any input buttons
await expect(fieldset.inputButton).toHaveValue('Submit');

await expect(playground.error).toHaveText(['', '', '']);

await fieldset.validateMessage.click();
Expand Down
Loading