import React from 'react'; import { View, StyleSheet } from 'react-native'; import Form from '../index'; import Input from '../../Input'; import Switch from '../../Switch'; import Radio from '../../Radio'; import Checkbox from '../../Checkbox'; import Button from '../../Button'; import { Values } from '../../AmountInput/interface'; import { ScrollView } from 'react-native'; import BottomSafeArea from '../../BottomSafeArea'; import { Fill, Toast } from '@xrnjs/ui'; import Card from '_global/Card'; import AmountInput from '../../AmountInput'; import OptionGroup from '../../OptionGroup'; import Options from '../../Options'; type FormData = { username: string; email: string; password: string; confirmPassword: string; description: string; feedback: string; transferAmount: Values; depositAmount: Values; isVip: boolean; notifications: boolean; gender: string; city: string; plan: string; hobbies: string[]; agreements: boolean; permissions: string[]; tags: string[]; category: string; priority: number; withdrawMethod: string; paymentMethods: string[]; cardType: string; // 新增字段 birthDate: string; vacationPeriod: [string, string]; loginPassword: string; country: string; skills: string[]; projectTags: string[]; }; // 使用高阶组件创建表单组件 const FormSwitch = Form.withFormItem(Switch, 'FormSwitch'); const FormBasicDemo: React.FC = () => { const form = Form.useForm({ defaultValues: { username1: '', username: '', email: '', password: '', confirmPassword: '', description: '', feedback: '', transferAmount: { amount: '', currency: 'USD' }, depositAmount: { amount: '', currency: 'CNY' }, isVip: false, notifications: true, gender: '', city: 'beijing', plan: 'basic', hobbies: [], agreements: false, permissions: ['read'], tags: [], category: '', priority: 1, withdrawMethod: '', paymentMethods: [], cardType: '', // 新增字段默认值 birthDate: '', vacationPeriod: ['', ''] as [string, string], loginPassword: '', country: '', skills: [], projectTags: [], }, }); const onSubmit = (data: FormData) => { console.log('Form submitted:', data); Toast( `提交成功 用户名: ${data.username}\n邮箱: ${ data.email }\n个人简介: ${data.description.substring(0, 20)}...` ); }; const onError = (errors: Record) => { console.log('Form errors:', errors); }; return ( {/* 显式传递 control prop 示例 */} {/* 组合用法示例 */} {/* 快捷用法示例 */} { const password = form.getValues('password'); return value === password || '两次密码不匹配'; }, }} /> {/* TextArea 示例 */} { if (value && value.length < 5) { return '意见反馈至少需要5个字符'; } return true; }, }} message="您的反馈对我们很重要" /> {/* AmountInput 示例 */} { if (!value.amount || parseFloat(value.amount) <= 0) { return '转账金额必须大于0'; } if (parseFloat(value.amount) > 10000) { return '转账金额不能超过可用余额'; } return true; }, }} /> { if (!value.amount) { return '请输入存款金额'; } if (parseFloat(value.amount) < 100) { return '最小存款金额为100'; } return true; }, }} > value === true || '必须开启VIP才能继续', }} /> name="plan" label="会员套餐" message="选择适合您的套餐" rules={{ required: '请选择会员套餐', validate: (value: string) => value !== 'free' || '免费套餐功能有限,建议升级', }} options={[ { label: '免费版', value: 'free' }, { label: '基础版 ¥99/月', value: 'basic' }, { label: '专业版 ¥199/月', value: 'pro' }, { label: '企业版 ¥999/月', value: 'enterprise' }, ]} /> { if (!value || value.length === 0) { return '请至少选择一个兴趣爱好'; } if (value.length < 2) { return '为了更好地为您推荐内容,请至少选择两个兴趣爱好'; } return true; }, }} > { // 处理单个checkbox的情况 if (Array.isArray(value)) { return value.length > 0 || '请同意服务条款'; } return value === true || '请同意服务条款'; }, }} options={[ { label: '我已阅读并同意《用户协议》和《隐私政策》', value: true, }, ]} /> { if (!value || value.length === 0) { return '请至少选择一个权限'; } if (value.includes('admin') && !value.includes('read')) { return '管理员权限必须包含读取权限'; } if (value.includes('delete') && !value.includes('write')) { return '删除权限必须包含写入权限'; } return true; }, }} options={[ { label: '读取权限', value: 'read' }, { label: '写入权限', value: 'write' }, { label: '删除权限', value: 'delete' }, { label: '管理员权限', value: 'admin' }, ]} /> { if (!value || value.length === 0) { return '请至少选择一个标签'; } return true; }, }} > { Toast(`${option.label} 暂不可用`); }} /> { if (!value || value.length === 0) { return '请至少选择一种支付方式'; } if (value.includes('crypto') && !value.includes('bank')) { return '选择加密货币支付需要同时支持银行卡'; } return true; }, }} multiple columns={2} showIcon style={styles.optionsFullWidth} fullColumn options={[ { label: '银行卡', description: '借记卡/信用卡', value: 'bank', }, { label: '支付宝', description: '余额/花呗', value: 'alipay', }, { label: '微信支付', description: '零钱/银行卡', value: 'wechat', }, { label: '加密货币', description: 'USDT/BTC', value: 'crypto', }, ]} /> {/* 新增组件示例 */} { if (!value || value.length === 0) { return '请至少添加一个项目标签'; } if (value.length > 5) { return '最多只能添加5个标签'; } return true; }, }} message="为您的项目添加相关标签" /> ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', }, scrollContainer: { flex: 1, padding: 20, paddingBottom: 0, }, title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20, textAlign: 'center', }, sectionTitle: { fontSize: 16, fontWeight: '600', marginTop: 20, marginBottom: 10, color: '#333', }, 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, }, optionsFullWidth: { paddingHorizontal: 0, }, }); export default FormBasicDemo;