UNPKG

@material-ui/lab

Version:
162 lines (143 loc) 4.81 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; 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 var monthPickerClasses = generateUtilityClasses('MuiMonthPicker', ['root']); var useUtilityClasses = function useUtilityClasses(styleProps) { var classes = styleProps.classes; var slots = { root: ['root'] }; return composeClasses(slots, getMonthPickerUtilityClass, classes); }; var MonthPickerRoot = styled('div', { name: 'MuiMonthPicker', slot: 'Root', overridesResolver: function overridesResolver(props, styles) { return styles.root; } })({ width: 310, display: 'flex', flexWrap: 'wrap', alignContent: 'stretch' }); var MonthPicker = /*#__PURE__*/React.forwardRef(function MonthPicker(inProps, ref) { var props = useThemeProps({ props: inProps, name: 'MuiMonthPicker' }); var className = props.className, date = props.date, disableFuture = props.disableFuture, disablePast = props.disablePast, maxDate = props.maxDate, minDate = props.minDate, onChange = props.onChange, onMonthChange = props.onMonthChange, other = _objectWithoutProperties(props, ["className", "date", "disableFuture", "disablePast", "maxDate", "minDate", "onChange", "onMonthChange"]); var styleProps = props; var classes = useUtilityClasses(styleProps); var utils = useUtils(); var now = useNow(); var currentMonth = utils.getMonth(date || now); var shouldDisableMonth = function shouldDisableMonth(month) { var firstEnabledMonth = utils.startOfMonth(disablePast && utils.isAfter(now, minDate) ? now : minDate); var lastEnabledMonth = utils.startOfMonth(disableFuture && utils.isBefore(now, maxDate) ? now : maxDate); var isBeforeFirstEnabled = utils.isBefore(month, firstEnabledMonth); var isAfterLastEnabled = utils.isAfter(month, lastEnabledMonth); return isBeforeFirstEnabled || isAfterLastEnabled; }; var onMonthSelect = function onMonthSelect(month) { var 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(function (month) { var monthNumber = utils.getMonth(month); var 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;