Skip to content

Commit 0c52052

Browse files
authored
chore: optimize table typescript support (#3087)
* chore: optimize table typescript support * chore: update table type * fix: table type error * fix: columns shuold not have default value * fix: sortDirections and scroll should not have default value * chore: rowSelectionType as tuple
1 parent 71a3691 commit 0c52052

File tree

4 files changed

+58
-44
lines changed

4 files changed

+58
-44
lines changed

components/table/Column.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { defineComponent } from 'vue';
2-
import { ColumnProps } from './interface';
2+
import { columnProps } from './interface';
33

44
export default defineComponent({
55
name: 'ATableColumn',
6-
props: ColumnProps,
6+
props: columnProps,
77
render() {
88
return null;
99
},

components/table/Table.tsx

+26-24
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import initDefaultProps from '../_util/props-util/initDefaultProps';
1717
import BaseMixin from '../_util/BaseMixin';
1818
import { defaultConfigProvider } from '../config-provider';
1919
import {
20-
TableProps,
20+
tableProps,
2121
TableComponents,
2222
TableState,
23-
ITableProps,
24-
IColumnProps,
23+
TableProps,
24+
ColumnProps,
2525
TableStateFilters,
2626
} from './interface';
2727
import Pagination from '../pagination';
@@ -38,15 +38,15 @@ function stopPropagation(e) {
3838
e.stopPropagation();
3939
}
4040

41-
function getRowSelection(props: ITableProps) {
41+
function getRowSelection(props: TableProps) {
4242
return props.rowSelection || {};
4343
}
4444

45-
function getColumnKey(column: IColumnProps, index?: number) {
45+
function getColumnKey(column: ColumnProps, index?: number) {
4646
return column.key || column.dataIndex || index;
4747
}
4848

49-
function isSameColumn(a: IColumnProps, b: IColumnProps): boolean {
49+
function isSameColumn(a: ColumnProps, b: ColumnProps): boolean {
5050
if (a && b && a.key && a.key === b.key) {
5151
return true;
5252
}
@@ -94,16 +94,16 @@ function isTheSameComponents(components1: TableComponents = {}, components2: Tab
9494
);
9595
}
9696

97-
function getFilteredValueColumns(state: TableState, columns?: IColumnProps) {
97+
function getFilteredValueColumns(state: TableState, columns?: ColumnProps) {
9898
return flatFilter(
9999
columns || (state || {}).columns || [],
100-
(column: IColumnProps) => typeof column.filteredValue !== 'undefined',
100+
(column: ColumnProps) => typeof column.filteredValue !== 'undefined',
101101
);
102102
}
103103

104-
function getFiltersFromColumns(state: TableState, columns: IColumnProps) {
104+
function getFiltersFromColumns(state: TableState, columns: ColumnProps) {
105105
const filters = {};
106-
getFilteredValueColumns(state, columns).forEach((col: IColumnProps) => {
106+
getFilteredValueColumns(state, columns).forEach((col: ColumnProps) => {
107107
const colKey = getColumnKey(col);
108108
filters[colKey] = col.filteredValue;
109109
});
@@ -117,26 +117,28 @@ function isFiltersChanged(state: TableState, filters: TableStateFilters[]) {
117117
return Object.keys(filters).some(columnKey => filters[columnKey] !== state.filters[columnKey]);
118118
}
119119

120+
export const defaultTableProps = initDefaultProps(tableProps, {
121+
dataSource: [],
122+
useFixedHeader: false,
123+
// rowSelection: null,
124+
size: 'default',
125+
loading: false,
126+
bordered: false,
127+
indentSize: 20,
128+
locale: {},
129+
rowKey: 'key',
130+
showHeader: true,
131+
sortDirections: ['ascend', 'descend'],
132+
childrenColumnName: 'children',
133+
});
134+
120135
export default defineComponent({
121136
name: 'Table',
122137
mixins: [BaseMixin],
123138
inheritAttrs: false,
124139
Column,
125140
ColumnGroup,
126-
props: initDefaultProps(TableProps, {
127-
dataSource: [],
128-
useFixedHeader: false,
129-
// rowSelection: null,
130-
size: 'default',
131-
loading: false,
132-
bordered: false,
133-
indentSize: 20,
134-
locale: {},
135-
rowKey: 'key',
136-
showHeader: true,
137-
sortDirections: ['ascend', 'descend'],
138-
childrenColumnName: 'children',
139-
}),
141+
props: defaultTableProps,
140142

141143
setup() {
142144
return {

components/table/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { App, defineComponent } from 'vue';
2-
import T from './Table';
2+
import T, { defaultTableProps } from './Table';
33
import Column from './Column';
44
import ColumnGroup from './ColumnGroup';
55
import {} from './interface';
@@ -9,7 +9,7 @@ const Table = defineComponent({
99
name: 'ATable',
1010
Column: T.Column,
1111
ColumnGroup: T.ColumnGroup,
12-
props: T.props,
12+
props: defaultTableProps,
1313
inheritAttrs: false,
1414
methods: {
1515
normalize(elements = []) {

components/table/interface.ts

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
import { ExtractPropTypes, PropType } from 'vue';
12
import PropTypes, { withUndefined } from '../_util/vue-types';
23
import { PaginationProps as getPaginationProps } from '../pagination';
34
import { SpinProps as getSpinProps } from '../spin';
45
import { Store } from './createStore';
56
import { tuple } from '../_util/type';
6-
import { ExtractPropTypes } from 'vue';
77

88
const PaginationProps = getPaginationProps();
99
const SpinProps = getSpinProps();
1010

11-
// export type CompareFn<T> = ((a: T, b: T) => number);
11+
export type CompareFn<T> = (a: T, b: T, sortOrder?: SortOrder) => number;
1212
export const ColumnFilterItem = PropTypes.shape({
1313
text: PropTypes.string,
1414
value: PropTypes.string,
1515
children: PropTypes.array,
1616
}).loose;
1717

18-
export const ColumnProps = {
18+
export const columnProps = {
1919
title: PropTypes.VNodeChild,
2020
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
2121
dataIndex: PropTypes.string,
@@ -52,7 +52,7 @@ export const ColumnProps = {
5252
// onHeaderCell?: (props: ColumnProps<T>) => any;
5353
};
5454

55-
export type IColumnProps = Partial<ExtractPropTypes<typeof ColumnProps>>;
55+
export type ColumnProps = Partial<ExtractPropTypes<typeof columnProps>>;
5656

5757
export interface TableComponents {
5858
table?: any;
@@ -80,10 +80,10 @@ export const TableLocale = PropTypes.shape({
8080
collapse: PropTypes.string,
8181
}).loose;
8282

83-
export const RowSelectionType = PropTypes.oneOf(['checkbox', 'radio']);
83+
export const RowSelectionType = PropTypes.oneOf(tuple('checkbox', 'radio'));
8484
// export type SelectionSelectFn<T> = (record: T, selected: boolean, selectedRows: Object[]) => any;
8585

86-
export const TableRowSelection = {
86+
export const tableRowSelection = {
8787
type: RowSelectionType,
8888
selectedRowKeys: PropTypes.array,
8989
// onChange?: (selectedRowKeys: string[] | number[], selectedRows: Object[]) => any;
@@ -101,10 +101,12 @@ export const TableRowSelection = {
101101
columnTitle: PropTypes.any,
102102
};
103103

104-
export const TableProps = {
104+
export type SortOrder = 'descend' | 'ascend';
105+
106+
export const tableProps = {
105107
prefixCls: PropTypes.string,
106108
dropdownPrefixCls: PropTypes.string,
107-
rowSelection: PropTypes.oneOfType([PropTypes.shape(TableRowSelection).loose, Object]),
109+
rowSelection: PropTypes.oneOfType([PropTypes.shape(tableRowSelection).loose, Object]),
108110
pagination: withUndefined(
109111
PropTypes.oneOfType([
110112
PropTypes.shape({
@@ -117,7 +119,9 @@ export const TableProps = {
117119
size: PropTypes.oneOf(tuple('default', 'middle', 'small', 'large')),
118120
dataSource: PropTypes.array,
119121
components: PropTypes.object,
120-
columns: PropTypes.array,
122+
columns: {
123+
type: Array as PropType<ColumnProps>,
124+
},
121125
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
122126
rowClassName: PropTypes.func,
123127
expandedRowRender: PropTypes.any,
@@ -137,10 +141,18 @@ export const TableProps = {
137141
showHeader: PropTypes.looseBool,
138142
footer: PropTypes.func,
139143
title: PropTypes.func,
140-
scroll: PropTypes.object,
144+
scroll: {
145+
type: Object as PropType<{
146+
x?: boolean | number | string;
147+
y?: boolean | number | string;
148+
scrollToFirstRowOnChange?: boolean;
149+
}>,
150+
},
141151
childrenColumnName: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
142-
bodyStyle: PropTypes.any,
143-
sortDirections: PropTypes.array,
152+
bodyStyle: PropTypes.style,
153+
sortDirections: {
154+
type: Array as PropType<SortOrder[]>,
155+
},
144156
tableLayout: PropTypes.string,
145157
getPopupContainer: PropTypes.func,
146158
expandIcon: PropTypes.func,
@@ -153,9 +165,9 @@ export const TableProps = {
153165
// children?: React.ReactNode;
154166
};
155167

156-
export type ITableRowSelection = Partial<ExtractPropTypes<typeof TableRowSelection>>;
168+
export type TableRowSelection = Partial<ExtractPropTypes<typeof tableRowSelection>>;
157169

158-
export type ITableProps = Partial<ExtractPropTypes<typeof TableProps>>;
170+
export type TableProps = Partial<ExtractPropTypes<typeof tableProps>>;
159171

160172
export interface TableStateFilters {
161173
[key: string]: string[];
@@ -164,9 +176,9 @@ export interface TableStateFilters {
164176
export interface TableState {
165177
pagination?: Partial<ExtractPropTypes<typeof PaginationProps>>;
166178
filters?: TableStateFilters;
167-
sortColumn?: Partial<ExtractPropTypes<typeof ColumnProps>> | null;
179+
sortColumn?: ColumnProps | null;
168180
sortOrder?: string;
169-
columns?: IColumnProps[];
181+
columns?: ColumnProps[];
170182
}
171183

172184
// export type SelectionItemSelectFn = (key: string[]) => any;

0 commit comments

Comments
 (0)