import React from 'react'; import type { SelectProps } from 'antd'; import ProField from '@ant-design/pro-field'; import type { ProSchema } from '@ant-design/pro-utils'; import { runFunction } from '@ant-design/pro-utils'; import type { ProFormItemProps } from '../../interface'; import type { ExtendsProps } from '../../BaseForm/createField'; import createField from '../../BaseForm/createField'; export type ProFormSelectProps = ProFormItemProps< SelectProps & { /** * 是否在输入框聚焦时触发搜索 * * @default false */ searchOnFocus?: boolean; /** * 选择完一个之后是否清空搜索项重新搜索 * * @default false */ resetAfterSelect?: boolean; /** 自定义选项渲染 */ optionItemRender?: (item: T) => React.ReactNode; } > & { valueEnum?: ProSchema['valueEnum']; params?: ProSchema['params']; request?: ProSchema['request']; options?: SelectProps['options'] | string[]; mode?: SelectProps['mode'] | 'singe'; showSearch?: SelectProps['showSearch']; readonly?: boolean; }; /** * 选择框 * * @param */ const ProFormSelectComponents = React.forwardRef>( ( { fieldProps, children, params, proFieldProps, mode, valueEnum, request, showSearch, options }, ref, ) => { return ( {children} ); }, ); const SearchSelect = React.forwardRef>( ({ fieldProps, children, params, proFieldProps, mode, valueEnum, request, options }, ref) => { const props: Omit, 'options'> & { options?: ProFormSelectProps['options']; } = { options, mode: (mode as 'multiple') || 'multiple', labelInValue: true, showSearch: true, showArrow: false, autoClearSearchValue: true, optionLabelProp: 'label', filterOption: false, ...fieldProps, }; return ( {children} ); }, ); const ProFormSelect = createField(ProFormSelectComponents, { customLightMode: true, }) as (props: ProFormSelectProps & ExtendsProps) => React.ReactElement; const ProFormSearchSelect = createField(SearchSelect, { customLightMode: true, }) as (props: ProFormSelectProps & ExtendsProps) => React.ReactElement; const WrappedProFormSelect = ProFormSelect as (( props: ProFormSelectProps, ) => React.ReactElement) & { SearchSelect: typeof ProFormSearchSelect; }; WrappedProFormSelect.SearchSelect = ProFormSearchSelect; export default WrappedProFormSelect;