'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage"; var _span; const _excluded = ["aria-describedby", "aria-label", "autoFocus", "autoWidth", "children", "className", "defaultOpen", "defaultValue", "disabled", "displayEmpty", "error", "IconComponent", "inputRef", "labelId", "MenuProps", "multiple", "name", "onBlur", "onChange", "onClose", "onFocus", "onOpen", "open", "readOnly", "renderValue", "SelectDisplayProps", "tabIndex", "type", "value", "variant"]; import * as React from 'react'; import { isFragment } from 'react-is'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import composeClasses from '@mui/utils/composeClasses'; import useId from '@mui/utils/useId'; import refType from '@mui/utils/refType'; import ownerDocument from '../utils/ownerDocument'; import capitalize from '../utils/capitalize'; import Menu from '../Menu/Menu'; import { nativeSelectSelectStyles, nativeSelectIconStyles } from '../NativeSelect/NativeSelectInput'; import { isFilled } from '../InputBase/utils'; import styled, { slotShouldForwardProp } from '../styles/styled'; import useForkRef from '../utils/useForkRef'; import useControlled from '../utils/useControlled'; import selectClasses, { getSelectUtilityClasses } from './selectClasses'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; const SelectSelect = styled('div', { name: 'MuiSelect', slot: 'Select', overridesResolver: (props, styles) => { const { ownerState } = props; return [ // Win specificity over the input base { [`&.${selectClasses.select}`]: styles.select }, { [`&.${selectClasses.select}`]: styles[ownerState.variant] }, { [`&.${selectClasses.error}`]: styles.error }, { [`&.${selectClasses.multiple}`]: styles.multiple }]; } })(nativeSelectSelectStyles, { // Win specificity over the input base [`&.${selectClasses.select}`]: { height: 'auto', // Resets for multiple select with chips minHeight: '1.4375em', // Required for select\text-field height consistency textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden' } }); const SelectIcon = styled('svg', { name: 'MuiSelect', slot: 'Icon', overridesResolver: (props, styles) => { const { ownerState } = props; return [styles.icon, ownerState.variant && styles[`icon${capitalize(ownerState.variant)}`], ownerState.open && styles.iconOpen]; } })(nativeSelectIconStyles); const SelectNativeInput = styled('input', { shouldForwardProp: prop => slotShouldForwardProp(prop) && prop !== 'classes', name: 'MuiSelect', slot: 'NativeInput', overridesResolver: (props, styles) => styles.nativeInput })({ bottom: 0, left: 0, position: 'absolute', opacity: 0, pointerEvents: 'none', width: '100%', boxSizing: 'border-box' }); function areEqualValues(a, b) { if (typeof b === 'object' && b !== null) { return a === b; } // The value could be a number, the DOM will stringify it anyway. return String(a) === String(b); } function isEmpty(display) { return display == null || typeof display === 'string' && !display.trim(); } const useUtilityClasses = ownerState => { const { classes, variant, disabled, multiple, open, error } = ownerState; const slots = { select: ['select', variant, disabled && 'disabled', multiple && 'multiple', error && 'error'], icon: ['icon', `icon${capitalize(variant)}`, open && 'iconOpen', disabled && 'disabled'], nativeInput: ['nativeInput'] }; return composeClasses(slots, getSelectUtilityClasses, classes); }; /** * @ignore - internal component. */ const SelectInput = /*#__PURE__*/React.forwardRef(function SelectInput(props, ref) { const { 'aria-describedby': ariaDescribedby, 'aria-label': ariaLabel, autoFocus, autoWidth, children, className, defaultOpen, defaultValue, disabled, displayEmpty, error = false, IconComponent, inputRef: inputRefProp, labelId, MenuProps = {}, multiple, name, onBlur, onChange, onClose, onFocus, onOpen, open: openProp, readOnly, renderValue, SelectDisplayProps = {}, tabIndex: tabIndexProp // catching `type` from Input which makes no sense for SelectInput , value: valueProp, variant = 'standard' } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const [value, setValueState] = useControlled({ controlled: valueProp, default: defaultValue, name: 'Select' }); const [openState, setOpenState] = useControlled({ controlled: openProp, default: defaultOpen, name: 'Select' }); const inputRef = React.useRef(null); const displayRef = React.useRef(null); const [displayNode, setDisplayNode] = React.useState(null); const { current: isOpenControlled } = React.useRef(openProp != null); const [menuMinWidthState, setMenuMinWidthState] = React.useState(); const handleRef = useForkRef(ref, inputRefProp); const handleDisplayRef = React.useCallback(node => { displayRef.current = node; if (node) { setDisplayNode(node); } }, []); const anchorElement = displayNode?.parentNode; React.useImperativeHandle(handleRef, () => ({ focus: () => { displayRef.current.focus(); }, node: inputRef.current, value }), [value]); // Resize menu on `defaultOpen` automatic toggle. React.useEffect(() => { if (defaultOpen && openState && displayNode && !isOpenControlled) { setMenuMinWidthState(autoWidth ? null : anchorElement.clientWidth); displayRef.current.focus(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [displayNode, autoWidth]); // `isOpenControlled` is ignored because the component should never switch between controlled and uncontrolled modes. // `defaultOpen` and `openState` are ignored to avoid unnecessary callbacks. React.useEffect(() => { if (autoFocus) { displayRef.current.focus(); } }, [autoFocus]); React.useEffect(() => { if (!labelId) { return undefined; } const label = ownerDocument(displayRef.current).getElementById(labelId); if (label) { const handler = () => { if (getSelection().isCollapsed) { displayRef.current.focus(); } }; label.addEventListener('click', handler); return () => { label.removeEventListener('click', handler); }; } return undefined; }, [labelId]); const update = (open, event) => { if (open) { if (onOpen) { onOpen(event); } } else if (onClose) { onClose(event); } if (!isOpenControlled) { setMenuMinWidthState(autoWidth ? null : anchorElement.clientWidth); setOpenState(open); } }; const handleMouseDown = event => { // Ignore everything but left-click if (event.button !== 0) { return; } // Hijack the default focus behavior. event.preventDefault(); displayRef.current.focus(); update(true, event); }; const handleClose = event => { update(false, event); }; const childrenArray = React.Children.toArray(children); // Support autofill. const handleChange = event => { const child = childrenArray.find(childItem => childItem.props.value === event.target.value); if (child === undefined) { return; } setValueState(child.props.value); if (onChange) { onChange(event, child); } }; const handleItemClick = child => event => { let newValue; // We use the tabindex attribute to signal the available options. if (!event.currentTarget.hasAttribute('tabindex')) { return; } if (multiple) { newValue = Array.isArray(value) ? value.slice() : []; const itemIndex = value.indexOf(child.props.value); if (itemIndex === -1) { newValue.push(child.props.value); } else { newValue.splice(itemIndex, 1); } } else { newValue = child.props.value; } if (child.props.onClick) { child.props.onClick(event); } if (value !== newValue) { setValueState(newValue); if (onChange) { // Redefine target to allow name and value to be read. // This allows seamless integration with the most popular form libraries. // https://github.com/mui/material-ui/issues/13485#issuecomment-676048492 // Clone the event to not override `target` of the original event. const nativeEvent = event.nativeEvent || event; const clonedEvent = new nativeEvent.constructor(nativeEvent.type, nativeEvent); Object.defineProperty(clonedEvent, 'target', { writable: true, value: { value: newValue, name } }); onChange(clonedEvent, child); } } if (!multiple) { update(false, event); } }; const handleKeyDown = event => { if (!readOnly) { const validKeys = [' ', 'ArrowUp', 'ArrowDown', // The native select doesn't respond to enter on macOS, but it's recommended by // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ 'Enter']; if (validKeys.indexOf(event.key) !== -1) { event.preventDefault(); update(true, event); } } }; const open = displayNode !== null && openState; const handleBlur = event => { // if open event.stopImmediatePropagation if (!open && onBlur) { // Preact support, target is read only property on a native event. Object.defineProperty(event, 'target', { writable: true, value: { value, name } }); onBlur(event); } }; delete other['aria-invalid']; let display; let displaySingle; const displayMultiple = []; let computeDisplay = false; let foundMatch = false; // No need to display any value if the field is empty. if (isFilled({ value }) || displayEmpty) { if (renderValue) { display = renderValue(value); } else { computeDisplay = true; } } const items = childrenArray.map(child => { if (! /*#__PURE__*/React.isValidElement(child)) { return null; } if (process.env.NODE_ENV !== 'production') { if (isFragment(child)) { console.error(["MUI: The Select component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n')); } } let selected; if (multiple) { if (!Array.isArray(value)) { throw new Error(process.env.NODE_ENV !== "production" ? `MUI: The \`value\` prop must be an array when using the \`Select\` component with \`multiple\`.` : _formatMuiErrorMessage(2)); } selected = value.some(v => areEqualValues(v, child.props.value)); if (selected && computeDisplay) { displayMultiple.push(child.props.children); } } else { selected = areEqualValues(value, child.props.value); if (selected && computeDisplay) { displaySingle = child.props.children; } } if (selected) { foundMatch = true; } return /*#__PURE__*/React.cloneElement(child, { 'aria-selected': selected ? 'true' : 'false', onClick: handleItemClick(child), onKeyUp: event => { if (event.key === ' ') { // otherwise our MenuItems dispatches a click event // it's not behavior of the native