/** * @file Options Overlay * @desc 下拉选择浮层的内容 */ import React, { useCallback, Key, Ref, createContext, useContext, ProviderProps } from 'react' import cns from 'classnames' import { ComponentSize, useLangData } from '../ConfigProvider' import { CheckIcon } from '../icons' import { Loading } from '../Loading' import { Menu, MenuItem, MenuItemGroup } from '../Menu' import Tabs, { TabPane } from '../Tabs' import { concat, getText } from '../utils' import { OptionInfo, OptionGroupInfo, OptionItem, OptionItems, isGroup, toggle, OptionGroupStyle, useSelectPrefix, Value } from './common' import { CheckAll, Checkbox, CheckboxGroup } from '../Checkbox' export interface OptionsOverlayCtxValue { selected: T[] onSelect: (optionValue: T[]) => void multiple: boolean size: ComponentSize } /** 给 OptionsOverlay 传递信息的 context,包含当前选中值及选中操作的回调等信息 */ const optionsOverlayCtx = createContext>({ selected: [], onSelect: () => undefined, multiple: false, size: 'default' }) export function useOptionsOverlayCtx() { return useContext(optionsOverlayCtx) as unknown as OptionsOverlayCtxValue } export function OptionsOverlayProvider({ value, children }: ProviderProps>) { return {children} } /** 没有可选项时的占位内容 */ export function Empty() { const prefix = useSelectPrefix() const { emptyText } = useLangData('select') return
{emptyText}
} /** items 中是否有可选项 */ export function hasAvailableOption(items: Array>) { return items.some(item => ( !isGroup(item) || item.options.length > 0 )) } /** 全选 Option 项的内部 key */ const selectAllOptionKey: Value = '__icecream_select_all' /** 渲染 option 项的组件 */ function RealOption({ value, disabled, content, rootHtmlProps }: Omit, 'key'>) { const { selected, onSelect, multiple } = useOptionsOverlayCtx() const active = selected.includes(value) const prefix = useSelectPrefix() const optionClassName = concat(prefix, 'option') const handleClick = useCallback(() => { onSelect(multiple ? toggle(selected, value) : [value]) }, [value, selected, onSelect, multiple]) const checkedIcon = active && multiple && ( ) return ( {content} {checkedIcon} ) } interface RealOptionGroupProps { title: string options: Array> } /** 渲染 option 组的组件 */ function RealOptionGroup({ title, options }: RealOptionGroupProps) { const optionsView = options.map(option => ( key={option.value as unknown as Key} {...option} /> )) return ( {optionsView} ) } type OptionsProps = { options: Array> } /** 在 Overlay 中渲染 `