From e9537e4681a36f0bf14d99fe019620894151dcc0 Mon Sep 17 00:00:00 2001 From: JiZhi <471695625@qq.com> Date: Wed, 27 Feb 2019 10:54:50 +0800 Subject: [PATCH] Recycle list add api (#417) * docs(recycle-list): add clean methods * feat(recycle-list): add clean methods * test(recycle-list): test clean methods * docs(recycle-list): change clean to reset * feat(recycle-list): change clean method to reset method * test(recycle-list): test list length of recycle-list * test(recycle-list): optimize unit case --- .../components/docs/en-US/recycle-list.md | 6 +++ .../components/docs/zh-CN/recycle-list.md | 6 +++ src/components/recycle-list/recycle-list.vue | 37 +++++++++++++++++++ test/unit/specs/recycle-list.spec.js | 18 +++++++++ 4 files changed, 67 insertions(+) diff --git a/document/components/docs/en-US/recycle-list.md b/document/components/docs/en-US/recycle-list.md index f0478662..09594f76 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 f7d93eac..91aa49fe 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 3106cc67..86b00940 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 c772960f..fb004419 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 -- GitLab