@material-ui/lab
Version:
Material-UI Lab - Incubator for Material-UI React components.
292 lines (267 loc) • 8.49 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import * as React from 'react';
import PropTypes from 'prop-types';
import { createStyles, withStyles } from '@material-ui/core/styles';
import clsx from 'clsx';
import MonthPicker from '../MonthPicker/MonthPicker';
import { useCalendarState } from './useCalendarState';
import { useUtils } from '../internal/pickers/hooks/useUtils';
import FadeTransitionGroup from './PickersFadeTransitionGroup';
import Calendar from './PickersCalendar';
import { useViews } from '../internal/pickers/hooks/useViews';
import { DAY_SIZE, DAY_MARGIN } from '../internal/pickers/constants/dimensions';
import CalendarHeader from './PickersCalendarHeader';
import YearPicker from '../YearPicker/YearPicker';
import { defaultMinDate, defaultMaxDate } from '../internal/pickers/constants/prop-types';
import { IsStaticVariantContext } from '../internal/pickers/wrappers/WrapperVariantContext';
import { findClosestEnabledDate } from '../internal/pickers/date-utils';
import PickerView from '../internal/pickers/Picker/PickerView';
export const styles = createStyles({
root: {
display: 'flex',
flexDirection: 'column'
},
viewTransitionContainer: {
overflowY: 'auto'
},
fullHeightContainer: {
flex: 1,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
minHeight: (DAY_SIZE + DAY_MARGIN * 4) * 7,
height: '100%'
}
});
export const defaultReduceAnimations = typeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent);
/**
* @ignore - do not document.
*/
const DayPicker = /*#__PURE__*/React.forwardRef(function DayPicker(props, ref) {
const {
allowKeyboardControl: allowKeyboardControlProp,
onViewChange,
date,
disableFuture,
disablePast,
defaultCalendarMonth,
classes,
loading,
maxDate: maxDateProp,
minDate: minDateProp,
onChange,
onMonthChange,
reduceAnimations = defaultReduceAnimations,
renderLoading,
shouldDisableDate,
shouldDisableYear,
view,
views = ['year', 'date'],
openTo = 'date',
className
} = props,
other = _objectWithoutPropertiesLoose(props, ["allowKeyboardControl", "onViewChange", "date", "disableFuture", "disablePast", "defaultCalendarMonth", "classes", "loading", "maxDate", "minDate", "onChange", "onMonthChange", "reduceAnimations", "renderLoading", "shouldDisableDate", "shouldDisableYear", "view", "views", "openTo", "className"]);
const utils = useUtils();
const isStatic = React.useContext(IsStaticVariantContext);
const allowKeyboardControl = allowKeyboardControlProp !== null && allowKeyboardControlProp !== void 0 ? allowKeyboardControlProp : !isStatic;
const minDate = minDateProp || utils.date(defaultMinDate);
const maxDate = maxDateProp || utils.date(defaultMaxDate);
const {
openView,
setOpenView
} = useViews({
view,
views,
openTo,
onChange,
onViewChange
});
const {
calendarState,
changeFocusedDay,
changeMonth,
isDateDisabled,
handleChangeMonth,
onMonthSwitchingAnimationEnd
} = useCalendarState({
date,
defaultCalendarMonth,
reduceAnimations,
onMonthChange,
minDate,
maxDate,
shouldDisableDate,
disablePast,
disableFuture
});
React.useEffect(() => {
if (date && isDateDisabled(date)) {
const closestEnabledDate = findClosestEnabledDate({
utils,
date,
minDate,
maxDate,
disablePast: Boolean(disablePast),
disableFuture: Boolean(disableFuture),
shouldDisableDate: isDateDisabled
});
onChange(closestEnabledDate, 'partial');
} // This call is too expensive to run it on each prop change.
// So just ensure that we are not rendering disabled as selected on mount.
}, []); // eslint-disable-line
React.useEffect(() => {
if (date) {
changeMonth(date);
}
}, [date]); // eslint-disable-line
return /*#__PURE__*/React.createElement(PickerView, {
ref: ref,
className: clsx(classes.root, className)
}, /*#__PURE__*/React.createElement(CalendarHeader, _extends({}, other, {
views: views,
openView: openView,
currentMonth: calendarState.currentMonth,
onViewChange: setOpenView,
onMonthChange: (newMonth, direction) => handleChangeMonth({
newMonth,
direction
}),
minDate: minDate,
maxDate: maxDate,
disablePast: disablePast,
disableFuture: disableFuture,
reduceAnimations: reduceAnimations
})), /*#__PURE__*/React.createElement(FadeTransitionGroup, {
reduceAnimations: reduceAnimations,
className: classes.viewTransitionContainer,
transKey: openView
}, /*#__PURE__*/React.createElement("div", null, openView === 'year' && /*#__PURE__*/React.createElement(YearPicker, _extends({}, other, {
date: date,
onChange: onChange,
minDate: minDate,
maxDate: maxDate,
disableFuture: disableFuture,
disablePast: disablePast,
isDateDisabled: isDateDisabled,
allowKeyboardControl: allowKeyboardControl,
shouldDisableYear: shouldDisableYear,
onFocusedDayChange: changeFocusedDay
})), openView === 'month' && /*#__PURE__*/React.createElement(MonthPicker, _extends({}, other, {
date: date,
onChange: onChange,
minDate: minDate,
maxDate: maxDate,
onMonthChange: onMonthChange
})), openView === 'date' && /*#__PURE__*/React.createElement(Calendar, _extends({}, other, calendarState, {
onMonthSwitchingAnimationEnd: onMonthSwitchingAnimationEnd,
onFocusedDayChange: changeFocusedDay,
reduceAnimations: reduceAnimations,
date: date,
onChange: onChange,
isDateDisabled: isDateDisabled,
allowKeyboardControl: allowKeyboardControl,
loading: loading,
renderLoading: renderLoading
})))));
});
DayPicker.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Enables keyboard listener for moving between days in calendar.
* @default currentWrapper !== 'static'
*/
allowKeyboardControl: PropTypes.bool,
/**
* @ignore
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* @ignore
*/
date: PropTypes.any,
/**
* Default calendar month displayed when `value={null}`.
* @default `new Date()`
*/
defaultCalendarMonth: PropTypes.any,
/**
* Disable future dates.
* @default false
*/
disableFuture: PropTypes.bool,
/**
* Disable past dates.
* @default false
*/
disablePast: PropTypes.bool,
/**
* If `true` renders `LoadingComponent` in calendar instead of calendar view.
* Can be used to preload information and show it in calendar.
* @default false
*/
loading: PropTypes.bool,
/**
* Max selectable date. @DateIOType
* @default Date(2099-31-12)
*/
maxDate: PropTypes.any,
/**
* Min selectable date. @DateIOType
* @default Date(1900-01-01)
*/
minDate: PropTypes.any,
/**
* Callback fired on date change
*/
onChange: PropTypes.func.isRequired,
/**
* Callback firing on month change. @DateIOType
*/
onMonthChange: PropTypes.func,
/**
* Callback fired on view change.
*/
onViewChange: PropTypes.func,
/**
* Initially open view.
*/
openTo: PropTypes.oneOf(['date', 'month', 'year']),
/**
* Disable heavy animations.
* @default /(android)/i.test(window.navigator.userAgent).
*/
reduceAnimations: PropTypes.bool,
/**
* Component displaying when passed `loading` true.
* @default () => "..."
*/
renderLoading: PropTypes.func,
/**
* Disable specific date. @DateIOType
*/
shouldDisableDate: PropTypes.func,
/**
* Disable specific years dynamically.
* Works like `shouldDisableDate` but for year selection view. @DateIOType.
*/
shouldDisableYear: PropTypes.func,
/**
* Controlled open view.
*/
view: PropTypes.oneOf(['date', 'month', 'year']),
/**
* Views for day picker.
*/
views: PropTypes.arrayOf(PropTypes.oneOf(['date', 'month', 'year']).isRequired)
};
export default withStyles(styles, {
name: 'MuiDayPicker'
})(DayPicker);