ant-design/components/table/SelectionCheckboxAll.tsx

241 lines
5.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2019-08-05 18:38:10 +08:00
import classNames from 'classnames';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
import { Down } from '@ant-design/icons';
import Checkbox, { CheckboxChangeEvent } from '../checkbox';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
import Dropdown from '../dropdown';
import Menu from '../menu';
2017-11-21 13:48:37 +08:00
import { SelectionCheckboxAllProps, SelectionCheckboxAllState, SelectionItem } from './interface';
function checkSelection<T>({
store,
getCheckboxPropsByItem,
getRecordKey,
data,
type,
byDefaultChecked,
}: {
store: SelectionCheckboxAllProps<T>['store'];
getCheckboxPropsByItem: SelectionCheckboxAllProps<T>['getCheckboxPropsByItem'];
getRecordKey: SelectionCheckboxAllProps<T>['getRecordKey'];
data: T[];
type: 'every' | 'some';
byDefaultChecked: boolean;
}) {
return byDefaultChecked
? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked)
2019-10-16 10:44:56 +08:00
: data[type]((item, i) => store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0);
}
function getIndeterminateState<T>(props: SelectionCheckboxAllProps<T>) {
const { store, data } = props;
if (!data.length) {
return false;
}
const someCheckedNotByDefaultChecked =
checkSelection<T>({
...props,
data,
type: 'some',
byDefaultChecked: false,
}) &&
!checkSelection<T>({
...props,
data,
type: 'every',
byDefaultChecked: false,
});
const someCheckedByDefaultChecked =
checkSelection<T>({
...props,
data,
type: 'some',
byDefaultChecked: true,
}) &&
!checkSelection<T>({
...props,
data,
type: 'every',
byDefaultChecked: true,
});
if (store.getState().selectionDirty) {
return someCheckedNotByDefaultChecked;
}
return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked;
}
function getCheckState<T>(props: SelectionCheckboxAllProps<T>) {
const { store, data } = props;
if (!data.length) {
return false;
2019-08-14 19:38:01 +08:00
}
if (store.getState().selectionDirty) {
return checkSelection({
...props,
data,
type: 'every',
byDefaultChecked: false,
});
}
return (
checkSelection({
...props,
data,
type: 'every',
byDefaultChecked: false,
}) ||
checkSelection({
...props,
data,
type: 'every',
byDefaultChecked: true,
})
);
}
class SelectionCheckboxAll<T> extends React.Component<
2018-12-07 16:17:45 +08:00
SelectionCheckboxAllProps<T>,
SelectionCheckboxAllState
> {
state = {
checked: false,
indeterminate: false,
};
unsubscribe: () => void;
2019-08-05 18:38:10 +08:00
2017-11-21 13:48:37 +08:00
defaultSelections: SelectionItem[];
2017-11-21 13:48:37 +08:00
constructor(props: SelectionCheckboxAllProps<T>) {
super(props);
2018-12-07 16:17:45 +08:00
this.defaultSelections = props.hideDefaultSelections
? []
: [
{
key: 'all',
text: props.locale.selectAll,
},
{
key: 'invert',
text: props.locale.selectInvert,
},
];
}
static getDerivedStateFromProps<T>(
props: SelectionCheckboxAllProps<T>,
state: SelectionCheckboxAllState,
) {
const checked = getCheckState(props);
const indeterminate = getIndeterminateState(props);
const newState: SelectionCheckboxAllState = {};
if (indeterminate !== state.indeterminate) {
newState.indeterminate = indeterminate;
}
if (checked !== state.checked) {
newState.checked = checked;
}
return newState;
}
componentDidMount() {
this.subscribe();
}
componentWillUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
2017-11-21 13:48:37 +08:00
setCheckState(props: SelectionCheckboxAllProps<T>) {
const checked = getCheckState(props);
const indeterminate = getIndeterminateState(props);
2018-12-07 16:17:45 +08:00
this.setState(prevState => {
2018-11-10 21:40:21 +08:00
const newState: SelectionCheckboxAllState = {};
if (indeterminate !== prevState.indeterminate) {
newState.indeterminate = indeterminate;
}
if (checked !== prevState.checked) {
newState.checked = checked;
}
return newState;
});
}
handleSelectAllChange = (e: CheckboxChangeEvent) => {
2019-08-05 18:38:10 +08:00
const { checked } = e.target;
this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
2018-12-07 16:17:45 +08:00
};
2019-08-05 18:38:10 +08:00
subscribe() {
const { store } = this.props;
this.unsubscribe = store.subscribe(() => {
this.setCheckState(this.props);
});
}
2017-11-21 13:48:37 +08:00
renderMenus(selections: SelectionItem[]) {
return selections.map((selection, index) => {
return (
2018-12-07 16:17:45 +08:00
<Menu.Item key={selection.key || index}>
<div
2018-12-07 16:17:45 +08:00
onClick={() => {
this.props.onSelect(selection.key, index, selection.onSelect);
}}
>
{selection.text}
</div>
</Menu.Item>
);
});
}
render() {
const { disabled, prefixCls, selections, getPopupContainer } = this.props;
const { checked, indeterminate } = this.state;
2018-11-10 21:40:21 +08:00
const selectionPrefixCls = `${prefixCls}-selection`;
let customSelections: React.ReactNode = null;
if (selections) {
2018-12-07 16:17:45 +08:00
const newSelections = Array.isArray(selections)
? this.defaultSelections.concat(selections)
: this.defaultSelections;
const menu = (
2018-12-07 16:17:45 +08:00
<Menu className={`${selectionPrefixCls}-menu`} selectedKeys={[]}>
{this.renderMenus(newSelections)}
</Menu>
);
2018-12-07 16:17:45 +08:00
customSelections =
newSelections.length > 0 ? (
<Dropdown overlay={menu} getPopupContainer={getPopupContainer}>
<div className={`${selectionPrefixCls}-down`}>
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
<Down />
2018-12-07 16:17:45 +08:00
</div>
</Dropdown>
) : null;
}
return (
<div className={selectionPrefixCls}>
<Checkbox
className={classNames({ [`${selectionPrefixCls}-select-all-custom`]: customSelections })}
checked={checked}
indeterminate={indeterminate}
disabled={disabled}
onChange={this.handleSelectAllChange}
/>
{customSelections}
</div>
);
}
}
export default SelectionCheckboxAll;