Skip to content

Commit

Permalink
added maskOnBlur so users can trigger mask function only on blur
Browse files Browse the repository at this point in the history
  • Loading branch information
joepuzzo committed Jan 10, 2023
1 parent f73f7e2 commit ae605a3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.39.0 (January 10th, 2023)

### Added

- maskOnBlur so users can trigger mask function only on blur

## 4.38.2 (January 9th, 2023)

### Fixed
Expand Down
21 changes: 19 additions & 2 deletions src/FormController.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export class FormController {
let maskedVal = value;

// call mask if passed
if (meta.mask) {
if (meta.mask && !meta.maskOnBlur) {
maskedVal = meta.mask(val);
}

Expand Down Expand Up @@ -356,7 +356,7 @@ export class FormController {
}

// call mask if passed
if (meta.mask) {
if (meta.mask && !meta.maskOnBlur) {
val = meta.mask(val);
maskedVal = val;
}
Expand Down Expand Up @@ -546,6 +546,23 @@ export class FormController {
// Update the state
ObjectMap.set(this.state.touched, name, value);

// Update value if maskOnBlur and we have mask
if (meta.mask && meta.maskOnBlur) {
let val = ObjectMap.get(this.state.values, name);
let maskedVal = val;
maskedVal = meta.mask(val);

// // Only parse if parser was passed
if (meta.parser) {
val = val != null ? informedParse(val, meta.parser) : val;
}

debug(`Setting ${name}'s value to`, maskedVal);
ObjectMap.set(this.state.values, name, maskedVal);
debug(`Setting ${name}'s maskedValue to`, maskedVal);
ObjectMap.set(this.state.maskedValues, name, maskedVal);
}

// We only need to call validate if the user gave us one
// and they want us to validate on blur
// Example validateOn = "change" ("change-change")==> true
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useField.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const useField = ({
gatherOnMount,
validateOnMount: userValidateOnMount,
validateOn: userValidateOn,
maskOnBlur,
validateWhen = [],
formatterDependencies = [],
formController: userFormController,
Expand Down Expand Up @@ -210,6 +211,7 @@ export const useField = ({
showErrorIfError,
showErrorIfTouched,
showErrorIfDirty,
maskOnBlur,
asyncValidate,
gatherData,
initialize,
Expand Down
6 changes: 6 additions & 0 deletions stories/Formatting/Mask/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const parser = value => value.toLowerCase();
mask={mask}
parser={parser}
/>
<Input
name="field3"
label="Field 3 ( masks on blur )"
mask={mask}
maskOnBlur
/>
<button type="submit">Submit</button>
</Form>;
```
6 changes: 6 additions & 0 deletions stories/Formatting/Mask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const Mask = () => (
mask={mask}
parser={parser}
/>
<Input
name="field3"
label="Field 3 ( masks on blur )"
mask={mask}
maskOnBlur
/>
<button type="submit">Submit</button>
<Debug values maskedValues />
</Form>
Expand Down
1 change: 1 addition & 0 deletions stories/Inputs/Intro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Below are all the input props that `informed`'s inputs accept.
| validate | func | NO | Function that gets called when form performs validation. Function accepts the value as a parameter and must return either an error or undefined. By default it only gets called onSubmit. See Validation section for additional details. |
| validateOn | bool | NO | Tells field when to perform validation. By default it only validates onBlur. |
| validateOnMount | bool | NO | Tells field to perform validation onMount. | |
| maskOnBlur | bool | NO | Tells field to perform masking on blur instead of on change. | |
| keep | object | NO | Keeps specified field state around even when the input itself is unmounted keep={{ value: true }} |
| keepState | bool | NO | Keeps the field state around even when the input itself is unmounted ( see dynamic form docs for example ) |
| keepStateIfRelevant | bool | NO | Keeps the field state around even when the input itself is not mounted ( only if its also relevant ) |
Expand Down

0 comments on commit ae605a3

Please sign in to comment.