網(wǎng)站首頁 編程語言 正文
正文
項(xiàng)目中需求如果涉及錢包,支付等功能,可以大概率會用到輸入密碼組件,也算是個(gè)常見組件吧。
之前寫過一個(gè)純js的開源組件,使用的類的形式,也比較老了,可直接添加npm庫到項(xiàng)目中進(jìn)行使用。
hooks版本
最近項(xiàng)目需要,又重新寫了一個(gè)hooks版本的,現(xiàn)在直接上源碼,對于不想添加依賴庫的伙伴,可直接復(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…
原文鏈接:https://juejin.cn/post/7170513204731183117
相關(guān)推薦
- 2023-03-20 Redis腦裂導(dǎo)致數(shù)據(jù)丟失的解決_Redis
- 2022-07-16 TritonObjDet新增要素的方法
- 2022-12-24 Python創(chuàng)建增量目錄的代碼實(shí)例_python
- 2022-04-25 ASP.NET?Core?MVC中過濾器工作原理介紹_實(shí)用技巧
- 2022-07-17 關(guān)于elasticsearch連接時(shí)斷時(shí)續(xù)以及Kibana出現(xiàn)server is not ready
- 2022-10-22 react實(shí)現(xiàn)Modal彈窗效果_React
- 2022-07-12 tk mybatis報(bào)錯(cuò):org.apache.ibatis.binding.BindingExce
- 2022-11-27 Python線性網(wǎng)絡(luò)實(shí)現(xiàn)分類糖尿病病例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支