Skip to content

Commit

Permalink
Add keepItems and keepDirtyItems to reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Nov 6, 2022
1 parent 5ddd638 commit 3147355
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to the library will be documented in this file.
- Add `keepResponse` property to `Form` component
- Add `keepResponse` option to `handleSubmit` and `reset` method
- Refactor `reset` method
- Add `keepItems` and `keepDirtyItems` option to `reset` method

## v0.4.0 (November 02, 2022)

Expand Down
12 changes: 8 additions & 4 deletions packages/solid/src/methods/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type ResetOptions<
keepSubmitted: boolean;
keepValues: boolean;
keepDirtyValues: boolean;
keepItems: boolean;
keepDirtyItems: boolean;
keepErrors: boolean;
keepTouched: boolean;
keepDirty: boolean;
Expand Down Expand Up @@ -74,6 +76,8 @@ export function reset<
keepSubmitted = false,
keepValues = false,
keepDirtyValues = false,
keepItems = false,
keepDirtyItems = false,
keepErrors = false,
keepTouched = false,
keepDirty = false,
Expand Down Expand Up @@ -139,11 +143,11 @@ export function reset<
// Get specified field array
const fieldArray = getFieldArray(form, name);

// Check if dirty value should be kept
const keepDirtyValue = keepDirtyValues && fieldArray.getDirty();
// Check if current dirty items should be kept
const keepCurrentDirtyItems = keepDirtyItems && fieldArray.getDirty();

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

// Reset dirty if it is not to be kept
if (!keepDirty && !keepValues && !keepDirtyValue) {
if (!keepDirty && !keepItems && !keepCurrentDirtyItems) {
fieldArray.setDirty(false);
}

Expand Down
12 changes: 11 additions & 1 deletion packages/website/src/routes/api/reset.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ reset(form, name, options);
- `keepSubmitted` <Property {...properties.keepSubmitted} />
- `keepValues` <Property {...properties.keepValues} />
- `keepDirtyValues` <Property {...properties.keepDirtyValues} />
- `keepItems` <Property {...properties.keepItems} />
- `keepDirtyItems` <Property {...properties.keepDirtyItems} />
- `keepErrors` <Property {...properties.keepErrors} />
- `keepTouched` <Property {...properties.keepTouched} />
- `keepDirty` <Property {...properties.keepDirty} />
Expand All @@ -51,7 +53,7 @@ When you reset a single field, you can specify a new `initialValue` that overrid

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

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

export const properties = {
form: {
Expand Down Expand Up @@ -117,6 +119,14 @@ export const properties = {
type: 'boolean',
defaultValue: { type: 'boolean', value: false },
},
keepItems: {
type: 'boolean',
defaultValue: { type: 'boolean', value: false },
},
keepDirtyItems: {
type: 'boolean',
defaultValue: { type: 'boolean', value: false },
},
keepErrors: {
type: 'boolean',
defaultValue: { type: 'boolean', value: false },
Expand Down

0 comments on commit 3147355

Please sign in to comment.