# mg-react-validator

> - 一块应用在 React 上的轻量级表单验证器(高阶组件)

Latest version **1.0.5** (published 2019-12-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install mg-react-validator
pnpm add mg-react-validator
yarn add mg-react-validator
bun add mg-react-validator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2019-12-25 |
| First published | 2019-12-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xiaomage |
| Maintainers | mawenxing |
| Keywords | react, mg, validator, withValidator, mg-react-validator |

## Links

- npm: https://www.npmjs.com/package/mg-react-validator
- npm.io page: https://npm.io/package/mg-react-validator

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2019-12-25
- 1.0.4 — 2019-12-25
- 1.0.3 — 2019-12-25
- 1.0.2 — 2019-12-25
- 1.0.1 — 2019-12-25
- 1.0.0 — 2019-12-25

## README

# mg-react-validator
- 一块应用在 React 上的轻量级表单验证器(高阶组件)

# Demo & github 地址
- 

# Install
```js
yarn add mg-react-validator
```

# Usage
- 第一步 : 引入高阶组件
```js
import  withValidator  from 'mg-react-validator'
```
- 第二步 : 高阶组件包装
```js
Child1 = withValidator({
  // 把当前的数据映射到 props 里面的 values 供表单使用
  mgMapStateToValues: { username: '', password: '' },
  // 点击提交表单
  mgHandleSubmit: (values, ownProps) => {
    console.log('点击了 handleSubmit')
  },
  // 校验规则
  mgValidate: values => {
    const errors = {}

    if (!values.username) {
      errors.username = 'Required'
    } else if (!/^[A-Z0-9]{3,6}$/i.test(values.username)) {
      errors.username = '用户名 格式错误'
    }
    if (!values.password) {
      errors.password = 'Required'
    } else if (!/^[A-Z0-9]{3,6}$/i.test(values.password)) {
      errors.password = '密码 格式错误'
    }

    return errors
  }
})(Child1)
```

- 第三步 : 从 props 引入一些数据
```js
    const {
      // 获取数据
      mgValues: { username, password },
      // 处理表单
      mgHandleChange,
      // 提交表单
      mgHandleSubmit,
      // 错误信息
      mgErrors,
      // 判断是否有效
      mgIsValid,
      // 失去焦点
      mgHandleBlur,
      // 点击过的表单元素
      mgTouched
    } = this.props
```

- 第四步 : 与表单绑定起来
```js
// 注意 : name 必须要有 
       <div>
          用户名 :
          <input
            type="text"
            name="username"
            onChange={mgHandleChange}
            value={username}
            onBlur={mgHandleBlur}
          />
          // 错误提示
          {mgTouched.username && mgErrors.username && (
            <span>{mgErrors.username}</span>
          )}
        </div>
        <div>
          密码 :
          <input
            type="text"
            name="password"
            onChange={mgHandleChange}
            value={password}
            onBlur={mgHandleBlur}
          />
          // 错误提示
          {mgTouched.password && mgErrors.password && (
            <span>{mgErrors.password}</span>
          )}
        </div>
        // 点击提交
        <button onClick={mgHandleSubmit}>按钮</button>
```

# Note
- 本高阶组件是一个轻量级的
- 由于时间仓促, 可能会有bug
- 欢迎提出指正修改意见,感谢!!

---
_Source: https://npm.io/package/mg-react-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
