ReactNative支付密碼輸入框?qū)崿F(xiàn)詳解
正文
項(xiàng)目中需求如果涉及錢包,支付等功能,可以大概率會(huì)用到輸入密碼組件,也算是個(gè)常見組件吧。
之前寫過一個(gè)純js的開源組件,使用的類的形式,也比較老了,可直接添加npm庫到項(xiàng)目中進(jìn)行使用。
hooks版本
最近項(xiàng)目需要,又重新寫了一個(gè)hooks版本的,現(xiàn)在直接上源碼,對(duì)于不想添加依賴庫的伙伴,可直接復(fù)制源碼到項(xiàng)目中,直接使用。
'use strict';
import React, {useRef, useState} from 'react';
import {StyleSheet, View, Pressable, TextInput} from 'react-native';
// 支付密碼輸入框
const PasswordInput = ({isAutoFocus = false}) => {
const [msg, setMsg] = useState('');
const textInputRef = useRef();
const onEnd = (text: string) => {
console.log('輸入密碼:', text);
};
const _getInputItem = () => {
let inputItem = [];
for (let i = 0; i < 6; i++) {
inputItem.push(
<View key={i} style={i === 5 ? [styles.textInputView, {borderRightWidth: 1}] : [styles.textInputView, {borderRightWidth: 0}]}>
{i < msg.length ? <View style={{width: 16, height: 16, backgroundColor: '#222', borderRadius: 8}} /> : null}
</View>,
);
}
return inputItem;
};
const _onPress = () => {
textInputRef?.current.focus();
};
return (
<Pressable onPress={_onPress}>
<View style={styles.container}>
<View style={{flexDirection: 'row', marginTop: 36, justifyContent: 'center'}}>
<TextInput
style={styles.textInputMsg}
ref={textInputRef}
maxLength={6}
autoFocus={isAutoFocus}
keyboardType="number-pad"
defaultValue={msg}
onChangeText={text => {
setMsg(text);
if (text.length === 6) {
onEnd(text);
}
}}
/>
{_getInputItem()}
</View>
</View>
</Pressable>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
justifyContent: 'center',
alignItems: 'center',
},
textInputView: {
height: 85 / 2,
width: 85 / 2,
borderWidth: 1,
borderColor: '#c9c7c7',
justifyContent: 'center',
alignItems: 'center',
},
textInputMsg: {
height: 45,
zIndex: 99,
position: 'absolute',
width: 45 * 6,
opacity: 0,
},
});
export default PasswordInput;
使用方式就很簡單了:
<PasswordInput />
項(xiàng)目庫地址鏈接Github: github.com/wayne214/re…
以上就是ReactNative支付密碼輸入框?qū)崿F(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于ReactNative支付密碼輸入框的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
react-native android狀態(tài)欄的實(shí)現(xiàn)
這篇文章主要介紹了react-native android狀態(tài)欄的實(shí)現(xiàn),使?fàn)顟B(tài)欄顏色與App顏色一致,使用戶界面更加整體。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
React+Node.js實(shí)現(xiàn)大文件傳輸與斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了如何使用React前端和Node.js后端實(shí)現(xiàn)大文件傳輸和斷點(diǎn)續(xù)傳的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下2024-12-12
React使用react-sortable-hoc如何實(shí)現(xiàn)拖拽效果
這篇文章主要介紹了React使用react-sortable-hoc如何實(shí)現(xiàn)拖拽效果問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
React Fiber中面試官最關(guān)心的技術(shù)話題
這篇文章主要為大家介紹了React Fiber中面試官最關(guān)心的技術(shù)話題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
ReactJS實(shí)現(xiàn)表單的單選多選和反選的示例
本篇文章主要介紹了ReactJS實(shí)現(xiàn)表單的單選多選和反選的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10

