/** * @file Checkboxes * @description 多选输入框 * @author fex */ import React from 'react'; import isEqual from 'lodash/isEqual'; import cx from 'classnames'; import { uncontrollable, LocaleProps, localeable, themeable, ThemeProps, autobind, findTree, flattenTree, getOptionValue, getOptionValueBindField } from 'amis-core'; import Checkbox from './Checkbox'; import {Option, Options} from './Select'; export interface BaseSelectionProps extends ThemeProps, LocaleProps { options: Options; className?: string; placeholder?: string; value?: any | Array; multiple?: boolean; clearable?: boolean; labelField?: string; valueField?: string; onChange?: (value: Array | any) => void; onDeferLoad?: (option: Option) => void; onLeftDeferLoad?: (option: Option, leftOptions: Option) => void; inline?: boolean; labelClassName?: string; option2value?: (option: Option) => any; itemClassName?: string; itemHeight?: number; // 每个选项的高度,主要用于虚拟渲染 virtualThreshold?: number; // 数据量多大的时候开启虚拟渲染 virtualListHeight?: number; // 虚拟渲染时,列表高度 itemRender: (option: Option, states: ItemRenderStates) => JSX.Element; disabled?: boolean; onClick?: (e: React.MouseEvent) => void; placeholderRender?: (props: any) => JSX.Element | null; checkAll?: boolean; checkAllLabel?: string; } export interface ItemRenderStates { index: number; labelField?: string; multiple?: boolean; checked: boolean; onChange: () => void; disabled?: boolean; } export class BaseSelection< T extends BaseSelectionProps = BaseSelectionProps, S = any > extends React.Component { static itemRender(option: Option, states: ItemRenderStates) { return ( {option[states?.labelField || 'label']} {option.tip || ''} ); } static defaultProps = { placeholder: 'placeholder.noOption', itemRender: this.itemRender, multiple: true, clearable: false, virtualThreshold: 1000, itemHeight: 32 }; static value2array( value: any, options: Options, option2value: (option: Option) => any = (option: Option) => option, valueField?: string ): Options { if (value === void 0) { return []; } if (!Array.isArray(value)) { value = [value]; } return value.map((value: any) => { const option = findTree( options, option => isEqual(option2value(option), value), valueField ? { value: getOptionValue(value, valueField), resolve: getOptionValueBindField(valueField) } : undefined ); return option || value; }); } static resolveSelected( value: any, options: Options, option2value: (option: Option) => any = (option: Option) => option ) { value = Array.isArray(value) ? value[0] : value; return findTree(options, option => isEqual(option2value(option), value)); } // 获取两个数组的交集 intersectArray( arr1: undefined | Array