import React from 'react'; import { View, StyleSheet, Text } from 'react-native'; import Form from '../index'; import Checkbox from '../../Checkbox'; import Button from '../../Button'; import { ScrollView } from 'react-native'; import BottomSafeArea from '../../BottomSafeArea'; import { Fill, Toast } from '@xrnjs/ui'; import Card from '_global/Card'; type FormData = { hobbies: string[]; agreements: boolean; permissions: string[]; features: string[]; marketing: boolean; terms: boolean; privacy: boolean; notifications: string[]; }; const CheckboxExamplesDemo: React.FC = () => { const form = Form.useForm({ defaultValues: { hobbies: [], agreements: false, permissions: ['read'], features: [], marketing: false, terms: false, privacy: false, notifications: [], }, }); const [marketingFixed, setMarketingFixed] = React.useState(true); const onSubmit = (data: FormData) => { console.log('Form submitted:', data); Toast( `提交成功!\n兴趣爱好: ${data.hobbies.join( ', ' )}\n权限: ${data.permissions.join(', ')}` ); }; const onError = (errors: Record) => { console.log('Form errors:', errors); }; return ( {/* 组合用法示例 */} { if (!value || value.length === 0) { return '请至少选择一个兴趣爱好'; } if (value.length < 2) { return '为了更好地为您推荐内容,请至少选择两个兴趣爱好'; } return true; }, }} > { if (value && value.length > 3) { return '最多只能选择3个功能'; } return true; }, }} > console.log(e)} label="数据分析报表" /> {/* 快捷用法示例 */} console.log(e)} rules={{ validate: (value: string[] | boolean) => { // 处理单个checkbox的情况 if (Array.isArray(value)) { return value.length > 0 || '请同意服务条款'; } return value === true || '请同意服务条款'; }, }} checkboxLabel="我已阅读并同意《用户协议》和《隐私政策》" /> {/* Form.CheckboxGroup 示例 */} { console.log('CheckboxGroup value:', value); }} rules={{ required: '请至少选择一个权限', validate: (value: string[]) => { 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.includes('all') && value.length > 1) { return '选择"全部通知"时不能同时选择其他选项'; } return true; }, }} options={[ { label: '全部通知', value: 'all' }, { label: '系统通知', value: 'system' }, { label: '安全提醒', value: 'security' }, { label: '活动推送', value: 'activity' }, ]} /> {/* 高级配置示例 */} {/* 禁用状态示例 */} 当前值:{marketingFixed ? 'true' : 'false'} setMarketingFixed(v)} /> ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', }, scrollContainer: { flex: 1, padding: 12, 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 CheckboxExamplesDemo;