Skip to content

Commit

Permalink
Add shouldFocus option to setValue method
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Nov 8, 2022
1 parent 34c3c2b commit ec09706
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## vX.X.X (Month DD, YYYY)

- Add `shouldFocus` option to `setValue` method

## v0.7.0 (November 08, 2022)

- Add `validateIfNecessary` util and refactor code
Expand Down
4 changes: 3 additions & 1 deletion packages/solid/src/methods/setValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ValueOptions = Partial<{
shouldTouched: boolean;
shouldDirty: boolean;
shouldValidate: boolean;
shouldFocus: boolean;
}>;

/**
Expand All @@ -30,6 +31,7 @@ export function setValue<
shouldTouched = true,
shouldDirty = true,
shouldValidate = true,
shouldFocus = true,
} = options;

// Ignores tracking of reactive dependencies
Expand Down Expand Up @@ -57,7 +59,7 @@ export function setValue<
if (shouldValidate) {
validateIfNecessary(form, name, {
on: 'input',
shouldFocus: true,
shouldFocus,
});
}
});
Expand Down
7 changes: 7 additions & 0 deletions packages/website/src/routes/api/setValue.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ setValue(form, name, value, options);
- `shouldTouched` <Property {...properties.shouldTouched} />
- `shouldDirty` <Property {...properties.shouldDirty} />
- `shouldValidate` <Property {...properties.shouldValidate} />
- `shouldFocus` <Property {...properties.shouldFocus} />

### Explanation

By default, the field is set to touched and the dirty state is updated. If you don't want this, you can set `shouldTouched` and/or `shouldDirty` to `false`.

By default, the field is validated according to the rules defined in the `options` of <A href="/api/createForm">`createForm`</A>. To disable validation you can set `shouldValidate` to `false`.

By default, the field is focused when an error occurs during validation. To change this behavior you can set `shouldFocus` to `false`.

## Return

- `value` <Property {...properties.value} />
Expand Down Expand Up @@ -65,4 +68,8 @@ export const properties = {
type: 'boolean',
defaultValue: { type: 'boolean', value: true },
},
shouldFocus: {
type: 'boolean',
defaultValue: { type: 'boolean', value: true },
},
};

0 comments on commit ec09706

Please sign in to comment.