import React from 'react'; import { OptionsControl, OptionsControlProps, Option, FormOptionsControl } from './Options'; import cx from 'classnames'; import Checkbox from '../../components/Checkbox'; import chunk from 'lodash/chunk'; import {Icon} from '../../components/icons'; import {Api} from '../../types'; import {autobind, hasAbility} from '../../utils/helper'; /** * 复选框 * 文档:https://baidu.gitee.io/amis/docs/components/form/checkboxes */ export interface CheckboxesControlSchema extends FormOptionsControl { type: 'checkboxes'; /** * 是否开启全选功能 */ checkAll?: boolean; /** * 是否默认全选 */ defaultCheckAll?: boolean; /** * 每行显示多少个 */ columnsCount?: number; } export interface CheckboxesProps extends OptionsControlProps, Omit< CheckboxesControlSchema, | 'options' | 'type' | 'className' | 'descriptionClassName' | 'inputClassName' > { placeholder?: any; itemClassName?: string; columnsCount?: number; labelClassName?: string; onAdd?: () => void; addApi?: Api; creatable: boolean; createBtnLabel: string; editable?: boolean; removable?: boolean; } export default class CheckboxesControl extends React.Component< CheckboxesProps, any > { static defaultProps = { columnsCount: 1, multiple: true, placeholder: 'placeholder.noOption', creatable: false, inline: true, createBtnLabel: 'Select.createLabel' }; componentDidMount() { const {defaultCheckAll, onToggleAll} = this.props; defaultCheckAll && onToggleAll(); } componentDidUpdate(prevProps: OptionsControlProps) { let {options: currOptions, onToggleAll, defaultCheckAll} = this.props; let {options: prevOptions} = prevProps; if (defaultCheckAll && prevOptions != currOptions) { onToggleAll(); } } reload() { const reload = this.props.reloadOptions; reload && reload(); } @autobind handleAddClick() { const {onAdd} = this.props; onAdd && onAdd(); } @autobind handleEditClick(e: Event, item: any) { const {onEdit} = this.props; e.preventDefault(); e.stopPropagation(); onEdit && onEdit(item); } @autobind handleDeleteClick(e: Event, item: any) { const {onDelete} = this.props; e.preventDefault(); e.stopPropagation(); onDelete && onDelete(item); } renderGroup(option: Option, index: number) { const {classnames: cx, labelField} = this.props; if (!option.children?.length) { return null; } return (
{option.children.map((option, index) => this.renderItem(option, index))}
); } renderItem(option: Option, index: number) { if (option.children) { return this.renderGroup(option, index); } const { itemClassName, onToggle, selectedOptions, disabled, inline, labelClassName, labelField, removable, editable, translate: __ } = this.props; return ( onToggle(option)} checked={!!~selectedOptions.indexOf(option)} disabled={disabled || option.disabled} inline={inline} labelClassName={labelClassName} description={option.description} > {String(option[labelField || 'label'])} {removable && hasAbility(option, 'removable') ? ( this.handleDeleteClick(e, option)} /> ) : null} {editable && hasAbility(option, 'editable') ? ( this.handleEditClick(e, option)} /> ) : null} ); } render() { const { className, disabled, placeholder, options, inline, columnsCount, selectedOptions, onToggle, onToggleAll, checkAll, classnames: cx, itemClassName, labelClassName, creatable, addApi, createBtnLabel, translate: __ } = this.props; let body: Array = []; if (options && options.length) { body = options.map((option, key) => this.renderItem(option, key)); } if (checkAll && body.length) { body.unshift( 全选/不选 ); } if ((columnsCount as number) > 1) { let weight = 12 / (columnsCount as number); let cellClassName = `Grid-col--sm${ weight === Math.round(weight) ? weight : '' }`; body = chunk(body, columnsCount).map((group, groupIndex) => (
{Array.from({length: columnsCount as number}).map((_, index) => (
{group[index]}
))}
)); } return (
{body && body.length ? ( body ) : ( {__(placeholder)} )} {(creatable || addApi) && !disabled ? ( {__(createBtnLabel)} ) : null}
); } } @OptionsControl({ type: 'checkboxes', sizeMutable: false }) export class CheckboxesControlRenderer extends CheckboxesControl {}