Skip to content

Commit

Permalink
Table: set toggleAllSelection as instance property (ElemeFE#17137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyoung authored and oleksiikhr committed Jan 13, 2020
1 parent fb2d476 commit 169e6a5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/checkbox/src/checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'is-focus': focus
}"
:tabindex="indeterminate ? 0 : false"
:role="indeterminate ? checkbox : false"
:role="indeterminate ? 'checkbox' : false"
:aria-checked="indeterminate ? 'mixed' : false"
>
<span class="el-checkbox__inner"></span>
Expand Down
4 changes: 4 additions & 0 deletions packages/table/src/store/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Store from './index';
import debounce from 'throttle-debounce/debounce';

export function createStore(table, initialState = {}) {
if (!table) {
Expand All @@ -7,6 +8,9 @@ export function createStore(table, initialState = {}) {

const store = new Store();
store.table = table;
// fix https://github.com/ElemeFE/element/issues/14075
// related pr https://github.com/ElemeFE/element/pull/14146
store.toggleAllSelection = debounce(10, store._toggleAllSelection);
Object.keys(initialState).forEach(key => {
store.states[key] = initialState[key];
});
Expand Down
5 changes: 2 additions & 3 deletions packages/table/src/store/watcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Vue from 'vue';
import debounce from 'throttle-debounce/debounce';
import merge from 'element-ui/src/utils/merge';
import { getKeysMap, getRowIdentity, getColumnById, getColumnByKey, orderBy, toggleRowStatus } from '../util';
import expand from './expand';
Expand Down Expand Up @@ -168,7 +167,7 @@ export default Vue.extend({
}
},

toggleAllSelection: debounce(10, function() {
_toggleAllSelection() {
const states = this.states;
const { data = [], selection } = states;
// when only some rows are selected (but not all), select or deselect all of them
Expand All @@ -195,7 +194,7 @@ export default Vue.extend({
this.table.$emit('selection-change', selection ? selection.slice() : []);
}
this.table.$emit('select-all', selection);
}),
},

updateSelectionByRowKey() {
const states = this.states;
Expand Down
48 changes: 43 additions & 5 deletions test/unit/specs/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,8 @@ describe('Table', () => {
</el-table>
`,

created() {
this.testData = getTestData();
},

data() {
return { testData: this.testData };
return { testData: getTestData() };
}
});

Expand Down Expand Up @@ -1716,6 +1712,48 @@ describe('Table', () => {
}, 50);
});

it('toggleAllSelection debounce', async() => {
const spy = sinon.spy();
const vm = createVue({
template: `
<div>
<el-table ref="table" :data="testData" @selection-change="change">
<el-table-column type="selection" />
<el-table-column prop="name" />
</el-table>
<el-table ref="table1" :data="testData1" @selection-change="change">
<el-table-column type="selection" />
<el-table-column prop="name" />
</el-table>
</div>
`,

data() {
return {
testData: getTestData(),
testData1: getTestData()
};
},

methods: {
change(selection) {
spy(selection);
}
},

mounted() {
this.$refs.table.toggleAllSelection();
this.$refs.table1.toggleAllSelection();
}
}, true);

await wait(50);
expect(spy.callCount).to.be.equal(2);
expect(spy.args[0][0].length).to.be.equal(5);
expect(spy.args[1][0].length).to.be.equal(5);
destroyVM(vm);
});

it('clearSelection', () => {
const vm = createTable('selection-change');
vm.$refs.table.toggleRowSelection(vm.testData[0]);
Expand Down

0 comments on commit 169e6a5

Please sign in to comment.