mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 19:42:54 +08:00
Fix implicit any error for Table
This commit is contained in:
parent
8260e32d1d
commit
5cb5a2ae9d
@ -7,12 +7,12 @@ import Select from '../select';
|
|||||||
import MiniSelect from './MiniSelect';
|
import MiniSelect from './MiniSelect';
|
||||||
|
|
||||||
export interface PaginationProps {
|
export interface PaginationProps {
|
||||||
total: number;
|
total?: number;
|
||||||
defaultCurrent?: number;
|
defaultCurrent?: number;
|
||||||
current?: number;
|
current?: number;
|
||||||
defaultPageSize?: number;
|
defaultPageSize?: number;
|
||||||
pageSize?: number;
|
pageSize?: number;
|
||||||
onChange?: (page: number, pageSize: number) => void;
|
onChange?: (page: number, pageSize?: number) => void;
|
||||||
showSizeChanger?: boolean;
|
showSizeChanger?: boolean;
|
||||||
pageSizeOptions?: string[];
|
pageSizeOptions?: string[];
|
||||||
onShowSizeChange?: (current: number, size: number) => void;
|
onShowSizeChange?: (current: number, size: number) => void;
|
||||||
|
@ -1,28 +1,4 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { ColumnProps } from './interface';
|
||||||
export interface ColumnProps<T> {
|
|
||||||
title?: React.ReactNode;
|
|
||||||
key?: string;
|
|
||||||
dataIndex?: string;
|
|
||||||
render?: (text: any, record: T, index: number) => React.ReactNode;
|
|
||||||
filters?: { text: string; value: string, children?: any[] }[];
|
|
||||||
onFilter?: (value: any, record: T) => boolean;
|
|
||||||
filterMultiple?: boolean;
|
|
||||||
filterDropdown?: React.ReactNode;
|
|
||||||
filterDropdownVisible?: boolean;
|
|
||||||
onFilterDropdownVisibleChange?: (visible: boolean) => void;
|
|
||||||
sorter?: boolean | ((a: any, b: any) => number);
|
|
||||||
defaultSortOrder?: 'ascend' | 'descend';
|
|
||||||
colSpan?: number;
|
|
||||||
width?: string | number;
|
|
||||||
className?: string;
|
|
||||||
fixed?: boolean | ('left' | 'right');
|
|
||||||
filterIcon?: React.ReactNode;
|
|
||||||
filteredValue?: any[];
|
|
||||||
sortOrder?: boolean | ('ascend' | 'descend');
|
|
||||||
children?: ColumnProps<T>[];
|
|
||||||
onCellClick?: (record: T, event: any) => void;
|
|
||||||
onCell?: (record: T) => any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Column<T> extends React.Component<ColumnProps<T>, React.ComponentState> {}
|
export default class Column<T> extends React.Component<ColumnProps<T>, React.ComponentState> {}
|
||||||
|
@ -1,21 +1,12 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import Checkbox from '../checkbox';
|
import Checkbox from '../checkbox';
|
||||||
import Radio from '../radio';
|
import Radio from '../radio';
|
||||||
import { Store } from './createStore';
|
import { SelectionBoxProps, SelectionBoxState } from './interface';
|
||||||
|
|
||||||
export interface SelectionBoxProps {
|
export default class SelectionBox extends React.Component<SelectionBoxProps, SelectionBoxState> {
|
||||||
store: Store;
|
|
||||||
type: string;
|
|
||||||
defaultSelection: string[];
|
|
||||||
rowIndex: string;
|
|
||||||
disabled?: boolean;
|
|
||||||
onChange: (e) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class SelectionBox extends React.Component<SelectionBoxProps, any> {
|
|
||||||
unsubscribe: () => void;
|
unsubscribe: () => void;
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: SelectionBoxProps) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
@ -41,7 +32,7 @@ export default class SelectionBox extends React.Component<SelectionBoxProps, any
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCheckState(props) {
|
getCheckState(props: SelectionBoxProps) {
|
||||||
const { store, defaultSelection, rowIndex } = props;
|
const { store, defaultSelection, rowIndex } = props;
|
||||||
let checked = false;
|
let checked = false;
|
||||||
if (store.getState().selectionDirty) {
|
if (store.getState().selectionDirty) {
|
||||||
|
@ -1,36 +1,17 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import Checkbox from '../checkbox';
|
import Checkbox from '../checkbox';
|
||||||
import { Store } from './createStore';
|
|
||||||
import Dropdown from '../dropdown';
|
import Dropdown from '../dropdown';
|
||||||
import Menu from '../menu';
|
import Menu from '../menu';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import { SelectionCheckboxAllProps, SelectionCheckboxAllState, SelectionItem } from './interface';
|
||||||
|
|
||||||
export interface SelectionDecorator {
|
export default class SelectionCheckboxAll<T> extends
|
||||||
key: string;
|
React.Component<SelectionCheckboxAllProps<T>, SelectionCheckboxAllState> {
|
||||||
text: React.ReactNode;
|
|
||||||
onSelect: (changeableRowKeys: string[]) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SelectionCheckboxAllProps {
|
|
||||||
store: Store;
|
|
||||||
locale: any;
|
|
||||||
disabled: boolean;
|
|
||||||
getCheckboxPropsByItem: (item: any, index: number) => any;
|
|
||||||
getRecordKey: (record: any, index?: number) => string;
|
|
||||||
data: any[];
|
|
||||||
prefixCls: string | undefined;
|
|
||||||
onSelect: (key: string, index: number, selectFunc: any) => void;
|
|
||||||
hideDefaultSelections?: boolean;
|
|
||||||
selections?: SelectionDecorator[] | boolean;
|
|
||||||
getPopupContainer: (triggerNode?: Element) => HTMLElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class SelectionCheckboxAll extends React.Component<SelectionCheckboxAllProps, any> {
|
|
||||||
unsubscribe: () => void;
|
unsubscribe: () => void;
|
||||||
defaultSelections: SelectionDecorator[];
|
defaultSelections: SelectionItem[];
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: SelectionCheckboxAllProps<T>) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.defaultSelections = props.hideDefaultSelections ? [] : [{
|
this.defaultSelections = props.hideDefaultSelections ? [] : [{
|
||||||
@ -53,7 +34,7 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
this.subscribe();
|
this.subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps: SelectionCheckboxAllProps<T>) {
|
||||||
this.setCheckState(nextProps);
|
this.setCheckState(nextProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +51,7 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
checkSelection(data, type, byDefaultChecked) {
|
checkSelection(data: T[], type: string, byDefaultChecked: boolean) {
|
||||||
const { store, getCheckboxPropsByItem, getRecordKey } = this.props;
|
const { store, getCheckboxPropsByItem, getRecordKey } = this.props;
|
||||||
// type should be 'every' | 'some'
|
// type should be 'every' | 'some'
|
||||||
if (type === 'every' || type === 'some') {
|
if (type === 'every' || type === 'some') {
|
||||||
@ -84,7 +65,7 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
setCheckState(props) {
|
setCheckState(props: SelectionCheckboxAllProps<T>) {
|
||||||
const checked = this.getCheckState(props);
|
const checked = this.getCheckState(props);
|
||||||
const indeterminate = this.getIndeterminateState(props);
|
const indeterminate = this.getIndeterminateState(props);
|
||||||
if (checked !== this.state.checked) {
|
if (checked !== this.state.checked) {
|
||||||
@ -95,7 +76,7 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getCheckState(props) {
|
getCheckState(props: SelectionCheckboxAllProps<T>) {
|
||||||
const { store, data } = props;
|
const { store, data } = props;
|
||||||
let checked;
|
let checked;
|
||||||
if (!data.length) {
|
if (!data.length) {
|
||||||
@ -112,7 +93,7 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
return checked;
|
return checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIndeterminateState(props) {
|
getIndeterminateState(props: SelectionCheckboxAllProps<T>) {
|
||||||
const { store, data } = props;
|
const { store, data } = props;
|
||||||
let indeterminate;
|
let indeterminate;
|
||||||
if (!data.length) {
|
if (!data.length) {
|
||||||
@ -132,12 +113,12 @@ export default class SelectionCheckboxAll extends React.Component<SelectionCheck
|
|||||||
return indeterminate;
|
return indeterminate;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSelectAllChagne = (e) => {
|
handleSelectAllChagne = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
let checked = e.target.checked;
|
let checked = e.target.checked;
|
||||||
this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
|
this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderMenus(selections: SelectionDecorator[]) {
|
renderMenus(selections: SelectionItem[]) {
|
||||||
return selections.map((selection, index) => {
|
return selections.map((selection, index) => {
|
||||||
return (
|
return (
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
|
@ -5,23 +5,34 @@ import PropTypes from 'prop-types';
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import Pagination, { PaginationProps } from '../pagination';
|
import Pagination, { PaginationProps } from '../pagination';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import Spin, { SpinProps } from '../spin';
|
import Spin from '../spin';
|
||||||
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
||||||
import defaultLocale from '../locale-provider/default';
|
import defaultLocale from '../locale-provider/default';
|
||||||
import warning from '../_util/warning';
|
import warning from '../_util/warning';
|
||||||
import FilterDropdown from './filterDropdown';
|
import FilterDropdown from './filterDropdown';
|
||||||
import createStore, { Store } from './createStore';
|
import createStore, { Store } from './createStore';
|
||||||
import SelectionBox from './SelectionBox';
|
import SelectionBox from './SelectionBox';
|
||||||
import SelectionCheckboxAll, { SelectionDecorator } from './SelectionCheckboxAll';
|
import SelectionCheckboxAll from './SelectionCheckboxAll';
|
||||||
import Column, { ColumnProps } from './Column';
|
import Column from './Column';
|
||||||
import ColumnGroup from './ColumnGroup';
|
import ColumnGroup from './ColumnGroup';
|
||||||
import createBodyRow from './createBodyRow';
|
import createBodyRow from './createBodyRow';
|
||||||
import { flatArray, treeMap, flatFilter, normalizeColumns } from './util';
|
import { flatArray, treeMap, flatFilter, normalizeColumns } from './util';
|
||||||
|
import {
|
||||||
|
TableProps,
|
||||||
|
TableState,
|
||||||
|
TableComponents,
|
||||||
|
RowSelectionType,
|
||||||
|
TableLocale,
|
||||||
|
ColumnProps,
|
||||||
|
CompareFn,
|
||||||
|
TableStateFilters,
|
||||||
|
SelectionItemSelectFn,
|
||||||
|
} from './interface';
|
||||||
|
|
||||||
function noop() {
|
function noop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopPropagation(e) {
|
function stopPropagation(e: React.SyntheticEvent<any>) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
if (e.nativeEvent.stopImmediatePropagation) {
|
if (e.nativeEvent.stopImmediatePropagation) {
|
||||||
e.nativeEvent.stopImmediatePropagation();
|
e.nativeEvent.stopImmediatePropagation();
|
||||||
@ -39,72 +50,7 @@ const defaultPagination = {
|
|||||||
*/
|
*/
|
||||||
const emptyObject = {};
|
const emptyObject = {};
|
||||||
|
|
||||||
export type TableColumnConfig<T> = ColumnProps<T>;
|
export default class Table<T> extends React.Component<TableProps<T>, TableState<T>> {
|
||||||
|
|
||||||
export interface TableComponents {
|
|
||||||
table?: any;
|
|
||||||
header?: {
|
|
||||||
wrapper?: any;
|
|
||||||
row?: any;
|
|
||||||
cell?: any;
|
|
||||||
};
|
|
||||||
body?: {
|
|
||||||
wrapper?: any;
|
|
||||||
row?: any;
|
|
||||||
cell?: any;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TableRowSelection<T> {
|
|
||||||
type?: 'checkbox' | 'radio';
|
|
||||||
selectedRowKeys?: string[] | number[];
|
|
||||||
onChange?: (selectedRowKeys: string[] | number[], selectedRows: Object[]) => any;
|
|
||||||
getCheckboxProps?: (record: T) => Object;
|
|
||||||
onSelect?: (record: T, selected: boolean, selectedRows: Object[]) => any;
|
|
||||||
onSelectAll?: (selected: boolean, selectedRows: Object[], changeRows: Object[]) => any;
|
|
||||||
onSelectInvert?: (selectedRows: Object[]) => any;
|
|
||||||
selections?: SelectionDecorator[] | boolean;
|
|
||||||
hideDefaultSelections?: boolean;
|
|
||||||
fixed?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TableProps<T> {
|
|
||||||
prefixCls?: string;
|
|
||||||
dropdownPrefixCls?: string;
|
|
||||||
rowSelection?: TableRowSelection<T>;
|
|
||||||
pagination?: PaginationProps | boolean;
|
|
||||||
size?: 'default' | 'middle' | 'small';
|
|
||||||
dataSource?: T[];
|
|
||||||
components?: TableComponents;
|
|
||||||
columns?: ColumnProps<T>[];
|
|
||||||
rowKey?: string | ((record: T, index: number) => string);
|
|
||||||
rowClassName?: (record: T, index: number) => string;
|
|
||||||
expandedRowRender?: any;
|
|
||||||
defaultExpandedRowKeys?: string[] | number[];
|
|
||||||
expandedRowKeys?: string[] | number[];
|
|
||||||
expandIconAsCell?: boolean;
|
|
||||||
expandIconColumnIndex?: number;
|
|
||||||
onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void;
|
|
||||||
onExpand?: (expanded: boolean, record: T) => void;
|
|
||||||
onChange?: (pagination: PaginationProps | boolean, filters: string[], sorter: Object) => any;
|
|
||||||
loading?: boolean | SpinProps;
|
|
||||||
locale?: Object;
|
|
||||||
indentSize?: number;
|
|
||||||
onRowClick?: (record: T, index: number, event: Event) => any;
|
|
||||||
onRow?: (record: T, index: number) => any;
|
|
||||||
useFixedHeader?: boolean;
|
|
||||||
bordered?: boolean;
|
|
||||||
showHeader?: boolean;
|
|
||||||
footer?: (currentPageData: Object[]) => React.ReactNode;
|
|
||||||
title?: (currentPageData: Object[]) => React.ReactNode;
|
|
||||||
scroll?: { x?: boolean | number | string, y?: boolean | number | string };
|
|
||||||
childrenColumnName?: string;
|
|
||||||
bodyStyle?: React.CSSProperties;
|
|
||||||
className?: string;
|
|
||||||
style?: React.CSSProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|
||||||
static Column = Column;
|
static Column = Column;
|
||||||
static ColumnGroup = ColumnGroup;
|
static ColumnGroup = ColumnGroup;
|
||||||
|
|
||||||
@ -141,12 +87,14 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
showHeader: true,
|
showHeader: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckboxPropsCache: Object;
|
CheckboxPropsCache: {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
store: Store;
|
store: Store;
|
||||||
columns: ColumnProps<T>[];
|
columns: ColumnProps<T>[];
|
||||||
components: TableComponents;
|
components: TableComponents;
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: TableProps<T>) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
warning(
|
warning(
|
||||||
@ -155,7 +103,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
'fixed columns instead, see: https://u.ant.design/fixed-columns.',
|
'fixed columns instead, see: https://u.ant.design/fixed-columns.',
|
||||||
);
|
);
|
||||||
|
|
||||||
this.columns = props.columns || normalizeColumns(props.children);
|
this.columns = props.columns || normalizeColumns(props.children as React.ReactChildren);
|
||||||
|
|
||||||
this.createComponents(props.components);
|
this.createComponents(props.components);
|
||||||
|
|
||||||
@ -174,7 +122,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCheckboxPropsByItem = (item, index) => {
|
getCheckboxPropsByItem = (item: T, index: number) => {
|
||||||
const { rowSelection = {} } = this.props;
|
const { rowSelection = {} } = this.props;
|
||||||
if (!rowSelection.getCheckboxProps) {
|
if (!rowSelection.getCheckboxProps) {
|
||||||
return {};
|
return {};
|
||||||
@ -193,12 +141,12 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
return this.getFlatData()
|
return this.getFlatData()
|
||||||
.filter((item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
|
.filter((item: T, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
|
||||||
.map((record, rowIndex) => this.getRecordKey(record, rowIndex));
|
.map((record, rowIndex) => this.getRecordKey(record, rowIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefaultPagination(props) {
|
getDefaultPagination(props: TableProps<T>) {
|
||||||
const pagination = props.pagination || {};
|
const pagination: PaginationProps = props.pagination || {};
|
||||||
return this.hasPagination(props) ?
|
return this.hasPagination(props) ?
|
||||||
{
|
{
|
||||||
...defaultPagination,
|
...defaultPagination,
|
||||||
@ -208,8 +156,8 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
} : {};
|
} : {};
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps: TableProps<T>) {
|
||||||
this.columns = nextProps.columns || normalizeColumns(nextProps.children);
|
this.columns = nextProps.columns || normalizeColumns(nextProps.children as React.ReactChildren);
|
||||||
if ('pagination' in nextProps || 'pagination' in this.props) {
|
if ('pagination' in nextProps || 'pagination' in this.props) {
|
||||||
this.setState(previousState => {
|
this.setState(previousState => {
|
||||||
const newPagination = {
|
const newPagination = {
|
||||||
@ -265,7 +213,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
this.createComponents(nextProps.components, this.props.components);
|
this.createComponents(nextProps.components, this.props.components);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRow = (record, index) => {
|
onRow = (record: T, index: number) => {
|
||||||
const { onRow, prefixCls } = this.props;
|
const { onRow, prefixCls } = this.props;
|
||||||
const custom = onRow ? onRow(record, index) : {};
|
const custom = onRow ? onRow(record, index) : {};
|
||||||
return {
|
return {
|
||||||
@ -276,8 +224,8 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
setSelectedRowKeys(selectedRowKeys, { selectWay, record, checked, changeRowKeys }: any) {
|
setSelectedRowKeys(selectedRowKeys: string[], { selectWay, record, checked, changeRowKeys }: any) {
|
||||||
const { rowSelection = {} } = this.props;
|
const { rowSelection = {} as any } = this.props;
|
||||||
if (rowSelection && !('selectedRowKeys' in rowSelection)) {
|
if (rowSelection && !('selectedRowKeys' in rowSelection)) {
|
||||||
this.store.setState({ selectedRowKeys });
|
this.store.setState({ selectedRowKeys });
|
||||||
}
|
}
|
||||||
@ -307,7 +255,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return (props || this.props).pagination !== false;
|
return (props || this.props).pagination !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
isFiltersChanged(filters) {
|
isFiltersChanged(filters: TableStateFilters) {
|
||||||
let filtersChanged = false;
|
let filtersChanged = false;
|
||||||
if (Object.keys(filters).length !== Object.keys(this.state.filters).length) {
|
if (Object.keys(filters).length !== Object.keys(this.state.filters).length) {
|
||||||
filtersChanged = true;
|
filtersChanged = true;
|
||||||
@ -321,26 +269,33 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return filtersChanged;
|
return filtersChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSortOrderColumns(columns?) {
|
getSortOrderColumns(columns?: ColumnProps<T>[]) {
|
||||||
return flatFilter(columns || this.columns || [], column => 'sortOrder' in column);
|
return flatFilter(
|
||||||
|
columns || this.columns || [],
|
||||||
|
(column: ColumnProps<T>) => 'sortOrder' in column,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getFilteredValueColumns(columns?) {
|
getFilteredValueColumns(columns?: ColumnProps<T>[]) {
|
||||||
return flatFilter(columns || this.columns || [], column => typeof column.filteredValue !== 'undefined');
|
return flatFilter(
|
||||||
|
columns || this.columns || [],
|
||||||
|
(column: ColumnProps<T>) => typeof column.filteredValue !== 'undefined',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getFiltersFromColumns(columns?) {
|
getFiltersFromColumns(columns?: ColumnProps<T>[]) {
|
||||||
let filters = {};
|
let filters: any = {};
|
||||||
this.getFilteredValueColumns(columns).forEach(col => {
|
this.getFilteredValueColumns(columns).forEach((col: ColumnProps<T>) => {
|
||||||
filters[this.getColumnKey(col)] = col.filteredValue;
|
const colKey = this.getColumnKey(col) as string;
|
||||||
|
filters[colKey] = col.filteredValue;
|
||||||
});
|
});
|
||||||
return filters;
|
return filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefaultSortOrder(columns?) {
|
getDefaultSortOrder(columns?: ColumnProps<T>[]) {
|
||||||
const definedSortState = this.getSortStateFromColumns(columns);
|
const definedSortState = this.getSortStateFromColumns(columns);
|
||||||
|
|
||||||
let defaultSortedColumn = flatFilter(columns || [], column => column.defaultSortOrder != null)[0];
|
let defaultSortedColumn = flatFilter(columns || [], (column: ColumnProps<T>) => column.defaultSortOrder != null)[0];
|
||||||
|
|
||||||
if (defaultSortedColumn && !definedSortState.sortColumn) {
|
if (defaultSortedColumn && !definedSortState.sortColumn) {
|
||||||
return {
|
return {
|
||||||
@ -352,10 +307,10 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return definedSortState;
|
return definedSortState;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSortStateFromColumns(columns?) {
|
getSortStateFromColumns(columns?: ColumnProps<T>[]) {
|
||||||
// return first column which sortOrder is not falsy
|
// return first column which sortOrder is not falsy
|
||||||
const sortedColumn =
|
const sortedColumn =
|
||||||
this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];
|
this.getSortOrderColumns(columns).filter((col: ColumnProps<T>) => col.sortOrder)[0];
|
||||||
|
|
||||||
if (sortedColumn) {
|
if (sortedColumn) {
|
||||||
return {
|
return {
|
||||||
@ -376,8 +331,9 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
typeof sortColumn.sorter !== 'function') {
|
typeof sortColumn.sorter !== 'function') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return (a, b) => {
|
|
||||||
const result = sortColumn.sorter(a, b);
|
return (a: T, b: T) => {
|
||||||
|
const result = (sortColumn!.sorter as CompareFn<T>)(a, b);
|
||||||
if (result !== 0) {
|
if (result !== 0) {
|
||||||
return (sortOrder === 'descend') ? -result : result;
|
return (sortOrder === 'descend') ? -result : result;
|
||||||
}
|
}
|
||||||
@ -385,7 +341,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleSortOrder(order, column) {
|
toggleSortOrder(order: string, column: ColumnProps<T>) {
|
||||||
let { sortColumn, sortOrder } = this.state;
|
let { sortColumn, sortOrder } = this.state;
|
||||||
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
||||||
let isSortColumn = this.isSortColumn(column);
|
let isSortColumn = this.isSortColumn(column);
|
||||||
@ -419,18 +375,18 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleFilter = (column, nextFilters) => {
|
handleFilter = (column: ColumnProps<T>, nextFilters: string[]) => {
|
||||||
const props = this.props;
|
const props = this.props;
|
||||||
let pagination = { ...this.state.pagination };
|
let pagination = { ...this.state.pagination };
|
||||||
const filters = {
|
const filters = {
|
||||||
...this.state.filters,
|
...this.state.filters,
|
||||||
[this.getColumnKey(column)]: nextFilters,
|
[this.getColumnKey(column) as string]: nextFilters,
|
||||||
};
|
};
|
||||||
// Remove filters not in current columns
|
// Remove filters not in current columns
|
||||||
const currentColumnKeys: string[] = [];
|
const currentColumnKeys: string[] = [];
|
||||||
treeMap(this.columns, c => {
|
treeMap(this.columns, c => {
|
||||||
if (!c.children) {
|
if (!c.children) {
|
||||||
currentColumnKeys.push(this.getColumnKey(c));
|
currentColumnKeys.push(this.getColumnKey(c) as string);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Object.keys(filters).forEach((columnKey) => {
|
Object.keys(filters).forEach((columnKey) => {
|
||||||
@ -442,7 +398,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
if (props.pagination) {
|
if (props.pagination) {
|
||||||
// Reset current prop
|
// Reset current prop
|
||||||
pagination.current = 1;
|
pagination.current = 1;
|
||||||
pagination.onChange(pagination.current);
|
pagination.onChange!(pagination.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newState = {
|
const newState = {
|
||||||
@ -451,7 +407,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
};
|
};
|
||||||
const filtersToSetState = { ...filters };
|
const filtersToSetState = { ...filters };
|
||||||
// Remove filters which is controlled
|
// Remove filters which is controlled
|
||||||
this.getFilteredValueColumns().forEach(col => {
|
this.getFilteredValueColumns().forEach((col: ColumnProps<T>) => {
|
||||||
const columnKey = this.getColumnKey(col);
|
const columnKey = this.getColumnKey(col);
|
||||||
if (columnKey) {
|
if (columnKey) {
|
||||||
delete filtersToSetState[columnKey];
|
delete filtersToSetState[columnKey];
|
||||||
@ -485,7 +441,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSelect = (record, rowIndex, e) => {
|
handleSelect = (record: T, rowIndex: number, e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const checked = e.target.checked;
|
const checked = e.target.checked;
|
||||||
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
||||||
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
||||||
@ -493,7 +449,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
if (checked) {
|
if (checked) {
|
||||||
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
||||||
} else {
|
} else {
|
||||||
selectedRowKeys = selectedRowKeys.filter((i) => key !== i);
|
selectedRowKeys = selectedRowKeys.filter((i: string) => key !== i);
|
||||||
}
|
}
|
||||||
this.store.setState({
|
this.store.setState({
|
||||||
selectionDirty: true,
|
selectionDirty: true,
|
||||||
@ -505,7 +461,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRadioSelect = (record, rowIndex, e) => {
|
handleRadioSelect = (record: T, rowIndex: number, e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const checked = e.target.checked;
|
const checked = e.target.checked;
|
||||||
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
||||||
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
||||||
@ -521,7 +477,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSelectRow = (selectionKey, index, onSelectFunc) => {
|
handleSelectRow = (selectionKey: string, index: number, onSelectFunc: SelectionItemSelectFn) => {
|
||||||
const data = this.getFlatCurrentPageData();
|
const data = this.getFlatCurrentPageData();
|
||||||
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
||||||
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
||||||
@ -588,7 +544,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePageChange = (current, ...otherArguments) => {
|
handlePageChange = (current: number, ...otherArguments: any[]) => {
|
||||||
const props = this.props;
|
const props = this.props;
|
||||||
let pagination = { ...this.state.pagination };
|
let pagination = { ...this.state.pagination };
|
||||||
if (current) {
|
if (current) {
|
||||||
@ -596,7 +552,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
} else {
|
} else {
|
||||||
pagination.current = pagination.current || 1;
|
pagination.current = pagination.current || 1;
|
||||||
}
|
}
|
||||||
pagination.onChange(pagination.current, ...otherArguments);
|
pagination.onChange!(pagination.current, ...otherArguments);
|
||||||
|
|
||||||
const newState = {
|
const newState = {
|
||||||
pagination,
|
pagination,
|
||||||
@ -626,11 +582,11 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderSelectionBox = (type) => {
|
renderSelectionBox = (type: RowSelectionType | undefined) => {
|
||||||
return (_, record, index) => {
|
return (_: any, record: T, index: number) => {
|
||||||
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
||||||
const props = this.getCheckboxPropsByItem(record, index);
|
const props = this.getCheckboxPropsByItem(record, index);
|
||||||
const handleChange = (e) => {
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
type === 'radio' ? this.handleRadioSelect(record, rowIndex, e) :
|
type === 'radio' ? this.handleRadioSelect(record, rowIndex, e) :
|
||||||
this.handleSelect(record, rowIndex, e);
|
this.handleSelect(record, rowIndex, e);
|
||||||
};
|
};
|
||||||
@ -650,10 +606,10 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getRecordKey = (record, index): string => {
|
getRecordKey = (record: T, index: number) => {
|
||||||
const rowKey = this.props.rowKey;
|
const rowKey = this.props.rowKey;
|
||||||
const recordKey = (typeof rowKey === 'function') ?
|
const recordKey = (typeof rowKey === 'function') ?
|
||||||
rowKey(record, index) : record[rowKey as string];
|
rowKey(record, index) : (record as any)[rowKey as string];
|
||||||
warning(recordKey !== undefined,
|
warning(recordKey !== undefined,
|
||||||
'Each record in dataSource of table should have a unique `key` prop, or set `rowKey` to an unique primary key,' +
|
'Each record in dataSource of table should have a unique `key` prop, or set `rowKey` to an unique primary key,' +
|
||||||
'see https://u.ant.design/table-row-key',
|
'see https://u.ant.design/table-row-key',
|
||||||
@ -665,7 +621,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return ReactDOM.findDOMNode(this) as HTMLElement;
|
return ReactDOM.findDOMNode(this) as HTMLElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
renderRowSelection(locale) {
|
renderRowSelection(locale: TableLocale) {
|
||||||
const { prefixCls, rowSelection } = this.props;
|
const { prefixCls, rowSelection } = this.props;
|
||||||
const columns = this.columns.concat();
|
const columns = this.columns.concat();
|
||||||
if (rowSelection) {
|
if (rowSelection) {
|
||||||
@ -716,19 +672,19 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
getColumnKey(column, index?) {
|
getColumnKey(column: ColumnProps<T>, index?: number) {
|
||||||
return column.key || column.dataIndex || index;
|
return column.key || column.dataIndex || index;
|
||||||
}
|
}
|
||||||
|
|
||||||
getMaxCurrent(total) {
|
getMaxCurrent(total: number) {
|
||||||
const { current, pageSize } = this.state.pagination;
|
const { current, pageSize } = this.state.pagination;
|
||||||
if ((current - 1) * pageSize >= total) {
|
if ((current! - 1) * pageSize! >= total) {
|
||||||
return Math.floor((total - 1) / pageSize) + 1;
|
return Math.floor((total - 1) / pageSize!) + 1;
|
||||||
}
|
}
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
isSortColumn(column) {
|
isSortColumn(column: ColumnProps<T>) {
|
||||||
const { sortColumn } = this.state;
|
const { sortColumn } = this.state;
|
||||||
if (!column || !sortColumn) {
|
if (!column || !sortColumn) {
|
||||||
return false;
|
return false;
|
||||||
@ -736,12 +692,12 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
|
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderColumnsDropdown(columns, locale) {
|
renderColumnsDropdown(columns: ColumnProps<T>[], locale: TableLocale) {
|
||||||
const { prefixCls, dropdownPrefixCls } = this.props;
|
const { prefixCls, dropdownPrefixCls } = this.props;
|
||||||
const { sortOrder } = this.state;
|
const { sortOrder } = this.state;
|
||||||
return treeMap(columns, (originColumn, i) => {
|
return treeMap(columns, (originColumn, i) => {
|
||||||
let column = { ...originColumn };
|
let column = { ...originColumn };
|
||||||
let key = this.getColumnKey(column, i);
|
let key = this.getColumnKey(column, i) as string;
|
||||||
let filterDropdown;
|
let filterDropdown;
|
||||||
let sortButton;
|
let sortButton;
|
||||||
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
|
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
|
||||||
@ -797,9 +753,9 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleShowSizeChange = (current, pageSize) => {
|
handleShowSizeChange = (current: number, pageSize: number) => {
|
||||||
const pagination = this.state.pagination;
|
const pagination = this.state.pagination;
|
||||||
pagination.onShowSizeChange(current, pageSize);
|
pagination.onShowSizeChange!(current, pageSize);
|
||||||
const nextPagination = {
|
const nextPagination = {
|
||||||
...pagination,
|
...pagination,
|
||||||
pageSize,
|
pageSize,
|
||||||
@ -860,7 +816,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return [pagination, filters, sorter];
|
return [pagination, filters, sorter];
|
||||||
}
|
}
|
||||||
|
|
||||||
findColumn(myKey) {
|
findColumn(myKey: string | number) {
|
||||||
let column;
|
let column;
|
||||||
treeMap(this.columns, c => {
|
treeMap(this.columns, c => {
|
||||||
if (this.getColumnKey(c) === myKey) {
|
if (this.getColumnKey(c) === myKey) {
|
||||||
@ -872,16 +828,16 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
|
|
||||||
getCurrentPageData() {
|
getCurrentPageData() {
|
||||||
let data = this.getLocalData();
|
let data = this.getLocalData();
|
||||||
let current;
|
let current: number;
|
||||||
let pageSize;
|
let pageSize: number;
|
||||||
let state = this.state;
|
let state = this.state;
|
||||||
// 如果没有分页的话,默认全部展示
|
// 如果没有分页的话,默认全部展示
|
||||||
if (!this.hasPagination()) {
|
if (!this.hasPagination()) {
|
||||||
pageSize = Number.MAX_VALUE;
|
pageSize = Number.MAX_VALUE;
|
||||||
current = 1;
|
current = 1;
|
||||||
} else {
|
} else {
|
||||||
pageSize = state.pagination.pageSize;
|
pageSize = state.pagination.pageSize as number;
|
||||||
current = this.getMaxCurrent(state.pagination.total || data.length);
|
current = this.getMaxCurrent(state.pagination.total || data.length) as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分页
|
// 分页
|
||||||
@ -904,9 +860,9 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
return flatArray(this.getCurrentPageData());
|
return flatArray(this.getCurrentPageData());
|
||||||
}
|
}
|
||||||
|
|
||||||
recursiveSort(data, sorterFn) {
|
recursiveSort(data: T[], sorterFn: (a: any, b: any) => number): T[] {
|
||||||
const { childrenColumnName = 'children' } = this.props;
|
const { childrenColumnName = 'children' } = this.props;
|
||||||
return data.sort(sorterFn).map(item => (item[childrenColumnName] ? {
|
return data.sort(sorterFn).map((item: any) => (item[childrenColumnName] ? {
|
||||||
...item,
|
...item,
|
||||||
[childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn),
|
[childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn),
|
||||||
} : item));
|
} : item));
|
||||||
@ -954,7 +910,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTable = (contextLocale) => {
|
renderTable = (contextLocale: TableLocale) => {
|
||||||
const locale = { ...contextLocale, ...this.props.locale };
|
const locale = { ...contextLocale, ...this.props.locale };
|
||||||
const { style, className, prefixCls, showHeader, ...restProps } = this.props;
|
const { style, className, prefixCls, showHeader, ...restProps } = this.props;
|
||||||
const data = this.getCurrentPageData();
|
const data = this.getCurrentPageData();
|
||||||
|
@ -21,7 +21,7 @@ export default function createTableRow(Component = 'tr') {
|
|||||||
private store: Store;
|
private store: Store;
|
||||||
private unsubscribe: () => void;
|
private unsubscribe: () => void;
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: BodyRowProps) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.store = props.store;
|
this.store = props.store;
|
||||||
|
@ -4,11 +4,11 @@ export interface Store {
|
|||||||
subscribe: (listener: () => void) => () => void;
|
subscribe: (listener: () => void) => () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function createStore(initialState): Store {
|
export default function createStore(initialState: object): Store {
|
||||||
let state = initialState;
|
let state = initialState;
|
||||||
const listeners: any[] = [];
|
const listeners: any[] = [];
|
||||||
|
|
||||||
function setState(partial) {
|
function setState(partial: object) {
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
...partial,
|
...partial,
|
||||||
@ -22,7 +22,7 @@ export default function createStore(initialState): Store {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribe(listener) {
|
function subscribe(listener: () => any) {
|
||||||
listeners.push(listener);
|
listeners.push(listener);
|
||||||
|
|
||||||
return function unsubscribe() {
|
return function unsubscribe() {
|
||||||
|
@ -8,26 +8,9 @@ import Icon from '../icon';
|
|||||||
import Checkbox from '../checkbox';
|
import Checkbox from '../checkbox';
|
||||||
import Radio from '../radio';
|
import Radio from '../radio';
|
||||||
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';
|
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';
|
||||||
|
import { FilterMenuProps, FilterMenuState, ColumnProps, ColumnFilterItem } from './interface';
|
||||||
|
|
||||||
export interface FilterMenuProps {
|
export default class FilterMenu<T> extends React.Component<FilterMenuProps<T>, FilterMenuState> {
|
||||||
locale: any;
|
|
||||||
selectedKeys: string[];
|
|
||||||
column: {
|
|
||||||
filterMultiple?: boolean,
|
|
||||||
filterDropdown?: React.ReactNode,
|
|
||||||
filters?: { text: string; value: string, children?: any[] }[],
|
|
||||||
filterDropdownVisible?: boolean,
|
|
||||||
onFilterDropdownVisibleChange?: (visible: boolean) => any,
|
|
||||||
fixed?: boolean | string,
|
|
||||||
filterIcon?: React.ReactNode;
|
|
||||||
};
|
|
||||||
confirmFilter: (column: Object, selectedKeys: string[]) => any;
|
|
||||||
prefixCls: string;
|
|
||||||
dropdownPrefixCls: string;
|
|
||||||
getPopupContainer: (triggerNode?: Element) => HTMLElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
handleFilter() {},
|
handleFilter() {},
|
||||||
column: {},
|
column: {},
|
||||||
@ -35,7 +18,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
|
|
||||||
neverShown: boolean;
|
neverShown: boolean;
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: FilterMenuProps<T>) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
const visible = ('filterDropdownVisible' in props.column) ?
|
const visible = ('filterDropdownVisible' in props.column) ?
|
||||||
@ -53,7 +36,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
this.setNeverShown(column);
|
this.setNeverShown(column);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps: FilterMenuProps<T>) {
|
||||||
const { column } = nextProps;
|
const { column } = nextProps;
|
||||||
this.setNeverShown(column);
|
this.setNeverShown(column);
|
||||||
const newState = {} as {
|
const newState = {} as {
|
||||||
@ -64,14 +47,14 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
newState.selectedKeys = nextProps.selectedKeys;
|
newState.selectedKeys = nextProps.selectedKeys;
|
||||||
}
|
}
|
||||||
if ('filterDropdownVisible' in column) {
|
if ('filterDropdownVisible' in column) {
|
||||||
newState.visible = column.filterDropdownVisible;
|
newState.visible = column.filterDropdownVisible as boolean;
|
||||||
}
|
}
|
||||||
if (Object.keys(newState).length > 0) {
|
if (Object.keys(newState).length > 0) {
|
||||||
this.setState(newState);
|
this.setState(newState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setNeverShown = (column) => {
|
setNeverShown = (column: ColumnProps<T>) => {
|
||||||
const rootNode = ReactDOM.findDOMNode(this);
|
const rootNode = ReactDOM.findDOMNode(this);
|
||||||
const filterBelongToScrollBody = !!closest(rootNode, `.ant-table-scroll`);
|
const filterBelongToScrollBody = !!closest(rootNode, `.ant-table-scroll`);
|
||||||
if (filterBelongToScrollBody) {
|
if (filterBelongToScrollBody) {
|
||||||
@ -83,11 +66,11 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setSelectedKeys = ({ selectedKeys }) => {
|
setSelectedKeys = ({ selectedKeys }: { selectedKeys: string[] }) => {
|
||||||
this.setState({ selectedKeys });
|
this.setState({ selectedKeys });
|
||||||
}
|
}
|
||||||
|
|
||||||
setVisible(visible) {
|
setVisible(visible: boolean) {
|
||||||
const { column } = this.props;
|
const { column } = this.props;
|
||||||
if (!('filterDropdownVisible' in column)) {
|
if (!('filterDropdownVisible' in column)) {
|
||||||
this.setState({ visible });
|
this.setState({ visible });
|
||||||
@ -108,7 +91,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
this.confirmFilter();
|
this.confirmFilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
onVisibleChange = (visible) => {
|
onVisibleChange = (visible: boolean) => {
|
||||||
this.setVisible(visible);
|
this.setVisible(visible);
|
||||||
if (!visible) {
|
if (!visible) {
|
||||||
this.confirmFilter();
|
this.confirmFilter();
|
||||||
@ -121,7 +104,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderMenuItem(item) {
|
renderMenuItem(item: ColumnFilterItem) {
|
||||||
const { column } = this.props;
|
const { column } = this.props;
|
||||||
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
|
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
|
||||||
const input = multiple ? (
|
const input = multiple ? (
|
||||||
@ -143,7 +126,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
return filters.some(item => !!(item.children && item.children.length > 0));
|
return filters.some(item => !!(item.children && item.children.length > 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
renderMenus(items) {
|
renderMenus(items: ColumnFilterItem[]): React.ReactElement<any>[] {
|
||||||
return items.map(item => {
|
return items.map(item => {
|
||||||
if (item.children && item.children.length > 0) {
|
if (item.children && item.children.length > 0) {
|
||||||
const { keyPathOfSelectedItem } = this.state;
|
const { keyPathOfSelectedItem } = this.state;
|
||||||
@ -161,7 +144,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMenuItemClick = (info) => {
|
handleMenuItemClick = (info: { keyPath: string, key: string }) => {
|
||||||
if (info.keyPath.length <= 1) {
|
if (info.keyPath.length <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -210,7 +193,7 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
|
|||||||
onDeselect={this.setSelectedKeys}
|
onDeselect={this.setSelectedKeys}
|
||||||
selectedKeys={this.state.selectedKeys}
|
selectedKeys={this.state.selectedKeys}
|
||||||
>
|
>
|
||||||
{this.renderMenus(column.filters)}
|
{this.renderMenus(column.filters!)}
|
||||||
</Menu>
|
</Menu>
|
||||||
<div className={`${prefixCls}-dropdown-btns`}>
|
<div className={`${prefixCls}-dropdown-btns`}>
|
||||||
<a
|
<a
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
import Table from './Table';
|
import Table from './Table';
|
||||||
|
|
||||||
export { ColumnProps } from './Column';
|
export * from './interface';
|
||||||
export { ColumnGroupProps } from './ColumnGroup';
|
|
||||||
export { FilterMenuProps } from './filterDropdown';
|
|
||||||
export { FilterDropdownMenuWrapperProps } from './FilterDropdownMenuWrapper';
|
|
||||||
export { SelectionBoxProps } from './SelectionBox';
|
|
||||||
export { SelectionCheckboxAllProps } from './SelectionCheckboxAll';
|
|
||||||
export { TableProps, TableRowSelection, TableColumnConfig } from './Table';
|
|
||||||
|
|
||||||
export default Table;
|
export default Table;
|
||||||
|
175
components/table/interface.tsx
Normal file
175
components/table/interface.tsx
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import { PaginationProps } from '../pagination';
|
||||||
|
import { SpinProps } from '../spin';
|
||||||
|
import { Store } from './createStore';
|
||||||
|
|
||||||
|
export type CompareFn<T> = ((a: T, b: T) => number);
|
||||||
|
export type ColumnFilterItem = { text: string; value: string, children?: ColumnFilterItem[] };
|
||||||
|
|
||||||
|
export interface ColumnProps<T> {
|
||||||
|
title?: React.ReactNode;
|
||||||
|
key?: React.Key;
|
||||||
|
dataIndex?: string;
|
||||||
|
render?: (text: any, record: T, index: number) => React.ReactNode;
|
||||||
|
filters?: ColumnFilterItem[];
|
||||||
|
onFilter?: (value: any, record: T) => boolean;
|
||||||
|
filterMultiple?: boolean;
|
||||||
|
filterDropdown?: React.ReactNode;
|
||||||
|
filterDropdownVisible?: boolean;
|
||||||
|
onFilterDropdownVisibleChange?: (visible: boolean) => void;
|
||||||
|
sorter?: boolean | CompareFn<T>;
|
||||||
|
defaultSortOrder?: 'ascend' | 'descend';
|
||||||
|
colSpan?: number;
|
||||||
|
width?: string | number;
|
||||||
|
className?: string;
|
||||||
|
fixed?: boolean | ('left' | 'right');
|
||||||
|
filterIcon?: React.ReactNode;
|
||||||
|
filteredValue?: any[];
|
||||||
|
sortOrder?: boolean | ('ascend' | 'descend');
|
||||||
|
children?: ColumnProps<T>[];
|
||||||
|
onCellClick?: (record: T, event: any) => void;
|
||||||
|
onCell?: (record: T) => any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TableComponents {
|
||||||
|
table?: any;
|
||||||
|
header?: {
|
||||||
|
wrapper?: any;
|
||||||
|
row?: any;
|
||||||
|
cell?: any;
|
||||||
|
};
|
||||||
|
body?: {
|
||||||
|
wrapper?: any;
|
||||||
|
row?: any;
|
||||||
|
cell?: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TableLocale {
|
||||||
|
filterTitle?: string;
|
||||||
|
filterConfirm?: string;
|
||||||
|
filterReset?: string;
|
||||||
|
emptyText?: string;
|
||||||
|
selectAll?: string;
|
||||||
|
selectInvert?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RowSelectionType = 'checkbox' | 'radio';
|
||||||
|
export type SelectionSelectFn<T> = (record: T, selected: boolean, selectedRows: Object[]) => any;
|
||||||
|
|
||||||
|
export interface TableRowSelection<T> {
|
||||||
|
type?: RowSelectionType;
|
||||||
|
selectedRowKeys?: string[] | number[];
|
||||||
|
onChange?: (selectedRowKeys: string[] | number[], selectedRows: Object[]) => any;
|
||||||
|
getCheckboxProps?: (record: T) => Object;
|
||||||
|
onSelect?: SelectionSelectFn<T>;
|
||||||
|
onSelectAll?: (selected: boolean, selectedRows: Object[], changeRows: Object[]) => any;
|
||||||
|
onSelectInvert?: (selectedRows: Object[]) => any;
|
||||||
|
selections?: SelectionItem[] | boolean;
|
||||||
|
hideDefaultSelections?: boolean;
|
||||||
|
fixed?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TableProps<T> {
|
||||||
|
prefixCls?: string;
|
||||||
|
dropdownPrefixCls?: string;
|
||||||
|
rowSelection?: TableRowSelection<T>;
|
||||||
|
pagination?: PaginationProps | false;
|
||||||
|
size?: 'default' | 'middle' | 'small';
|
||||||
|
dataSource?: T[];
|
||||||
|
components?: TableComponents;
|
||||||
|
columns?: ColumnProps<T>[];
|
||||||
|
rowKey?: string | ((record: T, index: number) => string);
|
||||||
|
rowClassName?: (record: T, index: number) => string;
|
||||||
|
expandedRowRender?: any;
|
||||||
|
defaultExpandedRowKeys?: string[] | number[];
|
||||||
|
expandedRowKeys?: string[] | number[];
|
||||||
|
expandIconAsCell?: boolean;
|
||||||
|
expandIconColumnIndex?: number;
|
||||||
|
onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void;
|
||||||
|
onExpand?: (expanded: boolean, record: T) => void;
|
||||||
|
onChange?: (pagination: PaginationProps | boolean, filters: string[], sorter: Object) => any;
|
||||||
|
loading?: boolean | SpinProps;
|
||||||
|
locale?: Object;
|
||||||
|
indentSize?: number;
|
||||||
|
onRowClick?: (record: T, index: number, event: Event) => any;
|
||||||
|
onRow?: (record: T, index: number) => any;
|
||||||
|
useFixedHeader?: boolean;
|
||||||
|
bordered?: boolean;
|
||||||
|
showHeader?: boolean;
|
||||||
|
footer?: (currentPageData: Object[]) => React.ReactNode;
|
||||||
|
title?: (currentPageData: Object[]) => React.ReactNode;
|
||||||
|
scroll?: { x?: boolean | number | string, y?: boolean | number | string };
|
||||||
|
childrenColumnName?: string;
|
||||||
|
bodyStyle?: React.CSSProperties;
|
||||||
|
className?: string;
|
||||||
|
style?: React.CSSProperties;
|
||||||
|
children?: React.ReactChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TableStateFilters {
|
||||||
|
[key: string]: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TableState<T> {
|
||||||
|
pagination: PaginationProps;
|
||||||
|
filters: TableStateFilters;
|
||||||
|
sortColumn: ColumnProps<T> | null;
|
||||||
|
sortOrder: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SelectionItemSelectFn = (key: string[]) => any;
|
||||||
|
|
||||||
|
export interface SelectionItem {
|
||||||
|
key: string;
|
||||||
|
text: React.ReactNode;
|
||||||
|
onSelect: SelectionItemSelectFn;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectionCheckboxAllProps<T> {
|
||||||
|
store: Store;
|
||||||
|
locale: any;
|
||||||
|
disabled: boolean;
|
||||||
|
getCheckboxPropsByItem: (item: any, index: number) => any;
|
||||||
|
getRecordKey: (record: any, index?: number) => string;
|
||||||
|
data: T[];
|
||||||
|
prefixCls: string | undefined;
|
||||||
|
onSelect: (key: string, index: number, selectFunc: any) => void;
|
||||||
|
hideDefaultSelections?: boolean;
|
||||||
|
selections?: SelectionItem[] | boolean;
|
||||||
|
getPopupContainer: (triggerNode?: Element) => HTMLElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectionCheckboxAllState {
|
||||||
|
checked: boolean;
|
||||||
|
indeterminate: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectionBoxProps {
|
||||||
|
store: Store;
|
||||||
|
type?: RowSelectionType;
|
||||||
|
defaultSelection: string[];
|
||||||
|
rowIndex: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
onChange: React.ChangeEventHandler<HTMLInputElement>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectionBoxState {
|
||||||
|
checked?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FilterMenuProps<T> {
|
||||||
|
locale: TableLocale;
|
||||||
|
selectedKeys: string[];
|
||||||
|
column: ColumnProps<T>;
|
||||||
|
confirmFilter: (column: ColumnProps<T>, selectedKeys: string[]) => any;
|
||||||
|
prefixCls: string;
|
||||||
|
dropdownPrefixCls: string;
|
||||||
|
getPopupContainer: (triggerNode?: Element) => HTMLElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FilterMenuState {
|
||||||
|
selectedKeys: string[];
|
||||||
|
keyPathOfSelectedItem: {[key: string]: string};
|
||||||
|
visible?: boolean;
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
|
||||||
export function flatArray(data: Object[] = [], childrenName = 'children') {
|
export function flatArray(data: any[] = [], childrenName = 'children') {
|
||||||
const result: Object[] = [];
|
const result: any[] = [];
|
||||||
const loop = (array) => {
|
const loop = (array: any[]) => {
|
||||||
array.forEach(item => {
|
array.forEach(item => {
|
||||||
if (item[childrenName]) {
|
if (item[childrenName]) {
|
||||||
const newItem = { ...item };
|
const newItem = { ...item };
|
||||||
@ -20,14 +20,14 @@ export function flatArray(data: Object[] = [], childrenName = 'children') {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function treeMap(tree: Object[], mapper: Function, childrenName = 'children') {
|
export function treeMap<Node>(tree: Node[], mapper: (node: Node, index: number) => any, childrenName = 'children') {
|
||||||
return tree.map((node, index) => {
|
return tree.map((node: any, index) => {
|
||||||
const extra = {};
|
const extra: any = {};
|
||||||
if (node[childrenName]) {
|
if (node[childrenName]) {
|
||||||
extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
|
extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...mapper(node, index),
|
...mapper(node as Node, index),
|
||||||
...extra,
|
...extra,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -46,7 +46,7 @@ export function flatFilter(tree: any[], callback: Function) {
|
|||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeColumns(elements) {
|
export function normalizeColumns(elements: React.ReactChildren) {
|
||||||
const columns: any[] = [];
|
const columns: any[] = [];
|
||||||
React.Children.forEach(elements, (element) => {
|
React.Children.forEach(elements, (element) => {
|
||||||
if (!React.isValidElement(element)) {
|
if (!React.isValidElement(element)) {
|
||||||
|
2
typings/custom-typings.d.ts
vendored
2
typings/custom-typings.d.ts
vendored
@ -78,6 +78,8 @@ declare module 'rc-form*';
|
|||||||
|
|
||||||
declare module 'react-lazy-load';
|
declare module 'react-lazy-load';
|
||||||
|
|
||||||
|
declare module 'dom-closest';
|
||||||
|
|
||||||
declare module "*.json" {
|
declare module "*.json" {
|
||||||
const value: any;
|
const value: any;
|
||||||
export const version: string;
|
export const version: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user