# @uiw/react-pin-code

> PinCode component

Latest version **4.22.3** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @uiw/react-pin-code
pnpm add @uiw/react-pin-code
yarn add @uiw/react-pin-code
bun add @uiw/react-pin-code
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.22.3 |
| Published | 2023-11-29 |
| First published | 2020-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 739 |
| Author | Kenny Wong |
| Maintainers | uiwjs, wcjiang |
| Keywords | pin-code, react-pin-code, design, uiw, uiw-react, react.js, react, react-component, component, components, ui, css, uikit, react-ui, framework, front-end, frontend |

## Links

- npm: https://www.npmjs.com/package/@uiw/react-pin-code
- Repository: https://github.com/uiwjs/uiw
- Homepage: https://uiwjs.github.io/#/components/pin-code
- Issues: https://github.com/uiwjs/uiw/issues
- Funding: https://jaywcjlove.github.io/#/sponsor
- npm.io page: https://npm.io/package/@uiw/react-pin-code

## Dependencies (2)

- [@uiw/utils](https://npm.io/package/@uiw/utils.md) ^4.22.3
- [@uiw/react-input](https://npm.io/package/@uiw/react-input.md) ^4.22.3

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@progress/kendo-vue-listbox](https://npm.io/package/@progress/kendo-vue-listbox.md) — 932 weekly downloads

## Recent versions

- 4.22.3 (latest) — 2023-11-29
- 5.0.0-bate.2.1 (bate) — 2023-03-10
- 4.22.2 — 2023-11-09
- 4.22.1 — 2023-11-08
- 4.22.0 — 2023-09-19
- 4.21.28 — 2023-05-05
- 4.21.27 — 2023-04-21
- 4.21.26 — 2023-04-20
- 4.21.25 — 2023-03-14
- 4.21.24 — 2023-03-08
- 5.0.0-bate.2.0 — 2023-03-02
- 4.21.23 — 2022-12-21
- 4.21.22 — 2022-12-19
- 4.21.21 — 2022-12-19
- 5.0.0-bate-19 — 2022-12-12
- … 136 more at https://npm.io/package/@uiw/react-pin-code/versions

## README

PinCode 安全码
===

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-pin-code/file/README.md)
[![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-pin-code.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-pin-code)
[![npm version](https://img.shields.io/npm/v/@uiw/react-pin-code.svg?label=@uiw/react-pin-code)](https://npmjs.com/@uiw/react-pin-code)

针对输入数字序列进行了优化。最常见的应用是输入一次性安全码。它经过优化，可快速输入数字。

```jsx
import { PinCode } from 'uiw';
// or
import PinCode from '@uiw/react-pin-code';
```

### 基础用法

```jsx mdx:preview&bg=#fff
import React from 'react';
import { PinCode } from 'uiw';

export default function Demo() {
  return (
    <div>
      <PinCode autoFocus value={['', '', '', '', '']} onChange={(val) => console.log(val)} />
    </div>
  );
}
```

### 在表单中使用

在 [`<Form />`](#/components/form) 表单中应用 [`<PinCode />`](#/components/pin-code) 组件

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Form, Row, Col, Icon, PinCode, Button, Notify } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Form
        resetOnSubmit={false}
        onSubmitError={(error) => {
          if (error.filed) {
            return { ...error.filed };
          }
          return null;
        }}
        onSubmit={({initial, current}) => {
          const errorObj = {};
          if (current.pinCode.join('').length === 0) {
            errorObj.pinCode = '请输入安全码！';
          }
          if (current.pinCode.join('').length < 5) {
            errorObj.pinCode = '安全码没有输入完整！';
          }
          if(Object.keys(errorObj).length > 0) {
            const err = new Error();
            err.filed = errorObj;
            Notify.error({ title: '提交失败！', description: '请确认提交表单是否正确！' });
            throw err;
          }
          Notify.success({
            title: '提交成功！',
            description: `表单提交成功，安全码为：${current.pinCode.join('')}！`,
          });
        }}
        fields={{
          pinCode: {
            initialValue: ['', '', '', '', ''],
            children: <PinCode />
          },
        }}
      >
        {({ fields, state, canSubmit }) => {
          console.log('state:',state.current.pinCode.join(''))
          return (
            <div>
              <Row>
                <Col fixed>{fields.pinCode}</Col>
              </Row>
              <Row>
                <Col fixed>
                  <Button size="small" disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
                  <span style={{ paddingLeft: 16, color: '#a7a7a7', verticalAlign: 'middle' }}>
                    {state.current.pinCode.join('')}
                  </span>
                </Col>
              </Row>
              <Row>
                <Col>
                  <pre style={{ padding: 10, marginTop: 10 }}>
                    {JSON.stringify(state.current, null, 2)}
                  </pre>
                </Col>
              </Row>
            </div>
          )
        }}
      </Form>
    </div>
  )
}
```

### 更改占位符

```jsx mdx:preview&bg=#fff
import React from 'react';
import { PinCode, Divider } from 'uiw';

export default function Demo() {
  return (
    <div>
      <PinCode placeholder="■" value={['8', '7', '3', '', '']} onChange={(val) => console.log(val)} />
      <Divider />
      <PinCode placeholder="★" value={['6', '', '', '', '']} onChange={(val) => console.log(val)} />
      <Divider />
      <PinCode placeholder="🤣" value={['', '', '', '', '']} onChange={(val) => console.log(val)} />
    </div>
  );
}
```

### 输入框被禁用

```jsx mdx:preview&bg=#fff
import React from 'react';
import { PinCode, Divider } from 'uiw';

export default function Demo() {
  return (
    <div>
      <PinCode disabled value={['1', '9', '8', '7', '0']} />
      <Divider />
      <PinCode disabled value={['', '', '', '', '']} />
      <Divider />
      <PinCode disabled placeholder="🤣" value={['', '', '', '', '']} />
    </div>
  );
}
```

### 尺寸大小

```jsx mdx:preview&bg=#fff
import React from 'react';
import { PinCode, Divider } from 'uiw';

export default function Demo() {
  return (
    <div>
      <PinCode size="large" value={['1', '9', '8', '7', '0']} />
      <Divider />
      <PinCode value={['', '', '', '', '']} />
      <Divider />
      <PinCode size="small" value={['', '', '', '', '']} />
    </div>
  );
}
```

## PinCode

| 参数 | 说明 | 类型 | 默认值 |
|--------- |-------- |--------- |-------- |
| value | 绑定值，根据数组长度，展示安全码的长度 | string[] | - |
| disabled | 禁用输入框 | Boolean | `false` |
| autoFocus | 自动获得焦点。 | Boolean | - |
| placeholder | 占位符 | string | `○` |
| size | 指定输入框的尺寸，除了默认的大小外，还提供了 `large`、`small` 和 `default` 三种尺寸。 | String | - |
| onChange | `value` 变化时回调函数 | Function(value: string[]) | - |
| onBlur | 失去焦点时运行 | Function(event: React.FocusEvent<HTMLInputElement\>) | - |
| onFocus | 获得焦点时运行 | Function(event: React.FocusEvent<HTMLInputElement\>) | - |

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