ReactNative支付密碼輸入框?qū)崿F(xiàn)詳解
正文
項目中需求如果涉及錢包,支付等功能,可以大概率會用到輸入密碼組件,也算是個常見組件吧。
之前寫過一個純js的開源組件,使用的類的形式,也比較老了,可直接添加npm庫到項目中進(jìn)行使用。
hooks版本
最近項目需要,又重新寫了一個hooks版本的,現(xiàn)在直接上源碼,對于不想添加依賴庫的伙伴,可直接復(fù)制源碼到項目中,直接使用。
'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 />
項目庫地址鏈接Github: github.com/wayne214/re…
以上就是ReactNative支付密碼輸入框?qū)崿F(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于ReactNative支付密碼輸入框的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于React狀態(tài)管理的三個規(guī)則總結(jié)
隨著 JavaScript 單頁應(yīng)用開發(fā)日趨復(fù)雜,JavaScript 需要管理比任何時候都要多的 state (狀態(tài)),這篇文章主要給大家介紹了關(guān)于React狀態(tài)管理的三個規(guī)則,需要的朋友可以參考下2021-07-07
react中通過props實現(xiàn)父子組件間通信的使用示例
在React中,父組件可以通過props屬性向子組件傳遞數(shù)據(jù),子組件可以通過props屬性接收父組件傳遞過來的數(shù)據(jù),本文就來介紹一下如何實現(xiàn),感興趣的可以了解一下2023-10-10
React入門教程之Hello World以及環(huán)境搭建詳解
Facebook 為了開發(fā)一套更好更適合自己的JavaScript MVC 框架,所以產(chǎn)生了react。后來反響很好,所以于2013年5月開源。下面這篇文章主要給大家介紹了關(guān)于React入門教程之Hello World以及環(huán)境搭建的相關(guān)資料,需要的朋友可以參考借鑒。2017-07-07
react?native?reanimated實現(xiàn)動畫示例詳解
這篇文章主要為大家介紹了react?native?reanimated實現(xiàn)動畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
react-router?重新加回跳轉(zhuǎn)攔截功能詳解
這篇文章主要為大家介紹了react-router?重新加回跳轉(zhuǎn)攔截功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

