Skip to content

Commit

Permalink
type(validator.tool): Update reset/getValues types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 30, 2021
1 parent 86191f1 commit 986583d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 152 deletions.
63 changes: 61 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,64 @@ $ npm install validator.tool --save
$ npm install @validator.tool/hook --save
```

```jsx
import Validator from 'validator.tool';

function Example() {
const [data, setData] = useState({
email: '[email protected]'
});
const validator = useRef(
new Validator({
initValues: { ...data },
})
);
return (
<form>
<div>
<label htmlFor="email">EMail:</label>
<input type="email" name="email" defaultValue={data.email} />
<span>
{validator.current.message('email', data.email, {
validate: (val) => !/^[A-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(val) ? `The ${val} must be a valid email address.` : ''
})}
</span>
</div>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>
)
}
```

```jsx
import { useValidator } from '@validator.tool/hook';

function Example() {
const [data, setData] = useState({
email: '[email protected]'
});
const validator = useValidator({
initValues: { ...data },
});
return (
<form onSubmit={...} onReset={...} onChange={...} onBlur={...}>
<div>
<label htmlFor="email">EMail:</label>
<input type="email" name="email" defaultValue={data.email} />
<span>
{validator.message('email', data.email, {
validate: (val) => !/^[A-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(val) ? `The ${val} must be a valid email address.` : ''
})}
</span>
</div>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>
);
}
```

## Usage

```js
Expand Down Expand Up @@ -226,6 +284,7 @@ export interface RulesOption {
export declare type ValidatorOption = {
messagesShown?: boolean;
rules?: Rules;
initValues?: Values;
form?: HTMLFormElement | null;
validate?: RulesOption['validate'];
};
Expand All @@ -246,8 +305,8 @@ export default class Validator {
/** How you define validation rules and add messages into the form. */
message: (field: string, inputValue?: Value | undefined, options?: RulesOption | undefined) => string | undefined;
setValues: (values?: Values) => void;
getValues: () => Partial<Record<string, Value>>;
reset: () => Partial<Record<string, Value>> | undefined;
getValues: () => Values;
reset: () => Values;
fieldValid: (field: string) => boolean;
/**
* Returns a boolean if all the fields pass validation or not.
Expand Down
6 changes: 2 additions & 4 deletions packages/core/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export default class Validator {
/** How you define validation rules and add messages into the form. */
message: (field: string, inputValue?: Value | undefined, options?: RulesOption | undefined) => string | undefined;
setValues: (values?: Values) => void;
getValues: () => Partial<Record<string, Value>>;
reset: () => {
[x: string]: Value | undefined;
};
getValues: () => Values;
reset: () => Values;
fieldValid: (field: string) => boolean;
/**
* Returns a boolean if all the fields pass validation or not.
Expand Down
146 changes: 3 additions & 143 deletions packages/core/dist/validator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 986583d

Please sign in to comment.