網站首頁 編程語言 正文
正文
項目中需求如果涉及錢包,支付等功能,可以大概率會用到輸入密碼組件,也算是個常見組件吧。
之前寫過一個純js的開源組件,使用的類的形式,也比較老了,可直接添加npm庫到項目中進行使用。
hooks版本
最近項目需要,又重新寫了一個hooks版本的,現在直接上源碼,對于不想添加依賴庫的伙伴,可直接復制源碼到項目中,直接使用。
'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…
原文鏈接:https://juejin.cn/post/7170513204731183117
相關推薦
- 2022-04-24 GBase?8s數據庫主鍵約束、唯一約束和唯一索引的區別解析_數據庫其它
- 2022-03-16 ASP.NET?Core在Linux下為dotnet創建守護進程_基礎應用
- 2022-06-23 Python+Turtle制作獨特的表白圖_python
- 2022-12-03 React使用ref方法與場景介紹_React
- 2022-04-10 為Xamarin.Forms的導航欄增加搜索功能_C#教程
- 2022-05-12 基于nginx反向代理獲取用戶真實Ip地址詳解_nginx
- 2022-09-23 python人工智能自定義求導tf_diffs詳解_python
- 2022-12-11 C語言冷知識之預處理字符串操作符詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支