@mui/base
Version:
A library of headless ('unstyled') React UI components and low-level hooks.
127 lines (124 loc) • 3.95 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["children", "disabled", "label", "slotProps", "slots", "value"];
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useForkRef as useForkRef } from '@mui/utils';
import composeClasses from '../composeClasses';
import { getOptionUtilityClass } from './optionClasses';
import { useSlotProps } from '../utils';
import useOption from '../useOption';
import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
import { jsx as _jsx } from "react/jsx-runtime";
function useUtilityClasses(ownerState) {
const {
disabled,
highlighted,
selected
} = ownerState;
const slots = {
root: ['root', disabled && 'disabled', highlighted && 'highlighted', selected && 'selected']
};
return composeClasses(slots, useClassNamesOverride(getOptionUtilityClass));
}
/**
* An unstyled option to be used within a Select.
*/
const Option = /*#__PURE__*/React.forwardRef(function Option(props, forwardedRef) {
var _slots$root, _optionRef$current;
const {
children,
disabled = false,
label,
slotProps = {},
slots = {},
value
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const Root = (_slots$root = slots.root) != null ? _slots$root : 'li';
const optionRef = React.useRef(null);
const combinedRef = useForkRef(optionRef, forwardedRef);
// If `label` is not explicitly provided, the `children` are used for convenience.
// This is used to populate the select's trigger with the selected option's label.
const computedLabel = label != null ? label : typeof children === 'string' ? children : (_optionRef$current = optionRef.current) == null ? void 0 : _optionRef$current.innerText;
const {
getRootProps,
selected,
highlighted,
index
} = useOption({
disabled,
label: computedLabel,
rootRef: combinedRef,
value
});
const ownerState = _extends({}, props, {
disabled,
highlighted,
index,
selected
});
const classes = useUtilityClasses(ownerState);
const rootProps = useSlotProps({
getSlotProps: getRootProps,
elementType: Root,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
className: classes.root,
ownerState
});
return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
children: children
}));
});
process.env.NODE_ENV !== "production" ? Option.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
/**
* If `true`, the option will be disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* A text representation of the option's content.
* Used for keyboard text navigation matching.
*/
label: PropTypes.string,
/**
* The props used for each slot inside the Option.
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}),
/**
* The components used for each slot inside the Option.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType
}),
/**
* The value of the option.
*/
value: PropTypes.any.isRequired
} : void 0;
/**
* An unstyled option to be used within a Select.
*
* Demos:
*
* - [Select](https://mui.com/base/react-select/)
*
* API:
*
* - [Option API](https://mui.com/base/react-select/components-api/#option)
*/
export default /*#__PURE__*/React.memo(Option);