Skip to content

Commit 3147355

Browse files
committed
Add keepItems and keepDirtyItems to reset method
1 parent 5ddd638 commit 3147355

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

packages/solid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to the library will be documented in this file.
88
- Add `keepResponse` property to `Form` component
99
- Add `keepResponse` option to `handleSubmit` and `reset` method
1010
- Refactor `reset` method
11+
- Add `keepItems` and `keepDirtyItems` option to `reset` method
1112

1213
## v0.4.0 (November 02, 2022)
1314

packages/solid/src/methods/reset.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type ResetOptions<
2929
keepSubmitted: boolean;
3030
keepValues: boolean;
3131
keepDirtyValues: boolean;
32+
keepItems: boolean;
33+
keepDirtyItems: boolean;
3234
keepErrors: boolean;
3335
keepTouched: boolean;
3436
keepDirty: boolean;
@@ -74,6 +76,8 @@ export function reset<
7476
keepSubmitted = false,
7577
keepValues = false,
7678
keepDirtyValues = false,
79+
keepItems = false,
80+
keepDirtyItems = false,
7781
keepErrors = false,
7882
keepTouched = false,
7983
keepDirty = false,
@@ -139,11 +143,11 @@ export function reset<
139143
// Get specified field array
140144
const fieldArray = getFieldArray(form, name);
141145

142-
// Check if dirty value should be kept
143-
const keepDirtyValue = keepDirtyValues && fieldArray.getDirty();
146+
// Check if current dirty items should be kept
147+
const keepCurrentDirtyItems = keepDirtyItems && fieldArray.getDirty();
144148

145149
// Reset initial items and items if it is not to be kept
146-
if (!keepValues && !keepDirtyValue) {
150+
if (!keepItems && !keepCurrentDirtyItems) {
147151
const initialItems = getInitialItems(form, name);
148152
fieldArray.setInitialItems(initialItems);
149153
fieldArray.setItems(initialItems);
@@ -155,7 +159,7 @@ export function reset<
155159
}
156160

157161
// Reset dirty if it is not to be kept
158-
if (!keepDirty && !keepValues && !keepDirtyValue) {
162+
if (!keepDirty && !keepItems && !keepCurrentDirtyItems) {
159163
fieldArray.setDirty(false);
160164
}
161165

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ reset(form, name, options);
3535
- `keepSubmitted` <Property {...properties.keepSubmitted} />
3636
- `keepValues` <Property {...properties.keepValues} />
3737
- `keepDirtyValues` <Property {...properties.keepDirtyValues} />
38+
- `keepItems` <Property {...properties.keepItems} />
39+
- `keepDirtyItems` <Property {...properties.keepDirtyItems} />
3840
- `keepErrors` <Property {...properties.keepErrors} />
3941
- `keepTouched` <Property {...properties.keepTouched} />
4042
- `keepDirty` <Property {...properties.keepDirty} />
@@ -51,7 +53,7 @@ When you reset a single field, you can specify a new `initialValue` that overrid
5153

5254
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`.
5355

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`.
56+
By default, the `value`, `items`, `error`, `touched` and `dirty` state of each field and field array are reset. To change this behavior you can set `keepValues`, `keepDirtyValues`, `keepItems`, `keepDirtyItems`, `keepErrors`, `keepTouched` and/or `keepDirty` to `true`.
5557

5658
export const properties = {
5759
form: {
@@ -117,6 +119,14 @@ export const properties = {
117119
type: 'boolean',
118120
defaultValue: { type: 'boolean', value: false },
119121
},
122+
keepItems: {
123+
type: 'boolean',
124+
defaultValue: { type: 'boolean', value: false },
125+
},
126+
keepDirtyItems: {
127+
type: 'boolean',
128+
defaultValue: { type: 'boolean', value: false },
129+
},
120130
keepErrors: {
121131
type: 'boolean',
122132
defaultValue: { type: 'boolean', value: false },

0 commit comments

Comments
 (0)