UNPKG

@material-ui/lab

Version:
397 lines (346 loc) 13.3 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import ButtonBase from '@material-ui/core/ButtonBase'; import { unstable_useEnhancedEffect as useEnhancedEffect } from '@material-ui/utils'; import { unstable_composeClasses as composeClasses, generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled'; import { useTheme, alpha, styled, useThemeProps } from '@material-ui/core/styles'; import { useForkRef } from '@material-ui/core/utils'; import { useUtils } from '../internal/pickers/hooks/useUtils'; import { DAY_SIZE, DAY_MARGIN } from '../internal/pickers/constants/dimensions'; import { jsx as _jsx } from "react/jsx-runtime"; export function getPickersDayUtilityClass(slot) { return generateUtilityClass('MuiPickersDay', slot); } export var pickersDayClasses = generateUtilityClasses('MuiPickersDay', ['root', 'dayWithMargin', 'dayOutsideMonth', 'hiddenDaySpacingFiller', 'today', 'selected', 'disabled']); var useUtilityClasses = function useUtilityClasses(styleProps) { var selected = styleProps.selected, disableMargin = styleProps.disableMargin, disableHighlightToday = styleProps.disableHighlightToday, today = styleProps.today, outsideCurrentMonth = styleProps.outsideCurrentMonth, showDaysOutsideCurrentMonth = styleProps.showDaysOutsideCurrentMonth, classes = styleProps.classes; var slots = { root: ['root', selected && 'selected', !disableMargin && 'dayWithMargin', !disableHighlightToday && today && 'today', outsideCurrentMonth && showDaysOutsideCurrentMonth && 'dayOutsideMonth'], hiddenDaySpacingFiller: ['hiddenDaySpacingFiller'] }; return composeClasses(slots, getPickersDayUtilityClass, classes); }; var styleArg = function styleArg(_ref) { var _extends2; var theme = _ref.theme, styleProps = _ref.styleProps; return _extends({}, theme.typography.caption, (_extends2 = { width: DAY_SIZE, height: DAY_SIZE, borderRadius: '50%', padding: 0, // background required here to prevent collides with the other days when animating with transition group backgroundColor: theme.palette.background.paper, color: theme.palette.text.primary, '&:hover': { backgroundColor: alpha(theme.palette.action.active, theme.palette.action.hoverOpacity) }, '&:focus': _defineProperty({ backgroundColor: alpha(theme.palette.action.active, theme.palette.action.hoverOpacity) }, "&.".concat(pickersDayClasses.selected), { willChange: 'background-color', backgroundColor: theme.palette.primary.dark }) }, _defineProperty(_extends2, "&.".concat(pickersDayClasses.selected), { color: theme.palette.primary.contrastText, backgroundColor: theme.palette.primary.main, fontWeight: theme.typography.fontWeightMedium, transition: theme.transitions.create('background-color', { duration: theme.transitions.duration.short }), '&:hover': { willChange: 'background-color', backgroundColor: theme.palette.primary.dark } }), _defineProperty(_extends2, "&.".concat(pickersDayClasses.disabled), { color: theme.palette.text.disabled }), _extends2), !styleProps.disableMargin && { margin: "0 ".concat(DAY_MARGIN, "px") }, styleProps.outsideCurrentMonth && styleProps.showDaysOutsideCurrentMonth && { color: theme.palette.text.secondary }, !styleProps.disableHighlightToday && styleProps.today && _defineProperty({}, "&:not(.".concat(pickersDayClasses.selected, ")"), { border: "1px solid ".concat(theme.palette.text.secondary) })); }; var overridesResolver = function overridesResolver(props, styles) { var styleProps = props.styleProps; return [styles.root, !styleProps.disableMargin && styles.dayWithMargin, !styleProps.disableHighlightToday && styleProps.today && styles.today, !styleProps.outsideCurrentMonth && styleProps.showDaysOutsideCurrentMonth && styles.dayOutsideMonth, styleProps.outsideCurrentMonth && !styleProps.showDaysOutsideCurrentMonth && styles.hiddenDaySpacingFiller]; }; var PickersDayRoot = styled(ButtonBase, { name: 'MuiPickersDay', slot: 'Root', overridesResolver: overridesResolver })(styleArg); var PickersDayFiller = styled('div', { name: 'MuiPickersDay', slot: 'Root', overridesResolver: overridesResolver })(function (_ref3) { var theme = _ref3.theme, styleProps = _ref3.styleProps; return _extends({}, styleArg({ theme: theme, styleProps: styleProps }), { visibility: 'hidden' }); }); var noop = function noop() {}; var PickersDay = /*#__PURE__*/React.forwardRef(function PickersDay(inProps, forwardedRef) { var props = useThemeProps({ props: inProps, name: 'MuiPickersDay' }); var _props$allowSameDateS = props.allowSameDateSelection, allowSameDateSelection = _props$allowSameDateS === void 0 ? false : _props$allowSameDateS, _props$autoFocus = props.autoFocus, autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus, className = props.className, day = props.day, _props$disabled = props.disabled, disabled = _props$disabled === void 0 ? false : _props$disabled, _props$disableHighlig = props.disableHighlightToday, disableHighlightToday = _props$disableHighlig === void 0 ? false : _props$disableHighlig, _props$disableMargin = props.disableMargin, disableMargin = _props$disableMargin === void 0 ? false : _props$disableMargin, hidden = props.hidden, isAnimating = props.isAnimating, onClick = props.onClick, _props$onDayFocus = props.onDayFocus, onDayFocus = _props$onDayFocus === void 0 ? noop : _props$onDayFocus, onDaySelect = props.onDaySelect, onFocus = props.onFocus, onKeyDown = props.onKeyDown, outsideCurrentMonth = props.outsideCurrentMonth, _props$selected = props.selected, selected = _props$selected === void 0 ? false : _props$selected, _props$showDaysOutsid = props.showDaysOutsideCurrentMonth, showDaysOutsideCurrentMonth = _props$showDaysOutsid === void 0 ? false : _props$showDaysOutsid, children = props.children, _props$today = props.today, isToday = _props$today === void 0 ? false : _props$today, other = _objectWithoutProperties(props, ["allowSameDateSelection", "autoFocus", "className", "day", "disabled", "disableHighlightToday", "disableMargin", "hidden", "isAnimating", "onClick", "onDayFocus", "onDaySelect", "onFocus", "onKeyDown", "outsideCurrentMonth", "selected", "showDaysOutsideCurrentMonth", "children", "today"]); var styleProps = _extends({}, props, { allowSameDateSelection: allowSameDateSelection, autoFocus: autoFocus, disabled: disabled, disableHighlightToday: disableHighlightToday, disableMargin: disableMargin, selected: selected, showDaysOutsideCurrentMonth: showDaysOutsideCurrentMonth, today: isToday }); var classes = useUtilityClasses(styleProps); var utils = useUtils(); var ref = React.useRef(null); var handleRef = useForkRef(ref, forwardedRef); // Since this is rendered when a Popper is opened we can't use passive effects. // Focusing in passive effects in Popper causes scroll jump. useEnhancedEffect(function () { if (autoFocus && !disabled && !isAnimating && !outsideCurrentMonth) { // ref.current being null would be a bug in Material-UI ref.current.focus(); } }, [autoFocus, disabled, isAnimating, outsideCurrentMonth]); var handleFocus = function handleFocus(event) { if (onDayFocus) { onDayFocus(day); } if (onFocus) { onFocus(event); } }; var handleClick = function handleClick(event) { if (!allowSameDateSelection && selected) return; if (!disabled) { onDaySelect(day, 'finish'); } if (onClick) { onClick(event); } }; var theme = useTheme(); function handleKeyDown(event) { if (onKeyDown !== undefined) { onKeyDown(event); } switch (event.key) { case 'ArrowUp': onDayFocus(utils.addDays(day, -7)); event.preventDefault(); break; case 'ArrowDown': onDayFocus(utils.addDays(day, 7)); event.preventDefault(); break; case 'ArrowLeft': onDayFocus(utils.addDays(day, theme.direction === 'ltr' ? -1 : 1)); event.preventDefault(); break; case 'ArrowRight': onDayFocus(utils.addDays(day, theme.direction === 'ltr' ? 1 : -1)); event.preventDefault(); break; case 'Home': onDayFocus(utils.startOfWeek(day)); event.preventDefault(); break; case 'End': onDayFocus(utils.endOfWeek(day)); event.preventDefault(); break; case 'PageUp': onDayFocus(utils.getNextMonth(day)); event.preventDefault(); break; case 'PageDown': onDayFocus(utils.getPreviousMonth(day)); event.preventDefault(); break; default: break; } } if (outsideCurrentMonth && !showDaysOutsideCurrentMonth) { return /*#__PURE__*/_jsx(PickersDayFiller, { className: clsx(classes.root, classes.hiddenDaySpacingFiller, className), styleProps: styleProps }); } return /*#__PURE__*/_jsx(PickersDayRoot, _extends({ className: clsx(classes.root, className), styleProps: styleProps, ref: handleRef, centerRipple: true, disabled: disabled, "aria-label": !children ? utils.format(day, 'fullDate') : undefined, tabIndex: selected ? 0 : -1, onFocus: handleFocus, onKeyDown: handleKeyDown, onClick: handleClick }, other, { children: !children ? utils.format(day, 'dayOfMonth') : children })); }); export var areDayPropsEqual = function areDayPropsEqual(prevProps, nextProps) { return prevProps.autoFocus === nextProps.autoFocus && prevProps.isAnimating === nextProps.isAnimating && prevProps.today === nextProps.today && prevProps.disabled === nextProps.disabled && prevProps.selected === nextProps.selected && prevProps.disableMargin === nextProps.disableMargin && prevProps.showDaysOutsideCurrentMonth === nextProps.showDaysOutsideCurrentMonth && prevProps.disableHighlightToday === nextProps.disableHighlightToday && prevProps.className === nextProps.className && prevProps.outsideCurrentMonth === nextProps.outsideCurrentMonth && prevProps.onDayFocus === nextProps.onDayFocus && prevProps.onDaySelect === nextProps.onDaySelect; }; process.env.NODE_ENV !== "production" ? PickersDay.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * If `true`, `onChange` is fired on click even if the same date is selected. * @default false */ allowSameDateSelection: PropTypes.bool, /** * @ignore */ autoFocus: PropTypes.bool, /** * The content of the component. */ children: PropTypes.node, /** * Override or extend the styles applied to the component. */ classes: PropTypes.object, /** * @ignore */ className: PropTypes.string, /** * The date to show. */ day: PropTypes.any.isRequired, /** * If `true`, renders as disabled. * @default false */ disabled: PropTypes.bool, /** * If `true`, todays date is rendering without highlighting with circle. * @default false */ disableHighlightToday: PropTypes.bool, /** * If `true`, days are rendering without margin. Useful for displaying linked range of days. * @default false */ disableMargin: PropTypes.bool, /** * @ignore */ hidden: PropTypes.bool, /** * @ignore */ isAnimating: PropTypes.bool, /** * @ignore */ onClick: PropTypes.func, /** * @ignore */ onDayFocus: PropTypes.func, /** * @ignore */ onDaySelect: PropTypes.func.isRequired, /** * @ignore */ onFocus: PropTypes.func, /** * @ignore */ onKeyDown: PropTypes.func, /** * If `true`, day is outside of month and will be hidden. */ outsideCurrentMonth: PropTypes.bool.isRequired, /** * If `true`, renders as selected. * @default false */ selected: PropTypes.bool, /** * If `true`, days that have `outsideCurrentMonth={true}` are displayed. * @default false */ showDaysOutsideCurrentMonth: PropTypes.bool, /** * The system prop that allows defining system overrides as well as additional CSS styles. */ sx: PropTypes.object, /** * If `true`, renders as today date. * @default false */ today: PropTypes.bool } : void 0; /** * * Demos: * * - [Date Picker](https://material-ui.com/components/date-picker/) * * API: * * - [PickersDay API](https://material-ui.com/api/pickers-day/) */ export default /*#__PURE__*/React.memo(PickersDay, areDayPropsEqual);