@tuya-oh/react-native-smooth-pincode-input
v1.0.9-rc-0.0.2
Published
A cross-platform, smooth, lightweight, customizable PIN code input component for React Native.
Readme
[!TIP] Github 地址
安装与使用
进入到工程目录并输入以下命令:
npm
npm install @tuya-oh/react-native-smooth-pincode-inputyarn
yarn add @tuya-oh/react-native-smooth-pincode-input下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import SmoothPinCodeInput from 'react-native-smooth-pincode-input';
export default class App extends React.Component {
state = {
code: '',
password: '',
};
pinInput = React.createRef();
_checkCode = (code) => {
if (code != '1234') {
this.pinInput.current.shake()
.then(() => this.setState({ code: '' }));
}
}
render() {
const { code, password } = this.state;
return (
<View style={styles.container}>
{/* default */}
<View style={styles.section}>
<Text style={styles.title}>Default</Text>
<SmoothPinCodeInput
ref={this.pinInput}
value={code}
onTextChange={code => this.setState({ code })}
onFulfill={this._checkCode}
onBackspace={() => console.log('No more back.')}
/>
</View>
{/* password */}
<View style={styles.section}>
<Text style={styles.title}>Password</Text>
<SmoothPinCodeInput password mask="﹡"
cellSize={36}
codeLength={8}
value={password}
onTextChange={password => this.setState({ password })}/>
</View>
{/* underline */}
<View style={styles.section}>
<Text style={styles.title}>Underline</Text>
<SmoothPinCodeInput
cellStyle={{
borderBottomWidth: 2,
borderColor: 'gray',
}}
cellStyleFocused={{
borderColor: 'black',
}}
value={code}
onTextChange={code => this.setState({ code })}
/>
</View>
{/* customized */}
<View style={styles.section}>
<Text style={styles.title}>Customized</Text>
<SmoothPinCodeInput
placeholder="⭑"
cellStyle={{
borderWidth: 2,
borderRadius: 24,
borderColor: 'orange',
backgroundColor: 'gold',
}}
cellStyleFocused={{
borderColor: 'darkorange',
backgroundColor: 'orange',
}}
textStyle={{
fontSize: 24,
color: 'salmon'
}}
textStyleFocused={{
color: 'crimson'
}}
value={code}
onTextChange={code => this.setState({ code })}
/>
</View>
{/* Custom placeholder & mask */}
<View style={styles.section}>
<Text style={styles.title}>Custom Placeholder</Text>
<SmoothPinCodeInput
placeholder={<View style={{
width: 10,
height: 10,
borderRadius: 25,
opacity: 0.3,
backgroundColor: 'blue',
}}></View>}
mask={<View style={{
width: 10,
height: 10,
borderRadius: 25,
backgroundColor: 'blue',
}}></View>}
maskDelay={1000}
password={true}
cellStyle={null}
cellStyleFocused={null}
value={code}
onTextChange={code => this.setState({ code })}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
section: {
alignItems: 'center',
margin: 16,
},
title: {
fontSize: 16,
fontWeight: 'bold',
marginBottom: 8,
},
});约束与限制
属性
[!TIP] "Platform"列表示该属性在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support | 默认值 | | ----------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -------- | ----------- | ----------------- | ---------------------------------------- | | value | 输入框显示的值 | String | no | IOS/Android | yes | '' | | codeLength | 输入字符的个数 | Number | no | IOS/Android | yes | 4 | | cellSize | 每个单元格的大小 | Number | no | IOS/Android | yes | 48 | | cellSpacing | 单元格之间的间距 | Number | no | IOS/Android | yes | 4 | | placeholder | '' | String | no | IOS/Android | yes | Element | | mask | '*' | String | no | IOS/Android | yes | Element | | maskDelay | 字符被遮罩前的延迟,单位为毫秒 | Number | no | IOS/Android | yes | 200 | | password | 是否遮罩输入值。每个单元格使用 mask 属性进行遮罩 | Boolean | no | IOS/Android | yes | FALSE | | autoFocus | 如果为 true,则在组件挂载时聚焦输入框 | Boolean | no | IOS/Android | yes | FALSE | | editable | 如果为 false,则每个单元格不可编辑 | Boolean | no | IOS/Android | yes | TRUE | | animated | 是否启用动画 | Boolean | no | IOS/Android | yes | TRUE | | animationFocused | 聚焦单元格的动画。可以是预设动画的字符串形式,也可以是自定义动画对象 | String, Object | no | IOS/Android | yes | 'pulse' | | restrictToNumbers | 是否仅限制输入数字 | Boolean | no | IOS/Android | yes | FALSE | | containerStyle | 整个单元格容器的视图样式 | React View StyleSheet | no | IOS/Android | yes | {} | | cellStyle | 每个单元格的视图样式 | React View StyleSheet | no | IOS/Android | yes | { borderColor: 'gray', borderWidth: 1} | | cellStyleFocused | 聚焦单元格的视图样式 | React View StyleSheet | no | IOS/Android | yes | { borderColor: 'black', borderWidth: 2 } | | textStyle | 单元格值的文本样式 | React Text StyleSheet | no | IOS/Android | yes | { color: 'gray', fontSize: 24 } | | textStyleFocused | 聚焦单元格值的文本样式 | React Text StyleSheet | no | IOS/Android | yes | { color: 'black' } | | onFulfill | 当输入完全填满时调用的回调函数 | Function | no | IOS/Android | yes | null | | onTextChange | 当文本更改时调用的回调函数 | Function | no | IOS/Android | yes | null | | onBackspace | 当输入为空且按下退格键时调用的回调函数 | Function | no | IOS/Android | yes | null | | keyboardType | 确定打开的键盘类型 | Enum('default', 'number-pad', 'decimal-pad', 'numeric', 'email-address', 'phone-pad') | no | IOS/Android | yes | 'numeric' |
开源协议
本项目基于 MIT License (MIT) ,请自由地享受和参与开源。
