diff --git a/document/components/docs/en-US/recycle-list.md b/document/components/docs/en-US/recycle-list.md index f04786623e7d4f55127c983d0998f97fa9415e30..09594f763e5c76308a21379e22d4e35a2deae921 100644 --- a/document/components/docs/en-US/recycle-list.md +++ b/document/components/docs/en-US/recycle-list.md @@ -144,3 +144,9 @@ The `onFetch` function must return a Promise, and the first argument to Promise' | item | Scope slot for each data item | data: An item in items | | spinner | Named slot for loading more | - | | noMore | Named slot for no more data | - | + +### Instance methods + +| Method name | Description | +| - | - | +| reset | To clean up all the contents | diff --git a/document/components/docs/zh-CN/recycle-list.md b/document/components/docs/zh-CN/recycle-list.md index f7d93eac8fd84c482321ff7c006a3733f4fb301b..91aa49fe8534ceb0cbbfd615bebee2a27743ba94 100644 --- a/document/components/docs/zh-CN/recycle-list.md +++ b/document/components/docs/zh-CN/recycle-list.md @@ -144,3 +144,9 @@ | item | 列表某项的作用域插槽,通过此插槽实现想要的渲染视图 | data: 调用方传入的数据项集合的某一项 | | spinner | 加载更多的提示文案的具名插槽 | - | | noMore | 无更多数据的提示文案的具名插槽 | - | + +### 实例方法 + +| 方法名 | 说明 | +| - | - | +| reset | 清空列表全部内容,重置数据 | diff --git a/src/components/recycle-list/recycle-list.vue b/src/components/recycle-list/recycle-list.vue index 3106cc67f3cc7db779d16fbb11364314be46776f..86b0094011a447dd1fed47e9a1fec96d4898ab44 100644 --- a/src/components/recycle-list/recycle-list.vue +++ b/src/components/recycle-list/recycle-list.vue @@ -263,6 +263,43 @@ } } }, + reset () { + const map = [ + { + key: 'items', + value: [] + }, + { + key: 'heights', + value: 0 + }, + { + key: 'startIndex', + value: 0 + }, + { + key: 'loadings', + value: [] + }, + { + key: 'noMore', + value: false + }, + { + key: 'list', + value: [] + }, + { + key: 'promiseStack', + value: [] + } + ] + map.forEach(({ key, value }) => { + this[key] = value + }) + this.$el.scrollTop = 0 + this.load() + }, _onScroll() { // trigger load if (!this.noMore && this.$el.scrollTop + this.$el.offsetHeight > this.heights - this.offset) { diff --git a/test/unit/specs/recycle-list.spec.js b/test/unit/specs/recycle-list.spec.js index c772960f888336f0bc72bc588599f2d1a9f43922..fb00441974a812d642bb8416387619c5102bfa07 100644 --- a/test/unit/specs/recycle-list.spec.js +++ b/test/unit/specs/recycle-list.spec.js @@ -47,6 +47,21 @@ describe('RecycleList', () => { }, 500) }, 500) }) + it('should clean all contents', (done) => { + vm = createRecycleList(true) + setTimeout(() => { + vm.$parent.reset() + vm.reset() + + expect(vm.list.length) + .to.equal(0) + setTimeout(() => { + expect(vm.list.length) + .to.equal(10) + done() + }, 500) + }, 500) + }) it('should call correct method', (done) => { vm = createRecycleList() setTimeout(() => { @@ -82,6 +97,9 @@ function createRecycleList (infinite) { } }, methods: { + reset () { + this.pid = 0 + }, onFetch () { let items = [] this.pid += 1