UNPKG

@material-ui/lab

Version:
165 lines (146 loc) 4.64 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["className", "date", "disableFuture", "disablePast", "maxDate", "minDate", "onChange", "onMonthChange"]; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { styled, useThemeProps } from '@material-ui/core/styles'; import { unstable_composeClasses as composeClasses, generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled'; import PickersMonth from './PickersMonth'; import { useUtils, useNow } from '../internal/pickers/hooks/useUtils'; import { jsx as _jsx } from "react/jsx-runtime"; export function getMonthPickerUtilityClass(slot) { return generateUtilityClass('MuiMonthPicker', slot); } export const monthPickerClasses = generateUtilityClasses('MuiMonthPicker', ['root']); const useUtilityClasses = styleProps => { const { classes } = styleProps; const slots = { root: ['root'] }; return composeClasses(slots, getMonthPickerUtilityClass, classes); }; const MonthPickerRoot = styled('div', { name: 'MuiMonthPicker', slot: 'Root', overridesResolver: (props, styles) => styles.root })({ width: 310, display: 'flex', flexWrap: 'wrap', alignContent: 'stretch' }); const MonthPicker = /*#__PURE__*/React.forwardRef(function MonthPicker(inProps, ref) { const props = useThemeProps({ props: inProps, name: 'MuiMonthPicker' }); const { className, date, disableFuture, disablePast, maxDate, minDate, onChange, onMonthChange } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const styleProps = props; const classes = useUtilityClasses(styleProps); const utils = useUtils(); const now = useNow(); const currentMonth = utils.getMonth(date || now); const shouldDisableMonth = month => { const firstEnabledMonth = utils.startOfMonth(disablePast && utils.isAfter(now, minDate) ? now : minDate); const lastEnabledMonth = utils.startOfMonth(disableFuture && utils.isBefore(now, maxDate) ? now : maxDate); const isBeforeFirstEnabled = utils.isBefore(month, firstEnabledMonth); const isAfterLastEnabled = utils.isAfter(month, lastEnabledMonth); return isBeforeFirstEnabled || isAfterLastEnabled; }; const onMonthSelect = month => { const newDate = utils.setMonth(date || now, month); onChange(newDate, 'finish'); if (onMonthChange) { onMonthChange(newDate); } }; return /*#__PURE__*/_jsx(MonthPickerRoot, _extends({ ref: ref, className: clsx(classes.root, className), styleProps: styleProps }, other, { children: utils.getMonthArray(date || now).map(month => { const monthNumber = utils.getMonth(month); const monthText = utils.format(month, 'monthShort'); return /*#__PURE__*/_jsx(PickersMonth, { value: monthNumber, selected: monthNumber === currentMonth, onSelect: onMonthSelect, disabled: shouldDisableMonth(month), children: monthText }, monthText); }) })); }); process.env.NODE_ENV !== "production" ? MonthPicker.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * Override or extend the styles applied to the component. */ classes: PropTypes.object, /** * className applied to the root element. */ className: PropTypes.string, /** * Date value for the MonthPicker */ date: PropTypes.any, /** * If `true` future days are disabled. */ disableFuture: PropTypes.bool, /** * If `true` past days are disabled. */ disablePast: PropTypes.bool, /** * Maximal selectable date. */ maxDate: PropTypes.any.isRequired, /** * Minimal selectable date. */ minDate: PropTypes.any.isRequired, /** * Callback fired on date change. */ onChange: PropTypes.func.isRequired, /** * @ignore */ onMonthChange: PropTypes.func, /** * The system prop that allows defining system overrides as well as additional CSS styles. */ sx: PropTypes.object } : void 0; /** * * Demos: * * - [Date Picker](https://material-ui.com/components/date-picker/) * * API: * * - [MonthPicker API](https://material-ui.com/api/month-picker/) */ export default MonthPicker;