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 dayjs from 'dayjs'; type FormData = { birthday: Date; appointmentDate: Date; startTime: Date; endTime: Date; workDate: Date; meetingTime: Date; deadlineDate: Date; eventTime: Date; }; const DatePickerExamplesDemo: React.FC = () => { const form = Form.useForm({ defaultValues: { birthday: undefined, appointmentDate: undefined, startTime: undefined, endTime: undefined, workDate: new Date(), meetingTime: undefined, deadlineDate: undefined, eventTime: undefined, }, }); const [fixedDate, setFixedDate] = React.useState( new Date('2025-01-01') ); const onSubmit = (data: FormData) => { console.log('Form submitted:', data); Toast( `提交成功!\n生日: ${ data.birthday ? dayjs(data.birthday).format('YYYY-MM-DD') : '未选择' }\n预约日期: ${ data.appointmentDate ? dayjs(data.appointmentDate).format('YYYY-MM-DD HH:mm') : '未选择' }` ); }; const onError = (errors: Record) => { console.log('Form errors:', errors); }; return ( {/* 基础日期选择示例 */} { if (dayjs(value).isBefore(dayjs(), 'day')) { return '工作日期不能早于今天'; } return true; }, }} message="选择开始工作的日期" min={new Date()} max={dayjs().add(1, 'year').toDate()} /> {/* 时间选择示例 */} { const startTime = form.getValues('startTime'); if (startTime && dayjs(value).isBefore(dayjs(startTime))) { return '结束时间不能早于开始时间'; } return true; }, }} message="选择任务结束时间" /> {/* 高级配置示例 */} { const now = dayjs(); const selected = dayjs(value); if (selected.isBefore(now, 'minute')) { return '预约时间不能早于当前时间'; } if (selected.isAfter(now.add(3, 'months'))) { return '预约时间不能超过3个月后'; } // 不允许选择周末 if (selected.day() === 0 || selected.day() === 6) { return '预约时间不能选择周末'; } // 只允许工作时间 9:00-18:00 const hour = selected.hour(); if (hour < 9 || hour >= 18) { return '预约时间只能在工作时间 9:00-18:00'; } return true; }, }} > { const workDate = form.getValues('workDate'); if (workDate && dayjs(value).isBefore(dayjs(workDate))) { return '截止日期不能早于工作开始日期'; } return true; }, }} message="选择任务或项目的截止日期" min={dayjs().add(1, 'day').toDate()} confirmButtonText="设置截止日期" cancelButtonText="取消" title="设置截止时间" /> {/* 不同模式示例 */} {/* 禁用状态示例 */} {/* 自定义输入框样式示例 */} 当前值:{dayjs(fixedDate).format('YYYY-MM-DD')} setFixedDate(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 DatePickerExamplesDemo;