Skip to content

Commit f8ada77

Browse files
committed
refactor: table to ts
1 parent f8750f2 commit f8ada77

17 files changed

+170
-102
lines changed

components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const components = [
213213

214214
const install = function(app: App) {
215215
components.forEach(component => {
216-
app.use(component as { install: () => any });
216+
app.use(component as typeof component & { install: () => void });
217217
});
218218

219219
app.config.globalProperties.$message = message;
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { defineComponent } from 'vue';
12
import { ColumnProps } from './interface';
23

3-
export default {
4+
export default defineComponent({
45
name: 'ATableColumn',
56
props: ColumnProps,
67
render() {
78
return null;
89
},
9-
};
10+
});
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { defineComponent } from 'vue';
12
import PropTypes from '../_util/vue-types';
23

3-
export default {
4+
export default defineComponent({
45
name: 'ATableColumnGroup',
56
props: {
67
title: PropTypes.any,
@@ -9,4 +10,4 @@ export default {
910
render() {
1011
return null;
1112
},
12-
};
13+
});

components/table/FilterDropdownMenuWrapper.jsx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { FunctionalComponent } from 'vue';
2+
3+
export interface FilterDropdownMenuWrapperProps {
4+
class?: string;
5+
}
6+
7+
const FilterDropdownMenuWrapper: FunctionalComponent<FilterDropdownMenuWrapperProps> = (
8+
props,
9+
{ slots },
10+
) => {
11+
return (
12+
<div class={props.class} onClick={e => e.stopPropagation()}>
13+
{slots.default?.()}
14+
</div>
15+
);
16+
};
17+
18+
FilterDropdownMenuWrapper.inheritAttrs = false;
19+
20+
export default FilterDropdownMenuWrapper;

components/table/SelectionBox.jsx renamed to components/table/SelectionBox.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
import { defineComponent } from 'vue';
12
import Checkbox from '../checkbox';
23
import Radio from '../radio';
34
import { SelectionBoxProps } from './interface';
45
import BaseMixin from '../_util/BaseMixin';
56
import { getOptionProps } from '../_util/props-util';
67

7-
export default {
8+
export default defineComponent({
89
name: 'SelectionBox',
910
mixins: [BaseMixin],
1011
inheritAttrs: false,
1112
props: SelectionBoxProps,
1213
data() {
1314
return {
14-
checked: this.getCheckState(this.$props),
15+
checked: false,
1516
};
1617
},
1718

19+
setup() {
20+
return {
21+
unsubscribe: null,
22+
};
23+
},
24+
25+
created() {
26+
this.checked = this.getCheckState(this.$props);
27+
},
28+
1829
mounted() {
1930
this.subscribe();
2031
},
@@ -25,7 +36,7 @@ export default {
2536
}
2637
},
2738
methods: {
28-
getCheckState(props) {
39+
getCheckState(props): boolean {
2940
const { store, defaultSelection, rowIndex } = props;
3041
let checked = false;
3142
if (store.getState().selectionDirty) {
@@ -47,7 +58,7 @@ export default {
4758
},
4859

4960
render() {
50-
const { type, rowIndex, ...rest } = { ...getOptionProps(this), ...this.$attrs };
61+
const { type, rowIndex, ...rest } = { ...getOptionProps(this), ...this.$attrs } as any;
5162
const { checked } = this;
5263
const checkboxProps = {
5364
checked,
@@ -59,4 +70,4 @@ export default {
5970
}
6071
return <Checkbox {...checkboxProps} />;
6172
},
62-
};
73+
});

components/table/SelectionCheckboxAll.jsx renamed to components/table/SelectionCheckboxAll.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Menu from '../menu';
55
import classNames from '../_util/classNames';
66
import { SelectionCheckboxAllProps } from './interface';
77
import BaseMixin from '../_util/BaseMixin';
8+
import { defineComponent } from 'vue';
89

910
function checkSelection({
1011
store,
@@ -87,13 +88,29 @@ function getCheckState(props) {
8788
);
8889
}
8990

90-
export default {
91+
export default defineComponent({
9192
name: 'SelectionCheckboxAll',
9293
mixins: [BaseMixin],
9394
inheritAttrs: false,
9495
props: SelectionCheckboxAllProps,
9596
data() {
9697
const { $props: props } = this;
98+
99+
return {
100+
checked: getCheckState(props),
101+
indeterminate: getIndeterminateState(props),
102+
};
103+
},
104+
105+
setup() {
106+
return {
107+
defaultSelections: [],
108+
unsubscribe: null,
109+
};
110+
},
111+
112+
created() {
113+
const { $props: props } = this;
97114
this.defaultSelections = props.hideDefaultSelections
98115
? []
99116
: [
@@ -106,11 +123,6 @@ export default {
106123
text: props.locale.selectInvert,
107124
},
108125
];
109-
110-
return {
111-
checked: getCheckState(props),
112-
indeterminate: getIndeterminateState(props),
113-
};
114126
},
115127

116128
watch: {
@@ -149,7 +161,7 @@ export default {
149161
const checked = getCheckState(props);
150162
const indeterminate = getIndeterminateState(props);
151163
this.setState(prevState => {
152-
const newState = {};
164+
const newState: any = {};
153165
if (indeterminate !== prevState.indeterminate) {
154166
newState.indeterminate = indeterminate;
155167
}
@@ -229,4 +241,4 @@ export default {
229241
</div>
230242
);
231243
},
232-
};
244+
});

components/table/Table.jsx renamed to components/table/Table.tsx

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, markRaw } from 'vue';
1+
import { defineComponent, inject, markRaw } from 'vue';
22
import CaretUpFilled from '@ant-design/icons-vue/CaretUpFilled';
33
import CaretDownFilled from '@ant-design/icons-vue/CaretDownFilled';
44
import VcTable, { INTERNAL_COL_DEFINE } from '../vc-table';
@@ -12,10 +12,18 @@ import Column from './Column';
1212
import ColumnGroup from './ColumnGroup';
1313
import createBodyRow from './createBodyRow';
1414
import { flatArray, treeMap, flatFilter } from './util';
15-
import { initDefaultProps, getOptionProps } from '../_util/props-util';
15+
import { getOptionProps } from '../_util/props-util';
16+
import initDefaultProps from '../_util/props-util/initDefaultProps';
1617
import BaseMixin from '../_util/BaseMixin';
1718
import { defaultConfigProvider } from '../config-provider';
18-
import { TableProps } from './interface';
19+
import {
20+
TableProps,
21+
TableComponents,
22+
TableState,
23+
ITableProps,
24+
IColumnProps,
25+
TableStateFilters,
26+
} from './interface';
1927
import Pagination from '../pagination';
2028
import Spin from '../spin';
2129
import LocaleReceiver from '../locale-provider/LocaleReceiver';
@@ -30,15 +38,15 @@ function stopPropagation(e) {
3038
e.stopPropagation();
3139
}
3240

33-
function getRowSelection(props) {
41+
function getRowSelection(props: ITableProps) {
3442
return props.rowSelection || {};
3543
}
3644

37-
function getColumnKey(column, index) {
45+
function getColumnKey(column: IColumnProps, index?: number) {
3846
return column.key || column.dataIndex || index;
3947
}
4048

41-
function isSameColumn(a, b) {
49+
function isSameColumn(a: IColumnProps, b: IColumnProps): boolean {
4250
if (a && b && a.key && a.key === b.key) {
4351
return true;
4452
}
@@ -68,7 +76,7 @@ const defaultPagination = {
6876
*/
6977
const emptyObject = {};
7078

71-
const createComponents = (components = {}) => {
79+
const createComponents = (components: TableComponents = {}) => {
7280
const bodyRow = components && components.body && components.body.row;
7381
return {
7482
...components,
@@ -79,37 +87,37 @@ const createComponents = (components = {}) => {
7987
};
8088
};
8189

82-
function isTheSameComponents(components1 = {}, components2 = {}) {
90+
function isTheSameComponents(components1: TableComponents = {}, components2: TableComponents = {}) {
8391
return (
8492
components1 === components2 ||
8593
['table', 'header', 'body'].every(key => shallowEqual(components1[key], components2[key]))
8694
);
8795
}
8896

89-
function getFilteredValueColumns(state, columns) {
97+
function getFilteredValueColumns(state: TableState, columns?: IColumnProps) {
9098
return flatFilter(
9199
columns || (state || {}).columns || [],
92-
column => typeof column.filteredValue !== 'undefined',
100+
(column: IColumnProps) => typeof column.filteredValue !== 'undefined',
93101
);
94102
}
95103

96-
function getFiltersFromColumns(state, columns) {
104+
function getFiltersFromColumns(state: TableState, columns: IColumnProps) {
97105
const filters = {};
98-
getFilteredValueColumns(state, columns).forEach(col => {
106+
getFilteredValueColumns(state, columns).forEach((col: IColumnProps) => {
99107
const colKey = getColumnKey(col);
100108
filters[colKey] = col.filteredValue;
101109
});
102110
return filters;
103111
}
104112

105-
function isFiltersChanged(state, filters) {
113+
function isFiltersChanged(state: TableState, filters: TableStateFilters[]) {
106114
if (Object.keys(filters).length !== Object.keys(state.filters).length) {
107115
return true;
108116
}
109117
return Object.keys(filters).some(columnKey => filters[columnKey] !== state.filters[columnKey]);
110118
}
111119

112-
export default {
120+
export default defineComponent({
113121
name: 'Table',
114122
mixins: [BaseMixin],
115123
inheritAttrs: false,
@@ -132,24 +140,19 @@ export default {
132140

133141
setup() {
134142
return {
143+
vcTable: null,
144+
checkboxPropsCache: {},
145+
store: null,
135146
configProvider: inject('configProvider', defaultConfigProvider),
136147
};
137148
},
138149

139150
data() {
140-
this.vcTable = null;
141-
// this.columns = props.columns || normalizeColumns(props.children)
142151
const props = getOptionProps(this);
143152
warning(
144153
!props.expandedRowRender || !('scroll' in props),
145154
'`expandedRowRender` and `scroll` are not compatible. Please use one of them at one time.',
146155
);
147-
this.checkboxPropsCache = {};
148-
149-
this.store = createStore({
150-
selectedRowKeys: getRowSelection(this.$props).selectedRowKeys || [],
151-
selectionDirty: false,
152-
});
153156
return {
154157
...this.getDefaultSortOrder(props.columns || []),
155158
// 减少状态
@@ -160,6 +163,13 @@ export default {
160163
filterDataCnt: 0,
161164
};
162165
},
166+
created() {
167+
const props = getOptionProps(this);
168+
this.store = createStore({
169+
selectedRowKeys: getRowSelection(props).selectedRowKeys || [],
170+
selectionDirty: false,
171+
});
172+
},
163173
watch: {
164174
pagination: {
165175
handler(val) {
@@ -847,7 +857,7 @@ export default {
847857
delete pagination.onChange;
848858
delete pagination.onShowSizeChange;
849859
const filters = state.sFilters;
850-
const sorter = {};
860+
const sorter: any = {};
851861
let currentColumn = column;
852862
if (state.sSortColumn && state.sSortOrder) {
853863
currentColumn = state.sSortColumn;
@@ -1088,7 +1098,7 @@ export default {
10881098
</div>
10891099
);
10901100
customHeaderCell = col => {
1091-
let colProps = {};
1101+
let colProps: any = {};
10921102
// Get original first
10931103
if (column.customHeaderCell) {
10941104
colProps = {
@@ -1153,7 +1163,7 @@ export default {
11531163
const { showHeader, locale, getPopupContainer, ...restProps } = {
11541164
...getOptionProps(this),
11551165
...this.$attrs,
1156-
};
1166+
} as any;
11571167
const data = this.getCurrentPageData();
11581168
const expandIconAsCell = this.expandedRowRender && this.expandIconAsCell !== false;
11591169

@@ -1278,4 +1288,4 @@ export default {
12781288
</div>
12791289
);
12801290
},
1281-
};
1291+
});

0 commit comments

Comments
 (0)