UNPKG

react-native-easy-calendar

Version:

Customizable, easy-to-use, performant calendar components for React Native

66 lines (56 loc) 2.38 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import React, { useMemo } from 'react'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import BaseCalendarWrappedInProviders from './Providers'; dayjs.extend(utc); // A thin wrapper to limit the props that can be passed to the BaseCalendar component const DateSelectionCalendar = ({ onSelectDate, disabledDates, selectedDate, initVisibleDate, allowYearView = true, ...others }) => { if (!selectedDate) { throw new Error('The `selectedDate` prop is required. Use an empty array if no dates should be selected.'); } if (typeof selectedDate !== 'string') { throw new Error('The `selectedDate` prop should be a date string in YYYY-MM-DD format.'); } if (!onSelectDate) { throw new Error('The `onSelectDate` prop is required.'); } if (typeof onSelectDate !== 'function') { throw new Error('The `onSelectDate` prop should be function that receives a date string as paramater.'); } const dateProperties = useMemo(() => { let disabledDateProperties = {}; let selectedDateProperties = {}; disabledDateProperties = disabledDates === null || disabledDates === void 0 ? void 0 : disabledDates.reduce((disabled, date) => { disabled[date] = { isDisabled: true }; return disabled; }, {}); if (dayjs(selectedDate).isValid()) { selectedDateProperties = { [dayjs(selectedDate).local().format('YYYY-MM-DD')]: { isSelected: true } }; } // Not possible for a date to be both disabled and selected, so overwriting is OK return { ...disabledDateProperties, ...selectedDateProperties }; }, [selectedDate, disabledDates]); return /*#__PURE__*/React.createElement(BaseCalendarWrappedInProviders, _extends({ allowYearView: allowYearView, onPressDay: onSelectDate, initVisibleDate: initVisibleDate || (dayjs(selectedDate).isValid() ? selectedDate : undefined), dateProperties: dateProperties }, others)); }; export default DateSelectionCalendar; //# sourceMappingURL=DateSelectionCalendar.js.map