UNPKG

@material-ui/lab

Version:
116 lines (110 loc) 3.44 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import clsx from 'clsx'; import { useForkRef, capitalize } from '@material-ui/core/utils'; import { alpha, styled } from '@material-ui/core/styles'; import { unstable_composeClasses as composeClasses, generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled'; import { WrapperVariantContext } from '../internal/pickers/wrappers/WrapperVariantContext'; import { jsx as _jsx } from "react/jsx-runtime"; export function getPickersYearUtilityClass(slot) { return generateUtilityClass('PrivatePickersYear', slot); } export const pickersYearClasses = generateUtilityClasses('PrivatePickersYear', ['root', 'modeMobile', 'modeDesktop', 'yearButton', 'disabled', 'selected']); const useUtilityClasses = styleProps => { const { wrapperVariant, disabled, selected, classes } = styleProps; const slots = { root: ['root', wrapperVariant && `mode${capitalize(wrapperVariant)}`], yearButton: ['yearButton', disabled && 'disabled', selected && 'selected'] }; return composeClasses(slots, getPickersYearUtilityClass, classes); }; const PickersYearRoot = styled('div', { skipSx: true })(({ styleProps }) => _extends({ flexBasis: '33.3%', display: 'flex', alignItems: 'center', justifyContent: 'center' }, (styleProps == null ? void 0 : styleProps.wrapperVariant) === 'desktop' && { flexBasis: '25%' })); const PickersYearButton = styled('button', { skipSx: true })(({ theme }) => _extends({ color: 'unset', backgroundColor: 'transparent', border: 0, outline: 0 }, theme.typography.subtitle1, { margin: '8px 0', height: 36, width: 72, borderRadius: 16, cursor: 'pointer', '&:focus, &:hover': { backgroundColor: alpha(theme.palette.action.active, theme.palette.action.hoverOpacity) }, [`&.${pickersYearClasses.disabled}`]: { color: theme.palette.text.secondary }, [`&.${pickersYearClasses.selected}`]: { color: theme.palette.primary.contrastText, backgroundColor: theme.palette.primary.main, '&:focus, &:hover': { backgroundColor: theme.palette.primary.dark } } })); /** * @ignore - internal component. */ const PickersYear = /*#__PURE__*/React.forwardRef(function PickersYear(props, forwardedRef) { const { autoFocus, className, children, disabled, onClick, onKeyDown, selected, value } = props; const ref = React.useRef(null); const refHandle = useForkRef(ref, forwardedRef); const wrapperVariant = React.useContext(WrapperVariantContext); const styleProps = _extends({}, props, { wrapperVariant }); const classes = useUtilityClasses(styleProps); // TODO: Can we just forward this to the button? React.useEffect(() => { if (autoFocus) { // `ref.current` being `null` would be a bug in Material-UIu ref.current.focus(); } }, [autoFocus]); return /*#__PURE__*/_jsx(PickersYearRoot, { className: clsx(classes.root, className), styleProps: styleProps, children: /*#__PURE__*/_jsx(PickersYearButton, { ref: refHandle, disabled: disabled, type: "button", tabIndex: selected ? 0 : -1, onClick: event => onClick(event, value), onKeyDown: event => onKeyDown(event, value), className: classes.yearButton, styleProps: styleProps, children: children }) }); }); export default PickersYear;