import React from 'react'; import { View, StyleSheet, Text } from 'react-native'; import { ScrollView } from 'react-native'; import Form from '../index'; import Button from '../../Button'; import { BottomSafeArea, Fill, Toast } from '@xrnjs/ui'; import Card from '_global/Card'; type FormData = { category: string; category1: string; tags: string[]; }; const CategoryList = [ { label: '前端开发', value: 'frontend' }, { label: '后端开发', value: 'backend' }, { label: '移动开发', value: 'mobile' }, { label: '数据科学', value: 'data' }, ]; const SelectExamplesDemo: React.FC = () => { const form = Form.useForm({ defaultValues: { category: '', tags: [], }, }); const [categoryFixed, setCategoryFixed] = React.useState('backend'); const getLabelByValue = ( opts: { label: string; value: string }[], v?: string ) => opts.find((o) => o.value === v)?.label || ''; const onSubmit = (data: FormData) => { console.log('Form submitted:', data); Toast( [ '提交成功', `分类: ${data.category || '未选择'}`, `分类1: ${data.category1 || '未选择'}`, `标签: ${data.tags.join(', ') || '未选择'}`, ].join('\n') ); }; const onError = (errors: Record) => { console.log('Form errors:', errors); }; return ( {/* 单选示例 */} console.log('单选变更:', val)} /> console.log('单选变更:', val)} showConfirmButton onConfirm={(val) => { console.log('单选确认:', val); return false; // 不关闭 }} /> {/* 多选示例 */} console.log('多选变更:', val)} onConfirm={(val) => { console.log('多选确认:', val); }} /> {/* 高级用法:搜索 + 自定义筛选 */} { const v = inputValue.trim().toLowerCase(); if (!v) return true; return ( String(option.label).toLowerCase().includes(v) || String((option as any).subLabel || '') .toLowerCase() .includes(v) ); }} onSearch={(val) => console.log('搜索内容:', val)} /> 当前值:{getLabelByValue(CategoryList, categoryFixed)} ( {categoryFixed}) setCategoryFixed(v as string)} /> ); }; 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 SelectExamplesDemo;