import React from 'react'; import { View, StyleSheet, Text } from 'react-native'; import Form from '../index'; import Button from '../../Button'; import { ScrollView } from 'react-native'; import BottomSafeArea from '../../BottomSafeArea'; import { Fill, Toast } from '@xrnjs/ui'; import Card from '_global/Card'; import PasswordInput from '../../PasswordInput'; type FormData = { password: string; confirmPassword: string; oldPassword: string; newPassword: string; loginPassword: string; paymentPassword: string; }; const PasswordInputExamplesDemo: React.FC = () => { const form = Form.useForm({ defaultValues: { password: '', confirmPassword: '', oldPassword: '', newPassword: '', loginPassword: '', paymentPassword: '', }, }); const [fixedPassword, setFixedPassword] = React.useState('External@123'); const onSubmit = (data: FormData) => { console.log('Form submitted:', data); Toast('密码设置成功!'); }; const onError = (errors: Record) => { console.log('Form errors:', errors); }; return ( {/* Form.PasswordInput 快捷用法 */} {/* Form.Item + PasswordInput 组合用法 */} { const password = form.getValues('password'); return value === password || '两次输入的密码不一致'; }, }} > {/* 密码修改示例 */} { const oldPassword = form.getValues('oldPassword'); if (value === oldPassword) { return '新密码不能与当前密码相同'; } if ( !/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/.test( value ) ) { return '密码必须包含大小写字母、数字和特殊字符'; } return true; }, }} /> {/* 高级配置示例 */} { console.log('密码显示状态:', secure ? '隐藏' : '显示'); }} rules={{ required: '请输入登录密码', validate: (value: string) => { if (value.length < 6) { return '登录密码至少6位'; } if (!/\d/.test(value)) { return '密码必须包含数字'; } return true; }, }} /> {/* 复杂验证示例 */} { const checks = [ { test: /[a-z]/, message: '缺少小写字母' }, { test: /[A-Z]/, message: '缺少大写字母' }, { test: /\d/, message: '缺少数字' }, { test: /[@$!%*?&]/, message: '缺少特殊字符(@$!%*?&)' }, ]; for (const check of checks) { if (!check.test.test(value)) { return check.message; } } // 检查是否包含连续字符 if (/(.)\1{2,}/.test(value)) { return '不能包含3个或以上连续相同字符'; } // 检查是否包含常见弱密码模式 const weakPatterns = ['123', 'abc', 'password', 'qwerty']; for (const pattern of weakPatterns) { if (value.toLowerCase().includes(pattern)) { return `不能包含常见弱密码模式: ${pattern}`; } } return true; }, }} /> {/* 禁用状态示例 */} 当前值:{fixedPassword} setFixedPassword(v)} /> ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', }, scrollContainer: { flex: 1, padding: 20, paddingBottom: 0, }, cardStyle: { marginBottom: 16, }, fixedButtonContainer: { backgroundColor: '#fff', paddingHorizontal: 20, paddingTop: 16, paddingBottom: 30, borderTopWidth: 1, borderTopColor: '#e5e5e5', flexDirection: 'row', justifyContent: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: -2, }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 8, }, buttonSpacer: { width: 20, }, }); export default PasswordInputExamplesDemo;