UNPKG

react-native-cn-month-year-picker

Version:

React Native Month Picker component for iOS & Android

48 lines (43 loc) 1.06 kB
import moment from 'moment'; import invariant from 'invariant'; moment.suppressDeprecationWarnings = true; import RNMonthPickerDialogModule from './RNMonthPickerDialogModule'; import { ACTION_DATE_SET, ACTION_DISMISSED, ACTION_NEUTRAL, NATIVE_FORMAT, } from './constants'; const MonthPicker = ({ value, onChange, minimumDate, maximumDate, ...restProps }) => { RNMonthPickerDialogModule.open({ value: new Date(moment(value, NATIVE_FORMAT).toDate()).getTime(), minimumDate: minimumDate?.getTime() ?? null, maximumDate: maximumDate?.getTime() ?? null, ...restProps, }).then( ({ action, year, month }) => { let date; switch (action) { case ACTION_DATE_SET: case ACTION_NEUTRAL: date = moment(`${month}-${year}`, NATIVE_FORMAT).toDate(); break; case ACTION_DISMISSED: default: date = undefined; } onChange && onChange(action, date); }, (error) => { throw error; }, ); return null; }; export default MonthPicker;