UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

74 lines (72 loc) 2.8 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useSelectRootContext } from '../root/SelectRootContext.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { mergeReactProps } from '../../utils/mergeReactProps.js'; /** * A text label of the currently selected item. * Renders a `<span>` element. * * Documentation: [Base UI Select](https://base-ui.com/react/components/select) */ const SelectValue = /*#__PURE__*/React.forwardRef(function SelectValue(props, forwardedRef) { const { className, render, children, placeholder, ...otherProps } = props; const { label, valueRef } = useSelectRootContext(); const mergedRef = useForkRef(forwardedRef, valueRef); const state = React.useMemo(() => ({}), []); const getValueProps = React.useCallback((externalProps = {}) => mergeReactProps(externalProps, { children: typeof children === 'function' ? children(label) : label || placeholder }), [children, label, placeholder]); const { renderElement } = useComponentRenderer({ propGetter: getValueProps, render: render ?? 'span', className, state, ref: mergedRef, extraProps: otherProps }); return renderElement(); }); process.env.NODE_ENV !== "production" ? SelectValue.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ children: PropTypes.func, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * A placeholder value to display when no value is selected. * * You can use this prop to pre-render the displayed text * during SSR in order to avoid the hydration flash. */ placeholder: PropTypes.string, /** * Allows you to replace the component’s HTML element * with a different tag, or compose it with another component. * * Accepts a `ReactElement` or a function that returns the element to render. */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]) } : void 0; export { SelectValue };