UNPKG

3.95 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3const _excluded = ["children", "disabled", "label", "slotProps", "slots", "value"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import { unstable_useForkRef as useForkRef } from '@mui/utils';
7import composeClasses from '../composeClasses';
8import { getOptionUtilityClass } from './optionClasses';
9import { useSlotProps } from '../utils';
10import useOption from '../useOption';
11import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
12import { jsx as _jsx } from "react/jsx-runtime";
13function useUtilityClasses(ownerState) {
14 const {
15 disabled,
16 highlighted,
17 selected
18 } = ownerState;
19 const slots = {
20 root: ['root', disabled && 'disabled', highlighted && 'highlighted', selected && 'selected']
21 };
22 return composeClasses(slots, useClassNamesOverride(getOptionUtilityClass));
23}
24
25/**
26 * An unstyled option to be used within a Select.
27 */
28const Option = /*#__PURE__*/React.forwardRef(function Option(props, forwardedRef) {
29 var _slots$root, _optionRef$current;
30 const {
31 children,
32 disabled = false,
33 label,
34 slotProps = {},
35 slots = {},
36 value
37 } = props,
38 other = _objectWithoutPropertiesLoose(props, _excluded);
39 const Root = (_slots$root = slots.root) != null ? _slots$root : 'li';
40 const optionRef = React.useRef(null);
41 const combinedRef = useForkRef(optionRef, forwardedRef);
42
43 // If `label` is not explicitly provided, the `children` are used for convenience.
44 // This is used to populate the select's trigger with the selected option's label.
45 const computedLabel = label != null ? label : typeof children === 'string' ? children : (_optionRef$current = optionRef.current) == null ? void 0 : _optionRef$current.innerText;
46 const {
47 getRootProps,
48 selected,
49 highlighted,
50 index
51 } = useOption({
52 disabled,
53 label: computedLabel,
54 rootRef: combinedRef,
55 value
56 });
57 const ownerState = _extends({}, props, {
58 disabled,
59 highlighted,
60 index,
61 selected
62 });
63 const classes = useUtilityClasses(ownerState);
64 const rootProps = useSlotProps({
65 getSlotProps: getRootProps,
66 elementType: Root,
67 externalSlotProps: slotProps.root,
68 externalForwardedProps: other,
69 className: classes.root,
70 ownerState
71 });
72 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
73 children: children
74 }));
75});
76process.env.NODE_ENV !== "production" ? Option.propTypes /* remove-proptypes */ = {
77 // ----------------------------- Warning --------------------------------
78 // | These PropTypes are generated from the TypeScript type definitions |
79 // | To update them edit TypeScript types and run "yarn proptypes" |
80 // ----------------------------------------------------------------------
81 /**
82 * @ignore
83 */
84 children: PropTypes.node,
85 /**
86 * If `true`, the option will be disabled.
87 * @default false
88 */
89 disabled: PropTypes.bool,
90 /**
91 * A text representation of the option's content.
92 * Used for keyboard text navigation matching.
93 */
94 label: PropTypes.string,
95 /**
96 * The props used for each slot inside the Option.
97 * @default {}
98 */
99 slotProps: PropTypes.shape({
100 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
101 }),
102 /**
103 * The components used for each slot inside the Option.
104 * Either a string to use a HTML element or a component.
105 * @default {}
106 */
107 slots: PropTypes.shape({
108 root: PropTypes.elementType
109 }),
110 /**
111 * The value of the option.
112 */
113 value: PropTypes.any.isRequired
114} : void 0;
115
116/**
117 * An unstyled option to be used within a Select.
118 *
119 * Demos:
120 *
121 * - [Select](https://mui.com/base/react-select/)
122 *
123 * API:
124 *
125 * - [Option API](https://mui.com/base/react-select/components-api/#option)
126 */
127export default /*#__PURE__*/React.memo(Option);
\No newline at end of file