import {BaseCheckboxes, BaseCheckboxesProps} from './Checkboxes'; import {themeable} from '../theme'; import React from 'react'; import {uncontrollable} from 'uncontrollable'; import Checkbox from './Checkbox'; import {Option} from './Select'; import {resolveVariable} from '../utils/tpl-builtin'; import {localeable} from '../locale'; export interface TableCheckboxesProps extends BaseCheckboxesProps { columns: Array<{ name: string; label: string; [propName: string]: any; }>; cellRender: ( column: { name: string; label: string; [propName: string]: any; }, option: Option, colIndex: number, rowIndex: number ) => JSX.Element; } export class TableCheckboxes extends BaseCheckboxes { static defaultProps = { ...BaseCheckboxes.defaultProps, cellRender: ( column: { name: string; label: string; [propName: string]: any; }, option: Option, colIndex: number, rowIndex: number ) => {resolveVariable(column.name, option)} }; getColumns() { let columns = this.props.columns; if (!Array.isArray(columns) || !columns.length) { columns = [{label: 'Label', name: 'label'}]; } return columns; } renderTHead() { const {options, classnames: cx, value, disabled, option2value} = this.props; let columns = this.getColumns(); let valueArray = BaseCheckboxes.value2array(value, options, option2value); const availableOptions = options.filter(option => !option.disabled); let partialChecked = false; let allChecked = !!availableOptions.length; availableOptions.forEach(option => { const isIn = !!~valueArray.indexOf(option); if (isIn && !partialChecked) { partialChecked = true; } else if (!isIn && allChecked) { allChecked = false; } }); return ( {Array.isArray(options) && options.length ? ( ) : null} {columns.map((column, index) => ( {column.label} ))} ); } renderTBody() { const { options, placeholder, classnames: cx, cellRender, value, disabled, option2value, translate: __ } = this.props; const columns = this.getColumns(); let valueArray = BaseCheckboxes.value2array(value, options, option2value); return ( {Array.isArray(options) && options.length ? ( options.map((option, rowIndex) => { const checked = valueArray.indexOf(option) !== -1; return ( e.defaultPrevented || this.toggleOption(option)} > {columns.map((column, colIndex) => ( {cellRender(column, option, colIndex, rowIndex)} ))} ); }) ) : ( {__(placeholder)} )} ); } render() { const { value, options, className, labelClassName, disabled, classnames: cx, option2value, itemClassName, itemRender } = this.props; let valueArray = BaseCheckboxes.value2array(value, options, option2value); let body: Array = []; if (Array.isArray(options) && options.length) { body = options.map((option, key) => (
this.toggleOption(option)} >
{itemRender(option)}
)); } return (
{this.renderTHead()} {this.renderTBody()}
); } } export default themeable( localeable( uncontrollable(TableCheckboxes, { value: 'onChange' }) ) );