Skip to content

Commit e6b7aa4

Browse files
committed
perf: table #3531
1 parent f589c8f commit e6b7aa4

23 files changed

+223
-549
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ components/style/color/*.less
2828
.huskyrc
2929
.gitmodules
3030
*.png
31+
v2-doc/
3132

components/_util/store/PropTypes.js

-7
This file was deleted.

components/_util/store/Provider.jsx

-15
This file was deleted.

components/_util/store/connect.jsx

-108
This file was deleted.

components/_util/store/create.js

-30
This file was deleted.

components/_util/store/index.js

-5
This file was deleted.

components/table/SelectionBox.tsx

+13-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent } from 'vue';
1+
import { computed, defineComponent } from 'vue';
22
import Checkbox from '../checkbox';
33
import Radio from '../radio';
44
import { SelectionBoxProps } from './interface';
@@ -11,52 +11,21 @@ export default defineComponent({
1111
inheritAttrs: false,
1212
props: SelectionBoxProps,
1313

14-
setup() {
14+
setup(props) {
1515
return {
16-
unsubscribe: null,
16+
checked: computed(() => {
17+
const { store, defaultSelection, rowIndex } = props;
18+
let checked = false;
19+
if (store.selectionDirty) {
20+
checked = store.selectedRowKeys.indexOf(rowIndex) >= 0;
21+
} else {
22+
checked =
23+
store.selectedRowKeys.indexOf(rowIndex) >= 0 || defaultSelection.indexOf(rowIndex) >= 0;
24+
}
25+
return checked;
26+
}),
1727
};
1828
},
19-
data() {
20-
return {
21-
checked: false,
22-
};
23-
},
24-
25-
created() {
26-
this.checked = this.getCheckState(this.$props);
27-
},
28-
29-
mounted() {
30-
this.subscribe();
31-
},
32-
33-
beforeUnmount() {
34-
if (this.unsubscribe) {
35-
this.unsubscribe();
36-
}
37-
},
38-
methods: {
39-
getCheckState(props): boolean {
40-
const { store, defaultSelection, rowIndex } = props;
41-
let checked = false;
42-
if (store.getState().selectionDirty) {
43-
checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0;
44-
} else {
45-
checked =
46-
store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 ||
47-
defaultSelection.indexOf(rowIndex) >= 0;
48-
}
49-
return checked;
50-
},
51-
subscribe() {
52-
const { store } = this;
53-
this.unsubscribe = store.subscribe(() => {
54-
const checked = this.getCheckState(this.$props);
55-
this.setState({ checked });
56-
});
57-
},
58-
},
59-
6029
render() {
6130
const { type, rowIndex, ...rest } = { ...getOptionProps(this), ...this.$attrs } as any;
6231
const { checked } = this;

components/table/SelectionCheckboxAll.tsx

+11-66
Original file line numberDiff line numberDiff line change
@@ -5,7 +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';
8+
import { computed, defineComponent } from 'vue';
99

1010
function checkSelection({
1111
store,
@@ -17,7 +17,7 @@ function checkSelection({
1717
}) {
1818
return byDefaultChecked
1919
? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked)
20-
: data[type]((item, i) => store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0);
20+
: data[type]((item, i) => store.selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0);
2121
}
2222

2323
function getIndeterminateState(props) {
@@ -53,7 +53,7 @@ function getIndeterminateState(props) {
5353
byDefaultChecked: true,
5454
});
5555

56-
if (store.getState().selectionDirty) {
56+
if (store.selectionDirty) {
5757
return someCheckedNotByDefaultChecked;
5858
}
5959
return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked;
@@ -64,7 +64,7 @@ function getCheckState(props) {
6464
if (!data.length) {
6565
return false;
6666
}
67-
if (store.getState().selectionDirty) {
67+
if (store.selectionDirty) {
6868
return checkSelection({
6969
...props,
7070
data,
@@ -94,29 +94,18 @@ export default defineComponent({
9494
inheritAttrs: false,
9595
props: SelectionCheckboxAllProps,
9696

97-
setup() {
97+
setup(props) {
9898
return {
9999
defaultSelections: [],
100-
unsubscribe: null,
101-
};
102-
},
103-
data() {
104-
const { $props: props } = this;
105-
106-
return {
107-
checked: getCheckState(props),
108-
indeterminate: getIndeterminateState(props),
100+
checked: computed(() => {
101+
return getCheckState(props);
102+
}),
103+
indeterminate: computed(() => {
104+
return getIndeterminateState(props);
105+
}),
109106
};
110107
},
111108

112-
watch: {
113-
propsSymbol: {
114-
handler() {
115-
this.setCheckState(this.$props);
116-
},
117-
},
118-
},
119-
120109
created() {
121110
const { $props: props } = this;
122111
this.defaultSelections = props.hideDefaultSelections
@@ -132,55 +121,11 @@ export default defineComponent({
132121
},
133122
];
134123
},
135-
136-
mounted() {
137-
this.subscribe();
138-
},
139-
140-
beforeUnmount() {
141-
if (this.unsubscribe) {
142-
this.unsubscribe();
143-
}
144-
},
145124
methods: {
146-
checkSelection(props, data, type, byDefaultChecked) {
147-
const { store, getCheckboxPropsByItem, getRecordKey } = props || this.$props;
148-
// type should be 'every' | 'some'
149-
if (type === 'every' || type === 'some') {
150-
return byDefaultChecked
151-
? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked)
152-
: data[type](
153-
(item, i) => store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0,
154-
);
155-
}
156-
return false;
157-
},
158-
159-
setCheckState(props) {
160-
const checked = getCheckState(props);
161-
const indeterminate = getIndeterminateState(props);
162-
this.setState(prevState => {
163-
const newState: any = {};
164-
if (indeterminate !== prevState.indeterminate) {
165-
newState.indeterminate = indeterminate;
166-
}
167-
if (checked !== prevState.checked) {
168-
newState.checked = checked;
169-
}
170-
return newState;
171-
});
172-
},
173-
174125
handleSelectAllChange(e) {
175126
const { checked } = e.target;
176127
this.$emit('select', checked ? 'all' : 'removeAll', 0, null);
177128
},
178-
subscribe() {
179-
const { store } = this;
180-
this.unsubscribe = store.subscribe(() => {
181-
this.setCheckState(this.$props);
182-
});
183-
},
184129

185130
renderMenus(selections) {
186131
return selections.map((selection, index) => {

0 commit comments

Comments
 (0)