refactor: table ts (#2664)

* refactor: table ts

* fix: should pass context to onChange correctly
pull/2694/head
Benjy Cui 9 years ago committed by 偏右
parent 2ed192db52
commit 978b9f98e7

@ -1,7 +1,14 @@
import * as React from 'react'; import * as React from 'react';
import RcDropdown from 'rc-dropdown'; import RcDropdown from 'rc-dropdown';
export default class Dropdown extends React.Component { export interface DropDownProps {
trigger: string[];
overlay: React.ReactNode;
visible: boolean;
onVisibleChange: (visible: boolean) => void;
}
export default class Dropdown extends React.Component<DropDownProps, any> {
static defaultProps = { static defaultProps = {
transitionName: 'slide-up', transitionName: 'slide-up',
prefixCls: 'ant-dropdown', prefixCls: 'ant-dropdown',

@ -14,6 +14,7 @@ export interface RadioProps {
prefixCls?: string; prefixCls?: string;
disabled?: boolean; disabled?: boolean;
className?: string; className?: string;
onChange?: (e: any) => any;
} }
export default class Radio extends React.Component<RadioProps, any> { export default class Radio extends React.Component<RadioProps, any> {

@ -32,7 +32,66 @@ const defaultPagination = {
onShowSizeChange: noop, onShowSizeChange: noop,
}; };
export default class Table extends React.Component { interface TableRowSelection {
type?: 'checkbox' | 'radio';
selectedRowKeys?: string[];
onChange?: (selectedRowKeys: string[], selectedRows: Object[]) => any;
getCheckboxProps?: (record: Object) => Object;
onSelect?: (record: Object, selected: boolean, selectedRows: Object[]) => any;
onSelectAll?: (selected: boolean, selectedRows: Object[], changeRows: Object[]) => any;
}
interface TableColumnConfig {
title?: React.ReactNode;
key?: string;
dataIndex?: string;
render?: (text: any, record: Object, index: number) => React.ReactNode;
filters?: string[];
onFilter?: (value: any, record: Object) => boolean;
filterMultiple?: boolean;
filterDropdown?: React.ReactNode;
sorter?: boolean | ((a: any, b: any) => number);
colSpan?: number;
width?: string | number;
className?: string;
fixed?: boolean | ('left' | 'right');
filteredValue?: any[];
sortOrder?: boolean | ('ascend' | 'descend');
}
export interface TableProps {
prefixCls?: string;
rowSelection?: TableRowSelection;
pagination?: any; // 等 Pagination 的 interface以便直接引用
size?: 'default' | 'small';
dataSource?: Object[];
columns?: TableColumnConfig[];
rowKey?: string | ((record: Object, index: number) => string);
rowClassName?: (record: Object, index: number) => string;
expandedRowRender?: any;
defaultExpandedRowKeys?: string[];
expandedRowKeys?: string[];
expandIconAsCell?: boolean;
onChange?: (pagination: any, filters: string[], sorter: Object) => any;
loading?: boolean;
locale?: Object;
indentSize?: number;
onRowClick?: (record: Object, index: number) => any;
useFixedHeader?: boolean;
bordered?: boolean;
showHeader?: boolean;
footer?: (currentPageData: Object[]) => React.ReactNode;
title?: (currentPageData: Object[]) => React.ReactNode;
scroll?: { x?: boolean | number, y?: boolean | number};
}
interface TableContext {
antLocale?: {
Table?: any,
};
}
export default class Table extends React.Component<TableProps, any> {
static propTypes = { static propTypes = {
dataSource: React.PropTypes.array, dataSource: React.PropTypes.array,
prefixCls: React.PropTypes.string, prefixCls: React.PropTypes.string,
@ -65,6 +124,9 @@ export default class Table extends React.Component {
antLocale: React.PropTypes.object, antLocale: React.PropTypes.object,
}; };
context: TableContext;
CheckboxPropsCache: Object;
constructor(props) { constructor(props) {
super(props); super(props);
@ -165,7 +227,7 @@ export default class Table extends React.Component {
} }
} }
setSelectedRowKeys(selectedRowKeys, { selectWay, record, checked, changeRowKeys }) { setSelectedRowKeys(selectedRowKeys, { selectWay, record, checked, changeRowKeys }: any) {
const { rowSelection = {} } = this.props; const { rowSelection = {} } = this.props;
if (rowSelection && !('selectedRowKeys' in rowSelection)) { if (rowSelection && !('selectedRowKeys' in rowSelection)) {
this.setState({ selectedRowKeys }); this.setState({ selectedRowKeys });
@ -208,15 +270,15 @@ export default class Table extends React.Component {
return filtersChanged; return filtersChanged;
} }
getSortOrderColumns(columns) { getSortOrderColumns(columns?) {
return (columns || this.props.columns || []).filter(column => 'sortOrder' in column); return (columns || this.props.columns || []).filter(column => 'sortOrder' in column);
} }
getFilteredValueColumns(columns) { getFilteredValueColumns(columns?) {
return (columns || this.props.columns || []).filter(column => 'filteredValue' in column); return (columns || this.props.columns || []).filter(column => 'filteredValue' in column);
} }
getFiltersFromColumns(columns) { getFiltersFromColumns(columns?) {
let filters = {}; let filters = {};
this.getFilteredValueColumns(columns).forEach(col => { this.getFilteredValueColumns(columns).forEach(col => {
filters[this.getColumnKey(col)] = col.filteredValue; filters[this.getColumnKey(col)] = col.filteredValue;
@ -224,7 +286,7 @@ export default class Table extends React.Component {
return filters; return filters;
} }
getSortStateFromColumns(columns) { getSortStateFromColumns(columns?) {
// return fisrt column which sortOrder is not falsy // return fisrt column which sortOrder is not falsy
const sortedColumn = const sortedColumn =
this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0]; this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];
@ -280,7 +342,7 @@ export default class Table extends React.Component {
this.setState(newState); this.setState(newState);
} }
this.props.onChange(...this.prepareParamsArguments(assign({}, this.state, newState))); this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, newState)));
} }
handleFilter = (column, nextFilters) => { handleFilter = (column, nextFilters) => {
@ -306,6 +368,7 @@ export default class Table extends React.Component {
const newState = { const newState = {
selectionDirty: false, selectionDirty: false,
pagination, pagination,
filters: null,
}; };
const filtersToSetState = assign({}, filters); const filtersToSetState = assign({}, filters);
// Remove filters which is controlled // Remove filters which is controlled
@ -327,7 +390,7 @@ export default class Table extends React.Component {
} }
this.setState(newState, () => { this.setState(newState, () => {
props.onChange(...this.prepareParamsArguments(assign({}, this.state, { props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false, selectionDirty: false,
filters, filters,
pagination, pagination,
@ -429,7 +492,7 @@ export default class Table extends React.Component {
} }
this.setState(newState); this.setState(newState);
this.props.onChange(...this.prepareParamsArguments(assign({}, this.state, { this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false, selectionDirty: false,
pagination, pagination,
}))); })));
@ -476,12 +539,12 @@ export default class Table extends React.Component {
); );
} }
getRecordKey(record, index) { getRecordKey(record, index?) {
const { rowKey } = this.props; const rowKey = this.props.rowKey;
if (typeof rowKey === 'function') { if (typeof rowKey === 'function') {
return rowKey(record, index); return rowKey(record, index);
} }
return record[rowKey] || index; return record[rowKey as string] || index;
} }
renderRowSelection() { renderRowSelection() {
@ -540,7 +603,7 @@ export default class Table extends React.Component {
return columns; return columns;
} }
getColumnKey(column, index) { getColumnKey(column, index?) {
return column.key || column.dataIndex || index; return column.key || column.dataIndex || index;
} }
@ -622,7 +685,7 @@ export default class Table extends React.Component {
pagination.onShowSizeChange(current, pageSize); pagination.onShowSizeChange(current, pageSize);
const nextPagination = assign({}, pagination, { pageSize, current }); const nextPagination = assign({}, pagination, { pageSize, current });
this.setState({ pagination: nextPagination }); this.setState({ pagination: nextPagination });
this.props.onChange(...this.prepareParamsArguments(assign({}, this.state, { this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
pagination: nextPagination, pagination: nextPagination,
}))); })));
} }
@ -652,11 +715,11 @@ export default class Table extends React.Component {
/> : null; /> : null;
} }
prepareParamsArguments(state) { prepareParamsArguments(state: any): [any, string[], Object] {
// 准备筛选、排序、分页的参数 // 准备筛选、排序、分页的参数
const pagination = state.pagination; const pagination = state.pagination;
const filters = state.filters; const filters = state.filters;
const sorter = {}; const sorter: any = {};
if (state.sortColumn && state.sortOrder) { if (state.sortColumn && state.sortOrder) {
sorter.column = state.sortColumn; sorter.column = state.sortColumn;
sorter.order = state.sortOrder; sorter.order = state.sortOrder;

@ -9,7 +9,18 @@ const FilterDropdownMenuWrapper = ({ onClick, children }) => (
<div className="ant-table-filter-dropdown" onClick={onClick}>{children}</div> <div className="ant-table-filter-dropdown" onClick={onClick}>{children}</div>
); );
export default class FilterMenu extends React.Component { export interface FilterMenuProps {
locale: any;
selectedKeys: string[];
column: {
filterMultiple?: boolean,
filterDropdown?: React.ReactNode,
filters?: string[]
};
confirmFilter: (column: Object, selectedKeys: string[]) => any;
}
export default class FilterMenu extends React.Component<FilterMenuProps, any> {
static defaultProps = { static defaultProps = {
handleFilter() {}, handleFilter() {},
column: null, column: null,

@ -151,6 +151,8 @@ declare module 'rc-progress' {
} }
declare module 'rc-menu' { declare module 'rc-menu' {
export const SubMenu: any;
export const Item: any;
export default function(): any; export default function(): any;
} }

Loading…
Cancel
Save