UNPKG

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