UNPKG

@material-ui/lab

Version:
235 lines (207 loc) 5.91 kB
import * as React from 'react'; import PropTypes from 'prop-types'; import { useTheme, styled, useThemeProps as useThemProps } from '@material-ui/core/styles'; import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled'; import clsx from 'clsx'; import PickersYear from './PickersYear'; import { useUtils, useNow } from '../internal/pickers/hooks/useUtils'; import { findClosestEnabledDate } from '../internal/pickers/date-utils'; import { WrapperVariantContext } from '../internal/pickers/wrappers/WrapperVariantContext'; import { getYearPickerUtilityClass } from './yearPickerClasses'; import { jsx as _jsx } from "react/jsx-runtime"; const useUtilityClasses = styleProps => { const { classes } = styleProps; const slots = { root: ['root'] }; return composeClasses(slots, getYearPickerUtilityClass, classes); }; const YearPickerRoot = styled('div', { name: 'MuiYearPicker', slot: 'Root', overridesResolver: (props, styles) => styles.root })({ display: 'flex', flexDirection: 'row', flexWrap: 'wrap', overflowY: 'auto', height: '100%', margin: '0 4px' }); const YearPicker = /*#__PURE__*/React.forwardRef(function YearPicker(inProps, ref) { const props = useThemProps({ props: inProps, name: 'MuiYearPicker' }); const { autoFocus, className, date, disableFuture, disablePast, isDateDisabled, maxDate, minDate, onChange, onFocusedDayChange, onYearChange, shouldDisableYear } = props; const styleProps = props; const classes = useUtilityClasses(styleProps); const now = useNow(); const theme = useTheme(); const utils = useUtils(); const selectedDate = date || now; const currentYear = utils.getYear(selectedDate); const wrapperVariant = React.useContext(WrapperVariantContext); const selectedYearRef = React.useRef(null); const [focusedYear, setFocusedYear] = React.useState(currentYear); const handleYearSelection = (event, year, isFinish = 'finish') => { const submitDate = newDate => { onChange(newDate, isFinish); if (onFocusedDayChange) { onFocusedDayChange(newDate || now); } if (onYearChange) { onYearChange(newDate); } }; const newDate = utils.setYear(selectedDate, year); if (isDateDisabled(newDate)) { const closestEnabledDate = findClosestEnabledDate({ utils, date: newDate, minDate, maxDate, disablePast: Boolean(disablePast), disableFuture: Boolean(disableFuture), shouldDisableDate: isDateDisabled }); submitDate(closestEnabledDate || now); } else { submitDate(newDate); } }; const focusYear = React.useCallback(year => { if (!isDateDisabled(utils.setYear(selectedDate, year))) { setFocusedYear(year); } }, [selectedDate, isDateDisabled, utils]); const yearsInRow = wrapperVariant === 'desktop' ? 4 : 3; const handleKeyDown = (event, year) => { switch (event.key) { case 'ArrowUp': focusYear(year - yearsInRow); event.preventDefault(); break; case 'ArrowDown': focusYear(year + yearsInRow); event.preventDefault(); break; case 'ArrowLeft': focusYear(year + (theme.direction === 'ltr' ? -1 : 1)); event.preventDefault(); break; case 'ArrowRight': focusYear(year + (theme.direction === 'ltr' ? 1 : -1)); event.preventDefault(); break; default: break; } }; return /*#__PURE__*/_jsx(YearPickerRoot, { ref: ref, className: clsx(classes.root, className), styleProps: styleProps, children: utils.getYearRange(minDate, maxDate).map(year => { const yearNumber = utils.getYear(year); const selected = yearNumber === currentYear; return /*#__PURE__*/_jsx(PickersYear, { selected: selected, value: yearNumber, onClick: handleYearSelection, onKeyDown: handleKeyDown, autoFocus: autoFocus && yearNumber === focusedYear, ref: selected ? selectedYearRef : undefined, disabled: disablePast && utils.isBeforeYear(year, now) || disableFuture && utils.isAfterYear(year, now) || shouldDisableYear && shouldDisableYear(year), children: utils.format(year, 'year') }, utils.format(year, 'year')); }) }); }); process.env.NODE_ENV !== "production" ? YearPicker.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * @ignore */ autoFocus: PropTypes.bool, /** * @ignore */ classes: PropTypes.object, /** * @ignore */ className: PropTypes.string, /** * @ignore */ date: PropTypes.any, /** * @ignore */ disableFuture: PropTypes.bool, /** * @ignore */ disablePast: PropTypes.bool, /** * @ignore */ isDateDisabled: PropTypes.func.isRequired, /** * @ignore */ maxDate: PropTypes.any.isRequired, /** * @ignore */ minDate: PropTypes.any.isRequired, /** * @ignore */ onChange: PropTypes.func.isRequired, /** * @ignore */ onFocusedDayChange: PropTypes.func, /** * Callback firing on year change @DateIOType. */ onYearChange: PropTypes.func, /** * Disable specific years dynamically. * Works like `shouldDisableDate` but for year selection view @DateIOType. */ shouldDisableYear: PropTypes.func } : void 0; /** * * Demos: * * - [Date Picker](https://material-ui.com/components/date-picker/) * * API: * * - [YearPicker API](https://material-ui.com/api/year-picker/) */ export default YearPicker;