import moment from 'moment'; import { NativeModules } from 'react-native'; import { ActionTypes, NATIVE_FORMAT } from './constants'; type RNMonthPickerDialogModule = { open: (options: { value?: number; onChange?: (action: ActionTypes, date: Date | undefined) => void; minimumDate?: number | null; maximumDate?: number | null; doneButtonLabel?: string; cancelButtonLabel?: string; autoTheme?: boolean; locale?: string; /* * Theme variant for the date picker. if autoTheme is true, this prop will be ignored. * - light: light theme * - dark: dark theme * */ themeVariant?: 'dark' | 'light'; }) => Promise<{ action: ActionTypes; year: number; month: number }>; }; const RNMonthPickerDialogModule = NativeModules.RNMonthYearPicker; interface MonthYearPickerDialogAndroidProps { value?: Date; onChange?: (action: string, date: Date | undefined) => void; minimumDate?: Date; maximumDate?: Date; doneButtonLabel?: string; cancelButtonLabel?: string; autoTheme?: boolean; locale?: string; /* * Theme variant for the date picker. if autoTheme is true, this prop will be ignored. * - light: light theme * - dark: dark theme * */ themeVariant?: 'dark' | 'light'; } const MonthYearPickerDialogAndroid = ({ value = new Date(), onChange, minimumDate, maximumDate, doneButtonLabel = 'Done', cancelButtonLabel = 'Cancel', autoTheme = false, locale = 'en', themeVariant = 'light', }: MonthYearPickerDialogAndroidProps) => { RNMonthPickerDialogModule.open({ value: value.getTime(), minimumDate: minimumDate?.getTime() ?? null, maximumDate: maximumDate?.getTime() ?? null, okButton: doneButtonLabel, cancelButton: cancelButtonLabel, autoTheme, locale, themeVariant, }).then( ({ action, year, month, }: { action: ActionTypes; year?: number; month?: number; }) => { let date; switch (action) { case ActionTypes.dateSetAction: date = moment(`${month}-${year}`, NATIVE_FORMAT).toDate(); break; case ActionTypes.dismissedAction: default: date = undefined; } if (onChange) { onChange(action, date); } }, (error: Error) => { throw error; } ); return null; }; export default MonthYearPickerDialogAndroid;