Skip to content

Commit

Permalink
fix(conform-react): stop updating value of input buttons (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung authored Sep 14, 2024
1 parent dd62330 commit f0a7f8e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
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

0 comments on commit f0a7f8e

Please sign in to comment.