import { TurboModule } from '@rnoh/react-native-openharmony/ts'; import type { TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; import Logger from './Logger'; export class DateTimePickerAndroidModule extends TurboModule { constructor(protected ctx: TurboModuleContext) { super(ctx); } // 定义 open 方法 open(options:{date: string, minDate: string, maxDate: string, mode: 'calendar' |'spinner' | 'default'}): Promise<{ action: 'dateSetAction' | 'dismissedAction', year?: number, month?: number, day?: number, }> { return new Promise((resolve) => { Logger.info("DatePickerDialog:mode()" + options.mode) Logger.info("DatePickerDialog:minDate()" + JSON.stringify(options)) if(['calendar', 'default'].includes(options.mode)){ CalendarPickerDialog.show({ selected: new Date(options.date), onAccept: (value) => { const min = options.minDate ? new Date(options.minDate) : null; const max = options.maxDate ? new Date(options.maxDate) : null; let selectedDate = new Date(value); // 判断 selectedDate 是否小于 minDate if (min && selectedDate < min) { selectedDate = min; } // 判断 selectedDate 是否大于 maxDate if (max && selectedDate > max) { selectedDate = max; } resolve({ action: 'dateSetAction', year: selectedDate.getFullYear(), month: selectedDate.getMonth(), day: selectedDate.getDate(), }); }, onCancel: () => { resolve({ action: 'dismissedAction' }); }, }); }else { DatePickerDialog.show({ selected: new Date(options.date), start: new Date(options.minDate), end: new Date(options.maxDate), showTime: false, onCancel: () => { console.info("DatePickerDialog:onCancel()") resolve({ action: 'dismissedAction' }); }, onDateAccept(value: Date){ Logger.info("onDateAccept onAccept:" + JSON.stringify(value)); const selectedDate = new Date(value); resolve({ action: 'dateSetAction', year: selectedDate.getFullYear(), month: selectedDate.getMonth(), day: selectedDate.getDate(), }); } }) } }); } }