UNPKG

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