Socket
Socket
Sign inDemoInstall

@uiw/react-pin-code

Package Overview
Dependencies
9
Maintainers
2
Versions
151
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @uiw/react-pin-code

PinCode component


Version published
Weekly downloads
209
increased by54.81%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

PinCode 安全码

Buy me a coffee Open in unpkg NPM Downloads npm version

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

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

基础用法

import React from 'react';
import { PinCode } from 'uiw';

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

在表单中使用

<Form /> 表单中应用 <PinCode /> 组件

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>
  )
}

更改占位符

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>
  );
}

输入框被禁用

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>
  );
}

尺寸大小

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禁用输入框Booleanfalse
autoFocus自动获得焦点。Boolean-
placeholder占位符string
size指定输入框的尺寸,除了默认的大小外,还提供了 largesmalldefault 三种尺寸。String-
onChangevalue 变化时回调函数Function(value: string[])-
onBlur失去焦点时运行Function(event: React.FocusEvent<HTMLInputElement>)-
onFocus获得焦点时运行Function(event: React.FocusEvent<HTMLInputElement>)-

Keywords

FAQs

Last updated on 29 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc