Skip to content

Commit 5ddd638

Browse files
committed
Reset response at handleSubmit and reset method
1 parent 07efbfd commit 5ddd638

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

packages/solid/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to the library will be documented in this file.
44

5+
## vX.X.X (Month DD, YYYY)
6+
7+
- Reset form response at `handleSubmit` and `reset` method
8+
- Add `keepResponse` property to `Form` component
9+
- Add `keepResponse` option to `handleSubmit` and `reset` method
10+
- Refactor `reset` method
11+
512
## v0.4.0 (November 02, 2022)
613

714
- Add `clearResponse`, `hasField` and `hasFieldArray` method

packages/solid/src/components/Form.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type FormProps<TFieldValues extends FieldValues> = Omit<
88
> & {
99
of: FormState<TFieldValues>;
1010
onSubmit: (values: TFieldValues, event: Event) => void | Promise<void>;
11+
keepResponse?: boolean;
1112
shouldActive?: boolean;
1213
shouldTouched?: boolean;
1314
shouldDirty?: boolean;
@@ -26,7 +27,13 @@ export function Form<TFieldValues extends FieldValues>(
2627
const [local, options, others] = splitProps(
2728
props,
2829
['of', 'onSubmit', 'children'],
29-
['shouldActive', 'shouldTouched', 'shouldDirty', 'shouldFocus']
30+
[
31+
'keepResponse',
32+
'shouldActive',
33+
'shouldTouched',
34+
'shouldDirty',
35+
'shouldFocus',
36+
]
3037
);
3138

3239
// Return HTML form element and include handleSubmit in onSubmit

packages/solid/src/methods/handleSubmit.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getValues } from './getValues';
44
import { validate } from './validate';
55

66
type SubmitOptions = Partial<{
7+
keepResponse: boolean;
78
shouldActive: boolean;
89
shouldTouched: boolean;
910
shouldDirty: boolean;
@@ -29,14 +30,20 @@ export function handleSubmit<TFieldValues extends FieldValues>(
2930

3031
// Destructure options and set default values
3132
const {
33+
keepResponse = false,
3234
shouldActive = true,
3335
shouldTouched = false,
3436
shouldDirty = false,
3537
shouldFocus = true,
3638
} = options;
3739

38-
// Increase submit count and set submitted and submitting to "true"
3940
batch(() => {
41+
// Reset response if it is not to be kept
42+
if (!keepResponse) {
43+
form.internal.setResponse({});
44+
}
45+
46+
// Increase submit count and set submitted and submitting to "true"
4047
form.internal.setSubmitCount((submitCount) => submitCount + 1);
4148
form.internal.setSubmitted(true);
4249
form.internal.setSubmitting(true);

packages/solid/src/methods/reset.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ResetOptions<
2424
> = Partial<{
2525
initialValue: [FieldPathValue<TFieldValues, TFieldName>];
2626
initialValues: DeepPartial<TFieldValues>;
27+
keepResponse: boolean;
2728
keepSubmitCount: boolean;
2829
keepSubmitted: boolean;
2930
keepValues: boolean;
@@ -68,8 +69,9 @@ export function reset<
6869
const {
6970
initialValue,
7071
initialValues,
71-
keepSubmitCount = resetSingleField,
72-
keepSubmitted = resetSingleField,
72+
keepResponse = false,
73+
keepSubmitCount = false,
74+
keepSubmitted = false,
7375
keepValues = false,
7476
keepDirtyValues = false,
7577
keepErrors = false,
@@ -165,6 +167,11 @@ export function reset<
165167

166168
// Reset form state if necessary
167169
if (resetEntireForm) {
170+
// Reset response if it is not to be kept
171+
if (!keepResponse) {
172+
form.internal.setResponse({});
173+
}
174+
168175
// Reset form if values should not be kept
169176
if (!keepValues) {
170177
form.element?.reset();

packages/website/src/routes/api/Form.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ HTML form element that simplifies form submission and disables the browser's def
1515
<Form
1616
of={}
1717
onSubmit={}
18+
keepResponse={}
1819
shouldActive={}
1920
shouldTouched={}
2021
shouldDirty={}
@@ -28,6 +29,7 @@ HTML form element that simplifies form submission and disables the browser's def
2829

2930
- `of` <Property {...properties.of} />
3031
- `onSubmit` <Property {...properties.onSubmit} />
32+
- `keepResponse` <Property {...properties.keepResponse} />
3133
- `shouldActive` <Property {...properties.shouldActive} />
3234
- `shouldTouched` <Property {...properties.shouldTouched} />
3335
- `shouldDirty` <Property {...properties.shouldDirty} />
@@ -38,6 +40,8 @@ HTML form element that simplifies form submission and disables the browser's def
3840
3941
### Explanation
4042

43+
Before executing the `onSubmit` function, the `response` of the form is reset. To change this behavior you can set the `keepResponse` property to `true`.
44+
4145
Errors thrown by the `onSubmit` function are made available via the `response` property of the <A href="/api/ModularForm">`ModularForm`</A> object to display them to your user.
4246

4347
By default, the `onSubmit` function validates and provides only the values of active fields via the `values` parameter and focuses on the first field with an error during validation if one occurs. To change this behavior you can set the `shouldActive` and `shouldFocus` property to `false`.
@@ -71,6 +75,10 @@ export const properties = {
7175
return: ['void', { type: 'custom', name: 'Promise', generics: ['void'] }],
7276
},
7377
},
78+
keepResponse: {
79+
type: 'boolean',
80+
defaultValue: { type: 'boolean', value: false },
81+
},
7482
shouldActive: {
7583
type: 'boolean',
7684
defaultValue: { type: 'boolean', value: true },

packages/website/src/routes/api/handleSubmit.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ It is recommended to execute the `handleSubmit` method directly in the `onSubmit
2727
- `form` <Property {...properties.form} />
2828
- `submitAction` <Property {...properties.submitAction} />
2929
- `options` <Property {...properties.options} />
30+
- `keepResponse` <Property {...properties.keepResponse} />
3031
- `shouldActive` <Property {...properties.shouldActive} />
3132
- `shouldTouched` <Property {...properties.shouldTouched} />
3233
- `shouldDirty` <Property {...properties.shouldDirty} />
3334
- `shouldFocus` <Property {...properties.shouldFocus} />
3435

3536
### Explanation
3637

38+
Before executing the `submitAction` function, the `response` of the `form` is reset. To change this behavior you can set the `keepResponse` property to `true`.
39+
3740
Errors thrown by the `submitAction` function are made available via the `response` property of the <A href="/api/ModularForm">`ModularForm`</A> object to display them to your user.
3841

3942
By default, the `submitAction` function provides only the values of active fields via the `values` parameter. To change this behavior you can set the `shouldActive` property to `false`.
@@ -77,6 +80,10 @@ export const properties = {
7780
type: 'object',
7881
defaultValue: { type: 'object', entries: [] },
7982
},
83+
keepResponse: {
84+
type: 'boolean',
85+
defaultValue: { type: 'boolean', value: false },
86+
},
8087
shouldActive: {
8188
type: 'boolean',
8289
defaultValue: { type: 'boolean', value: true },

packages/website/src/routes/api/reset.mdx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ reset(form, name, options);
3030
- `options` <Property {...properties.options} />
3131
- `initialValues` <Property {...properties.initialValues} />
3232
- `initialValue` <Property {...properties.initialValue} />
33+
- `keepResponse` <Property {...properties.keepResponse} />
3334
- `keepSubmitCount` <Property {...properties.keepSubmitCount} />
3435
- `keepSubmitted` <Property {...properties.keepSubmitted} />
3536
- `keepValues` <Property {...properties.keepValues} />
@@ -48,9 +49,9 @@ When you reset several fields, you can specify `initialValues` that get shallow
4849

4950
When you reset a single field, you can specify a new `initialValue` that override the initial value you may set in the `initialValues` property of the <A href="/api/createForm">`createForm`</A> primitive.
5051

51-
By default, the reactive `submitCount` and `submitted` state of the `form` is reset when the entire form is reset. To change this behavior you can set `keepSubmitCount` and/or `keepSubmitted` to `true` or `false`.
52+
By default, the reactive `response`, `submitCount` and `submitted` state of the `form` is reset when the entire form is reset. To change this behavior you can set `keepResponse`, `keepSubmitCount` and/or `keepSubmitted` to `true`.
5253

53-
By default, the value, error, touched and dirty state of each field and field array are reset. To change this behavior you can set `keepValues`, `keepDirtyValues`, `keepErrors`, `keepErrors` and/or `keepDirty` to `true`.
54+
By default, the `value`, `error`, `touched` and `dirty` state of each field and field array are reset. To change this behavior you can set `keepValues`, `keepDirtyValues`, `keepErrors`, `keepErrors` and/or `keepDirty` to `true`.
5455

5556
export const properties = {
5657
form: {
@@ -96,19 +97,17 @@ export const properties = {
9697
'undefined',
9798
],
9899
},
100+
keepResponse: {
101+
type: 'boolean',
102+
defaultValue: { type: 'boolean', value: false },
103+
},
99104
keepSubmitCount: {
100105
type: 'boolean',
101-
defaultValue: [
102-
{ type: 'boolean', value: true },
103-
{ type: 'boolean', value: false },
104-
],
106+
defaultValue: [{ type: 'boolean', value: false }],
105107
},
106108
keepSubmitted: {
107109
type: 'boolean',
108-
defaultValue: [
109-
{ type: 'boolean', value: true },
110-
{ type: 'boolean', value: false },
111-
],
110+
defaultValue: [{ type: 'boolean', value: false }],
112111
},
113112
keepValues: {
114113
type: 'boolean',

0 commit comments

Comments
 (0)