/* * Copyright 2024 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import {PopoverProps as AriaPopoverProps} from 'react-aria-components/Popover'; import { Select as AriaSelect, SelectProps as AriaSelectProps, SelectValue } from 'react-aria-components/Select'; import {baseColor, focusRing, size, style} from '../style' with {type: 'macro'}; import {Button} from 'react-aria-components/Button'; import {centerBaseline} from './CenterBaseline'; import {checkmark, description, icon, label, menuitem, sectionHeader, sectionHeading} from './Menu'; import CheckmarkIcon from '../ui-icons/Checkmark'; import ChevronIcon from '../ui-icons/Chevron'; import {ContextValue, DEFAULT_SLOT, Provider} from 'react-aria-components/slots'; import {controlFont, fieldInput, StyleProps} from './style-utils' with {type: 'macro'}; import {css} from '../style/style-macro' with {type: 'macro'}; import {edgeToText} from '../style/spectrum-theme' with {type: 'macro'}; import {FieldLabel} from './Field'; import {FocusableRef, FocusableRefValue, SpectrumLabelableProps} from '@react-types/shared'; import {forwardRefType} from './types'; import {HeaderContext, HeadingContext, Text, TextContext} from './Content'; import {IconContext} from './Icon'; import {ListBox, ListBoxItem, ListBoxItemProps, ListBoxProps} from 'react-aria-components/ListBox'; import {Placement} from 'react-aria-components/Popover'; import {Popover} from './Popover'; import {pressScale} from './pressScale'; import React, {createContext, forwardRef, ReactNode, useContext, useRef} from 'react'; import {useFocusableRef} from './useDOMRef'; import {useFormProps} from './Form'; import {useLocale} from 'react-aria/I18nProvider'; import {useSpectrumContextProps} from './useSpectrumContextProps'; export interface PickerStyleProps {} export interface PickerProps extends Omit, 'children' | 'style' | 'className' | 'render' | 'placeholder'>, PickerStyleProps, StyleProps, SpectrumLabelableProps, Pick, 'items'>, Pick { /** The contents of the collection. */ children: ReactNode | ((item: T) => ReactNode); /** * Direction the menu will render relative to the Picker. * * @default 'bottom' */ direction?: 'bottom' | 'top'; /** * Alignment of the menu relative to the input target. * * @default 'start' */ align?: 'start' | 'end'; /** * Width of the menu. By default, matches width of the trigger. Note that the minimum width of the * dropdown is always equal to the trigger's width. */ menuWidth?: number; /** Density of the tabs, affects the height of the picker. */ density: 'compact' | 'regular'; /** * If the tab picker should only display icon and no text for the button label. * * @default 'show */ labelBehavior?: 'show' | 'hide'; /** Id for the SelectedValue so we can label using it. */ valueId?: string; } export const PickerContext = createContext>, FocusableRefValue>>( null ); const inputButton = style({ ...focusRing(), ...fieldInput(), outlineStyle: { default: 'none', isFocusVisible: 'solid' }, position: 'relative', font: 'ui', display: 'flex', textAlign: 'start', borderStyle: 'none', borderRadius: 'sm', alignItems: 'center', transition: 'default', columnGap: 'text-to-visual', paddingX: 0, backgroundColor: 'transparent', color: { default: baseColor('neutral'), isDisabled: 'disabled' }, maxWidth: { isQuiet: 'max' }, disableTapHighlight: true, height: { default: 48, density: { compact: 32 } }, boxSizing: 'border-box' }); export let menu = style({ outlineStyle: 'none', display: 'grid', gridTemplateColumns: [ edgeToText(32), 'auto', 'auto', 'minmax(0, 1fr)', 'auto', 'auto', 'auto', edgeToText(32) ], boxSizing: 'border-box', width: 'full', maxHeight: 'inherit', overflow: 'auto', padding: 8, fontFamily: 'sans', fontSize: controlFont() }); const valueStyles = style({ flexGrow: 0, truncate: true, display: 'flex', alignItems: 'center', height: 'full' }); const iconStyles = style({ flexShrink: 0, rotate: 90, '--iconPrimary': { type: 'fill', value: 'currentColor' } }); const iconCenterWrapper = style({ display: 'flex', gridArea: 'icon', paddingStart: { labelBehavior: { hide: size(6) } } }); let InsideSelectValueContext = createContext(false); function Picker(props: PickerProps, ref: FocusableRef) { [props, ref] = useSpectrumContextProps(props, ref, PickerContext); let domRef = useFocusableRef(ref); props = useFormProps(props); let { direction = 'bottom', align = 'start', shouldFlip = true, children, items, density, labelBehavior = 'show', valueId, ...pickerProps } = props; let isQuiet = true; const menuOffset: number = 6; const size = 'M'; let ariaLabelledby = props['aria-labelledby'] ?? ''; let {direction: dir} = useLocale(); let RTLFlipOffset = dir === 'rtl' ? -1 : 1; return (
{({isOpen}) => ( <>
{children}
)}
); } /** * Pickers allow users to choose a single option from a collapsible list of options when space is * limited. */ let _Picker = /*#__PURE__*/ (forwardRef as forwardRefType)(Picker); export {_Picker as Picker}; const selectedIndicator = style<{isDisabled?: boolean}>({ backgroundColor: { default: 'neutral', isDisabled: 'disabled', forcedColors: { default: 'Highlight', isDisabled: 'GrayText' } }, height: '[2px]', borderStyle: 'none', borderRadius: 'full', marginTop: '[-2px]', transitionDuration: 130, transitionTimingFunction: 'in-out' }); function TabLine(props: {isDisabled?: boolean}) { return
; } export interface PickerItemProps extends Omit, StyleProps { children: ReactNode; } export function PickerItem(props: PickerItemProps): ReactNode { let ref = useRef(null); let isLink = props.href != null; const size = 'M'; // oxlint-disable react/react-compiler return ( (props.UNSAFE_className || '') + menuitem({...renderProps, size, isLink}, props.styles) }> {renderProps => { let {children} = props; return ( {!isLink && ( )} {typeof children === 'string' ? {children} : children} ); }} ); // oxlint-enable react/react-compiler } // A Context.Provider that only sets a value if not inside SelectValue. function DefaultProvider({ context, value, children }: { context: React.Context; value: any; children: any; }) { let inSelectValue = useContext(InsideSelectValueContext); if (inSelectValue) { return children; } return {children}; }