From 744a2dfe1dfd055aa87176d24259f31d3467f7d0 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 25 Feb 2021 16:14:39 +0800
Subject: [PATCH 01/30] refactor(Pagination): add layout parse tools
---
.../src/components/Pagination/src/tools/parseLayout.ts | 3 +++
.../components/Pagination/tests/tools/parseLayout.spec.ts | 7 +++++++
2 files changed, 10 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/tools/parseLayout.ts
create mode 100644 packages/element3/src/components/Pagination/tests/tools/parseLayout.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/tools/parseLayout.ts b/packages/element3/src/components/Pagination/src/tools/parseLayout.ts
new file mode 100644
index 000000000..b8f91757f
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/tools/parseLayout.ts
@@ -0,0 +1,3 @@
+export function parseLayout(layout: string): string[] {
+ return layout.split(',').map((v) => v.trim())
+}
diff --git a/packages/element3/src/components/Pagination/tests/tools/parseLayout.spec.ts b/packages/element3/src/components/Pagination/tests/tools/parseLayout.spec.ts
new file mode 100644
index 000000000..029db225f
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/tools/parseLayout.spec.ts
@@ -0,0 +1,7 @@
+import { parseLayout } from '../../src/tools/parseLayout'
+
+describe('parseLayout.ts', () => {
+ it('parse a layout string', () => {
+ expect(parseLayout('prev, pager, next ')).toEqual(['prev', 'pager', 'next'])
+ })
+})
From 23e6451a92918f04cf06a9c10204629b9b1a00f4 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Sun, 28 Feb 2021 17:16:42 +0800
Subject: [PATCH 02/30] refactor(Pagination): core finish
---
.../components/Pagination/src/entity/Pager.ts | 83 ++++++++++++++
.../Pagination/tests/entity/Pager.spec.ts | 106 ++++++++++++++++++
2 files changed, 189 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/entity/Pager.ts
create mode 100644 packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/entity/Pager.ts b/packages/element3/src/components/Pagination/src/entity/Pager.ts
new file mode 100644
index 000000000..42ad1f9bd
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/entity/Pager.ts
@@ -0,0 +1,83 @@
+import { makerArray } from '../tools/makerArray'
+
+export class Pager {
+ private _current: number
+ private _count: number
+ private _viewCount: number
+
+ get pagerCountNotSelf(): number {
+ return this._viewCount - 1
+ }
+
+ get halfPager(): number {
+ return Math.floor(this.pagerCountNotSelf / 2)
+ }
+
+ get leftCount(): number {
+ return Math.max(this._current - 1, 0)
+ }
+
+ get rightCount(): number {
+ return Math.max(this._count - this._current, 0)
+ }
+
+ get current(): number {
+ return this._current
+ }
+
+ set current(v: number) {
+ if (v > this.count || v < 1) {
+ console.warn(`[Pager Warn] class Pager.current not over value ${v}.`)
+ }
+ v = Math.min(this.count, v)
+ v = Math.max(1, v)
+ this._current = v
+ }
+
+ get count(): number {
+ return this._count
+ }
+
+ get viewCount(): number {
+ return this._viewCount
+ }
+
+ get pages(): number[] {
+ return makerArray(1, this._count)
+ }
+
+ get viewPages(): number[] {
+ let leftViewCount = Math.min(this.leftCount, this.halfPager)
+ let rightViewCount = Math.min(this.rightCount, this.halfPager)
+
+ if (leftViewCount > rightViewCount) {
+ leftViewCount =
+ this.pagerCountNotSelf - Math.min(rightViewCount, this.halfPager)
+ } else {
+ rightViewCount =
+ this.pagerCountNotSelf - Math.min(leftViewCount, this.halfPager)
+ }
+
+ return this.catOut(
+ this._current - leftViewCount,
+ this._current + rightViewCount
+ )
+ }
+
+ get midViewPages(): number[] {
+ const result = this.viewPages
+ result.shift()
+ result.pop()
+ return result
+ }
+
+ constructor(pageCount: number, currentPage: number, pagerCount: number) {
+ this._count = pageCount
+ this._current = currentPage
+ this._viewCount = pagerCount
+ }
+
+ catOut(start: number, end: number): number[] {
+ return this.pages.slice(Math.max(start - 1, 0), Math.min(end, this._count))
+ }
+}
diff --git a/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
new file mode 100644
index 000000000..1c0292821
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
@@ -0,0 +1,106 @@
+import { Pager } from '../../src/entity/Pager'
+
+describe('Pager.ts', () => {
+ it('instance Pager', () => {
+ const pageCount = 6
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager(pageCount, currentPage, pagerCount)
+
+ expect(pager.pages).toHaveLength(6)
+ })
+
+ it('view pages', () => {
+ const pageCount = 10
+ const currentPage = 1
+ const pagerCount = 7
+ const pager = new Pager(pageCount, currentPage, pagerCount)
+
+ expect(pager.midViewPages).toEqual([2, 3, 4, 5, 6])
+
+ pager.current = 2
+ expect(pager.midViewPages).toEqual([2, 3, 4, 5, 6])
+
+ pager.current = 3
+ expect(pager.midViewPages).toEqual([2, 3, 4, 5, 6])
+
+ pager.current = 5
+ expect(pager.midViewPages).toEqual([3, 4, 5, 6, 7])
+
+ pager.current = 8
+ expect(pager.midViewPages).toEqual([5, 6, 7, 8, 9])
+
+ pager.current = 9
+ expect(pager.midViewPages).toEqual([5, 6, 7, 8, 9])
+
+ pager.current = 10
+ expect(pager.midViewPages).toEqual([5, 6, 7, 8, 9])
+
+ expect(pager.leftCount).toBe(9)
+ expect(pager.rightCount).toBe(0)
+ })
+
+ it('test catOut method', () => {
+ const pageCount = 10
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager(pageCount, currentPage, pagerCount)
+
+ expect(pager.catOut(1, 2)).toEqual([1, 2])
+ expect(pager.catOut(4, 7)).toEqual([4, 5, 6, 7])
+ expect(pager.catOut(0, 11)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
+ })
+
+ it('test pageCount = pagerCount', () => {
+ const pageCount = 10
+ const currentPage = 2
+ const pagerCount = 10
+ const pager = new Pager(pageCount, currentPage, pagerCount)
+
+ expect(pager.viewPages).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
+ expect(pager.leftCount).toBe(1)
+ expect(pager.rightCount).toBe(8)
+ })
+
+ it('test viewPages', () => {
+ const pageCount = 10
+ const currentPage = 1
+ const pagerCount = 5
+ const pager = new Pager(pageCount, currentPage, pagerCount)
+
+ expect(pager.viewPages).toEqual([1, 2, 3, 4, 5])
+
+ pager.current = 2
+ expect(pager.viewPages).toEqual([1, 2, 3, 4, 5])
+
+ pager.current = 3
+ expect(pager.viewPages).toEqual([1, 2, 3, 4, 5])
+
+ pager.current = 4
+ expect(pager.viewPages).toEqual([2, 3, 4, 5, 6])
+
+ pager.current = 5
+ expect(pager.viewPages).toEqual([3, 4, 5, 6, 7])
+
+ pager.current = 6
+ expect(pager.viewPages).toEqual([4, 5, 6, 7, 8])
+
+ pager.current = 7
+ expect(pager.viewPages).toEqual([5, 6, 7, 8, 9])
+
+ pager.current = 8
+ expect(pager.viewPages).toEqual([6, 7, 8, 9, 10])
+
+ pager.current = 9
+ expect(pager.viewPages).toEqual([6, 7, 8, 9, 10])
+
+ pager.current = 10
+ expect(pager.viewPages).toEqual([6, 7, 8, 9, 10])
+
+ pager.current = 0
+ expect(pager.viewPages).toEqual([1, 2, 3, 4, 5])
+
+ pager.current = 11
+ expect(pager.viewPages).toEqual([6, 7, 8, 9, 10])
+ })
+})
From 661a4502dcd4f09656565e5135586acfa4e07c35 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Sun, 28 Feb 2021 17:17:58 +0800
Subject: [PATCH 03/30] refactor(Pagination): layout Pager component show page
number ok
---
.../components/Pagination/src/parts/Pager.vue | 47 +++++++++++
.../Pagination/tests/parts/Pager.spec.ts | 82 +++++++++++++++++++
2 files changed, 129 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/parts/Pager.vue
create mode 100644 packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/parts/Pager.vue b/packages/element3/src/components/Pagination/src/parts/Pager.vue
new file mode 100644
index 000000000..cf56c8300
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/parts/Pager.vue
@@ -0,0 +1,47 @@
+
+
+
+
+
diff --git a/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
new file mode 100644
index 000000000..70aa1b227
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
@@ -0,0 +1,82 @@
+import { mount } from '@vue/test-utils'
+import { Pager } from '../../src/entity/Pager'
+import ElPager from '../../src/parts/Pager.vue'
+
+describe('Pager.vue', () => {
+ it('show page pageCount <= pagerCount', () => {
+ const pager = new Pager(5, 3, 7)
+ const wrapper = mount(ElPager, {
+ props: {
+ pager
+ }
+ })
+
+ expect(wrapper.findAll('.number')).toHaveLength(5)
+ expect(wrapper.findAll('.number').map((item) => item.text())).toEqual([
+ '1',
+ '2',
+ '3',
+ '4',
+ '5'
+ ])
+ expect(wrapper.find('.number.active').text()).toBe('3')
+ expect(wrapper.findAll('.el-icon.more')).toHaveLength(0)
+ })
+ it('show page pageCount > pagerCount currentPage is 4', () => {
+ const pager = new Pager(30, 4, 7)
+
+ const wrapper = mount(ElPager, {
+ props: {
+ pager
+ }
+ })
+
+ expect(wrapper.findAll('.number')).toHaveLength(7)
+ expect(wrapper.find('.el-icon.more.btn-quickprev').exists()).toBeFalsy()
+ expect(wrapper.find('.el-icon.more.btn-quicknext').exists()).toBeTruthy()
+ })
+ it('show page pageCount > pagerCount currentPage is 27', () => {
+ const pager = new Pager(30, 27, 7)
+
+ const wrapper = mount(ElPager, {
+ props: {
+ pager
+ }
+ })
+
+ expect(wrapper.findAll('.number')).toHaveLength(7)
+ expect(wrapper.find('.el-icon.more.btn-quickprev').exists()).toBeTruthy()
+ expect(wrapper.find('.el-icon.more.btn-quicknext').exists()).toBeFalsy()
+ })
+ it('show page pageCount > pagerCount', () => {
+ const pager = new Pager(30, 10, 7)
+
+ const wrapper = mount(ElPager, {
+ props: {
+ pager
+ }
+ })
+
+ expect(wrapper.findAll('.number')).toHaveLength(7)
+ expect(wrapper.findAll('.el-icon.more')).toHaveLength(2)
+ expect(wrapper.find('.number.active').text()).toBe('10')
+ })
+
+ it('when pageCount less', () => {
+ const currentPage = 1
+ const pageCount = 2
+ const pagerCount = 7
+ const wrapper = mount(ElPager, {
+ props: {
+ pager: new Pager(pageCount, currentPage, pagerCount)
+ }
+ })
+
+ expect(wrapper.findAll('.number')).toHaveLength(2)
+ expect(wrapper.findAll('.number').map((item) => item.text())).toEqual([
+ '1',
+ '2'
+ ])
+ expect(wrapper.find('.number.active').text()).toBe('1')
+ })
+})
From 085adbbb45918234e5d5be02d496cbe3760d6378 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Sun, 28 Feb 2021 17:19:08 +0800
Subject: [PATCH 04/30] refactor(Pagination): make array tools
---
.../src/components/Pagination/src/tools/makerArray.ts | 5 +++++
.../components/Pagination/tests/tools/makerArray.spec.ts | 7 +++++++
2 files changed, 12 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/tools/makerArray.ts
create mode 100644 packages/element3/src/components/Pagination/tests/tools/makerArray.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/tools/makerArray.ts b/packages/element3/src/components/Pagination/src/tools/makerArray.ts
new file mode 100644
index 000000000..68ff86429
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/tools/makerArray.ts
@@ -0,0 +1,5 @@
+export function makerArray(start: number, end: number): number[] {
+ const arr = []
+ for (let i = start; i <= end; i++) arr.push(i)
+ return arr
+}
diff --git a/packages/element3/src/components/Pagination/tests/tools/makerArray.spec.ts b/packages/element3/src/components/Pagination/tests/tools/makerArray.spec.ts
new file mode 100644
index 000000000..053e1cd7a
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/tools/makerArray.spec.ts
@@ -0,0 +1,7 @@
+import { makerArray } from '../../src/tools/makerArray'
+
+describe('makerArray.ts', () => {
+ it('test create 2-6 array', () => {
+ expect(makerArray(2, 6)).toEqual([2, 3, 4, 5, 6])
+ })
+})
From 2c60d10688a8017222bfc38ad64197142537eacf Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Sun, 28 Feb 2021 18:53:28 +0800
Subject: [PATCH 05/30] refactor(Pagination): add hover class
---
.../components/Pagination/src/parts/Pager.vue | 40 ++++++++++++++++++-
1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/packages/element3/src/components/Pagination/src/parts/Pager.vue b/packages/element3/src/components/Pagination/src/parts/Pager.vue
index cf56c8300..a2ae68da8 100644
--- a/packages/element3/src/components/Pagination/src/parts/Pager.vue
+++ b/packages/element3/src/components/Pagination/src/parts/Pager.vue
@@ -9,7 +9,19 @@
{
+ if (flag) {
+ el.classList.add(a)
+ el.classList.remove(b)
+ } else {
+ el.classList.remove(a)
+ el.classList.add(b)
+ }
+ }
+ return { switchClass }
}
})
From d214ccb9535baa2a366071d00c374db66e0bb391 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Sun, 28 Feb 2021 18:54:13 +0800
Subject: [PATCH 06/30] refactor(Pagination): add prev/next btn
---
.../element3/src/components/Pagination/src/parts/Next.vue | 5 +++++
.../element3/src/components/Pagination/src/parts/Prev.vue | 5 +++++
2 files changed, 10 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/parts/Next.vue
create mode 100644 packages/element3/src/components/Pagination/src/parts/Prev.vue
diff --git a/packages/element3/src/components/Pagination/src/parts/Next.vue b/packages/element3/src/components/Pagination/src/parts/Next.vue
new file mode 100644
index 000000000..34252089d
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/parts/Next.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/packages/element3/src/components/Pagination/src/parts/Prev.vue b/packages/element3/src/components/Pagination/src/parts/Prev.vue
new file mode 100644
index 000000000..7959fa812
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/parts/Prev.vue
@@ -0,0 +1,5 @@
+
+
+
From bc025ca33e064f0c07169693612f76c0ab77df20 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Mon, 1 Mar 2021 17:16:20 +0800
Subject: [PATCH 07/30] refactor(Pagination): Modify the Pager incoming
parameters
---
.../components/Pagination/src/entity/Pager.ts | 31 ++++++++----
.../Pagination/tests/entity/Pager.spec.ts | 49 +++++++++++++++++--
.../Pagination/tests/parts/Pager.spec.ts | 15 +++---
3 files changed, 73 insertions(+), 22 deletions(-)
diff --git a/packages/element3/src/components/Pagination/src/entity/Pager.ts b/packages/element3/src/components/Pagination/src/entity/Pager.ts
index 42ad1f9bd..630141f25 100644
--- a/packages/element3/src/components/Pagination/src/entity/Pager.ts
+++ b/packages/element3/src/components/Pagination/src/entity/Pager.ts
@@ -1,10 +1,22 @@
import { makerArray } from '../tools/makerArray'
+export interface PagerParam {
+ size: number
+ total: number
+ current: number
+ viewCount: number
+}
+
export class Pager {
private _current: number
- private _count: number
+ private _total: number
+ private _size: number
private _viewCount: number
+ get total(): number {
+ return this._total
+ }
+
get pagerCountNotSelf(): number {
return this._viewCount - 1
}
@@ -18,7 +30,7 @@ export class Pager {
}
get rightCount(): number {
- return Math.max(this._count - this._current, 0)
+ return Math.max(this.count - this._current, 0)
}
get current(): number {
@@ -35,7 +47,7 @@ export class Pager {
}
get count(): number {
- return this._count
+ return Math.ceil(this._total / this._size)
}
get viewCount(): number {
@@ -43,7 +55,7 @@ export class Pager {
}
get pages(): number[] {
- return makerArray(1, this._count)
+ return makerArray(1, this.count)
}
get viewPages(): number[] {
@@ -71,13 +83,14 @@ export class Pager {
return result
}
- constructor(pageCount: number, currentPage: number, pagerCount: number) {
- this._count = pageCount
- this._current = currentPage
- this._viewCount = pagerCount
+ constructor(pagerParam: PagerParam) {
+ this._size = pagerParam.size
+ this._total = pagerParam.total
+ this._current = pagerParam.current
+ this._viewCount = pagerParam.viewCount
}
catOut(start: number, end: number): number[] {
- return this.pages.slice(Math.max(start - 1, 0), Math.min(end, this._count))
+ return this.pages.slice(Math.max(start - 1, 0), Math.min(end, this.count))
}
}
diff --git a/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
index 1c0292821..18ae58df4 100644
--- a/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
@@ -5,7 +5,12 @@ describe('Pager.ts', () => {
const pageCount = 6
const currentPage = 2
const pagerCount = 7
- const pager = new Pager(pageCount, currentPage, pagerCount)
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
expect(pager.pages).toHaveLength(6)
})
@@ -14,7 +19,12 @@ describe('Pager.ts', () => {
const pageCount = 10
const currentPage = 1
const pagerCount = 7
- const pager = new Pager(pageCount, currentPage, pagerCount)
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
expect(pager.midViewPages).toEqual([2, 3, 4, 5, 6])
@@ -44,7 +54,12 @@ describe('Pager.ts', () => {
const pageCount = 10
const currentPage = 2
const pagerCount = 7
- const pager = new Pager(pageCount, currentPage, pagerCount)
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
expect(pager.catOut(1, 2)).toEqual([1, 2])
expect(pager.catOut(4, 7)).toEqual([4, 5, 6, 7])
@@ -55,7 +70,12 @@ describe('Pager.ts', () => {
const pageCount = 10
const currentPage = 2
const pagerCount = 10
- const pager = new Pager(pageCount, currentPage, pagerCount)
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
expect(pager.viewPages).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(pager.leftCount).toBe(1)
@@ -66,7 +86,12 @@ describe('Pager.ts', () => {
const pageCount = 10
const currentPage = 1
const pagerCount = 5
- const pager = new Pager(pageCount, currentPage, pagerCount)
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
expect(pager.viewPages).toEqual([1, 2, 3, 4, 5])
@@ -103,4 +128,18 @@ describe('Pager.ts', () => {
pager.current = 11
expect(pager.viewPages).toEqual([6, 7, 8, 9, 10])
})
+ it('test total', () => {
+ const total = 100
+ const currentPage = 2
+ const pagerCount = 7
+ const pageSize = 10
+ const pager = new Pager({
+ total: total,
+ size: pageSize,
+ current: currentPage,
+ viewCount: pagerCount
+ })
+
+ expect(pager.count).toBe(10)
+ })
})
diff --git a/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
index 70aa1b227..8288f5b45 100644
--- a/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
@@ -4,7 +4,7 @@ import ElPager from '../../src/parts/Pager.vue'
describe('Pager.vue', () => {
it('show page pageCount <= pagerCount', () => {
- const pager = new Pager(5, 3, 7)
+ const pager = new Pager({ total: 5, size: 1, current: 3, viewCount: 7 })
const wrapper = mount(ElPager, {
props: {
pager
@@ -23,7 +23,7 @@ describe('Pager.vue', () => {
expect(wrapper.findAll('.el-icon.more')).toHaveLength(0)
})
it('show page pageCount > pagerCount currentPage is 4', () => {
- const pager = new Pager(30, 4, 7)
+ const pager = new Pager({ total: 30, size: 1, current: 4, viewCount: 7 })
const wrapper = mount(ElPager, {
props: {
@@ -36,7 +36,7 @@ describe('Pager.vue', () => {
expect(wrapper.find('.el-icon.more.btn-quicknext').exists()).toBeTruthy()
})
it('show page pageCount > pagerCount currentPage is 27', () => {
- const pager = new Pager(30, 27, 7)
+ const pager = new Pager({ total: 30, size: 1, current: 27, viewCount: 7 })
const wrapper = mount(ElPager, {
props: {
@@ -49,7 +49,7 @@ describe('Pager.vue', () => {
expect(wrapper.find('.el-icon.more.btn-quicknext').exists()).toBeFalsy()
})
it('show page pageCount > pagerCount', () => {
- const pager = new Pager(30, 10, 7)
+ const pager = new Pager({ total: 30, size: 1, current: 10, viewCount: 7 })
const wrapper = mount(ElPager, {
props: {
@@ -63,12 +63,11 @@ describe('Pager.vue', () => {
})
it('when pageCount less', () => {
- const currentPage = 1
- const pageCount = 2
- const pagerCount = 7
+ const pager = new Pager({ total: 2, size: 1, current: 1, viewCount: 7 })
+
const wrapper = mount(ElPager, {
props: {
- pager: new Pager(pageCount, currentPage, pagerCount)
+ pager
}
})
From 263184fd18efbcb8ed6f8fa3192a4d7465d950b2 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Mon, 1 Mar 2021 17:21:27 +0800
Subject: [PATCH 08/30] refactor(Pagination): add Total Part
---
.../components/Pagination/src/parts/Total.vue | 12 +++++++++++
.../Pagination/tests/parts/Total.spec.ts | 20 +++++++++++++++++++
2 files changed, 32 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/parts/Total.vue
create mode 100644 packages/element3/src/components/Pagination/tests/parts/Total.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/parts/Total.vue b/packages/element3/src/components/Pagination/src/parts/Total.vue
new file mode 100644
index 000000000..e47efb875
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/parts/Total.vue
@@ -0,0 +1,12 @@
+
+
+
+
+
diff --git a/packages/element3/src/components/Pagination/tests/parts/Total.spec.ts b/packages/element3/src/components/Pagination/tests/parts/Total.spec.ts
new file mode 100644
index 000000000..1e28db18e
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/parts/Total.spec.ts
@@ -0,0 +1,20 @@
+import { mount } from '@vue/test-utils'
+import { Pager } from '../../src/entity/Pager'
+import Total from '../../src/parts/Total.vue'
+
+describe('Total.vue', () => {
+ it('show total', () => {
+ const wrapper = mount(Total, {
+ props: {
+ pager: new Pager({
+ total: 10,
+ size: 1,
+ current: 2,
+ viewCount: 7
+ })
+ }
+ })
+
+ expect(wrapper.text()).toBe(`共 ${10} 条`)
+ })
+})
From 833937c5b466f1717d397696c0e7c946ab863f7f Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Mon, 1 Mar 2021 17:33:22 +0800
Subject: [PATCH 09/30] refactor(Pagination): Complete page number
display(hug-sun#567)
---
.../components/Pagination/src/Pagination.vue | 87 +++++++++++++++++++
.../Pagination/tests/Pagination.spec.ts | 68 +++++++++++++++
2 files changed, 155 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/src/Pagination.vue
create mode 100644 packages/element3/src/components/Pagination/tests/Pagination.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
new file mode 100644
index 000000000..d5e9ec19e
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -0,0 +1,87 @@
+
+
+
+
+
+
+
diff --git a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
new file mode 100644
index 000000000..b7537ed86
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
@@ -0,0 +1,68 @@
+import { mount } from '@vue/test-utils'
+import Pagination from '../src/Pagination.vue'
+import Prev from '../src/parts/Prev.vue'
+import Next from '../src/parts/Next.vue'
+import Pager from '../src/parts/Pager.vue'
+import Total from '../src/parts/Total.vue'
+
+describe('Pagination.vue', () => {
+ it('Realize layout prev,pager,next', () => {
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'prev, pager, next, total',
+ currentPage: 2,
+ pageCount: 5
+ }
+ })
+
+ expect(wrapper.findComponent(Prev).exists()).toBeTruthy()
+ expect(wrapper.findComponent(Next).exists()).toBeTruthy()
+ expect(wrapper.findComponent(Pager).exists()).toBeTruthy()
+ expect(wrapper.findComponent(Total).exists()).toBeTruthy()
+ })
+ it('Realize simple paging display', () => {
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'prev, pager, next',
+ currentPage: 2,
+ pageCount: 5
+ }
+ })
+
+ const pager = wrapper.vm.pager
+
+ expect(wrapper.findComponent(Pager).vm.pager).toBe(pager)
+ expect(pager.current).toBe(2)
+ expect(pager.count).toBe(5)
+ })
+ it('Calculate page numbers by Total and pageSize', () => {
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'prev, pager, next, total',
+ currentPage: 2,
+ pageSize: 10,
+ total: 100
+ }
+ })
+
+ const pager = wrapper.vm.pager
+
+ expect(wrapper.findComponent(Total).vm.pager).toBe(pager)
+ expect(pager.current).toBe(2)
+ expect(pager.count).toBe(10)
+ })
+
+ it('Hide when there is only one page', () => {
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'pager',
+ currentPage: 1,
+ pageSize: 1,
+ total: 1,
+ hideOnSinglePage: true
+ }
+ })
+
+ expect(wrapper.findComponent(Pager).exists()).toBeFalsy()
+ })
+})
From 961f1aeac244a6eeb0e3088d5a0ae468dbfad6f0 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Mon, 1 Mar 2021 17:53:16 +0800
Subject: [PATCH 10/30] refactor(Pagination): To optimize the Code
---
.../components/Pagination/src/Pagination.vue | 52 +++++++++++++------
1 file changed, 37 insertions(+), 15 deletions(-)
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
index d5e9ec19e..2be08f411 100644
--- a/packages/element3/src/components/Pagination/src/Pagination.vue
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -13,7 +13,8 @@ import {
reactive,
toRefs,
watch,
- watchEffect
+ watchEffect,
+ Ref
} from 'vue'
import { ElPaginationProps } from '../types'
import { Pager as PagerCore } from './entity/Pager'
@@ -63,18 +64,22 @@ export default defineComponent({
Total
},
setup(props: ElPaginationProps) {
- const { currentPage, total, pageSize, pagerCount, pageCount } = toRefs(
- props
- )
- const layoutPart = computed(() => parseLayout(props.layout))
- const pager = reactive(
- new PagerCore({
- total: total?.value ?? pageCount.value,
- size: pageSize.value,
- viewCount: pagerCount.value,
- current: currentPage.value
- })
- )
+ const {
+ currentPage,
+ total,
+ pageSize,
+ pagerCount,
+ pageCount,
+ layout
+ } = toRefs(props)
+ const { layoutPart } = useLayout(layout)
+ const { pager } = usePager({
+ currentPage,
+ total,
+ pageSize,
+ pagerCount,
+ pageCount
+ })
return {
layoutPart,
@@ -82,6 +87,23 @@ export default defineComponent({
}
}
})
-
-
+function usePager({ total, pageCount, pageSize, pagerCount, currentPage }) {
+ const pager = reactive(
+ new PagerCore({
+ total: total?.value ?? pageCount.value,
+ size: pageSize.value,
+ viewCount: pagerCount.value,
+ current: currentPage.value
+ })
+ )
+ return { pager }
+}
+
+function useLayout(layout: Ref) {
+ const layoutPart = computed(() => parseLayout(layout.value))
+ return {
+ layoutPart
+ }
+}
+
From 31b6fc1d85eb4e6e5f19a5a7a7d418cd554e533b Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Mon, 1 Mar 2021 18:11:01 +0800
Subject: [PATCH 11/30] refactor(Pagination): test coverage 100%
---
.../Pagination/tests/parts/Pager.spec.ts | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
index 8288f5b45..ca4fda822 100644
--- a/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/parts/Pager.spec.ts
@@ -78,4 +78,29 @@ describe('Pager.vue', () => {
])
expect(wrapper.find('.number.active').text()).toBe('1')
})
+
+ it('more btn mouse enter and leave', async () => {
+ const pager = new Pager({ total: 100, size: 1, current: 50, viewCount: 7 })
+
+ const wrapper = mount(ElPager, {
+ props: {
+ pager
+ }
+ })
+
+ const quickPrev = wrapper.find('.btn-quickprev')
+ const quickNext = wrapper.find('.btn-quicknext')
+
+ await quickPrev.trigger('mouseenter')
+ await quickNext.trigger('mouseenter')
+
+ expect(quickPrev.classes()).toContain('el-icon-d-arrow-left')
+ expect(quickNext.classes()).toContain('el-icon-d-arrow-right')
+
+ await quickPrev.trigger('mouseleave')
+ await quickNext.trigger('mouseleave')
+
+ expect(quickPrev.classes()).toContain('el-icon-more')
+ expect(quickNext.classes()).toContain('el-icon-more')
+ })
})
From 4171fab4a0e4d4d09c4667916029250471dcb4e8 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 4 Mar 2021 13:54:03 +0800
Subject: [PATCH 12/30] refactor(Pagination): add props type
---
.../element3/src/components/Pagination/types.ts | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 packages/element3/src/components/Pagination/types.ts
diff --git a/packages/element3/src/components/Pagination/types.ts b/packages/element3/src/components/Pagination/types.ts
new file mode 100644
index 000000000..39e585235
--- /dev/null
+++ b/packages/element3/src/components/Pagination/types.ts
@@ -0,0 +1,15 @@
+export interface ElPaginationProps {
+ layout?: string
+ pagerCount?: number
+ currentPage?: number
+ pageCount?: number
+ total?: number
+ pageSize?: number
+ hideOnSinglePage?: boolean
+}
+
+declare class ElPagination {
+ $props: ElPaginationProps
+}
+
+export default ElPagination
From 2d649eebb7ba9031d3eed61b36604c78c70d17b6 Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 4 Mar 2021 17:26:06 +0800
Subject: [PATCH 13/30] refactor(Pagination): Complete the previous page to the
next page switch
---
.../components/Pagination/src/Pagination.vue | 37 +++++++-
.../components/Pagination/src/entity/Pager.ts | 61 +++++++++++--
.../components/Pagination/src/parts/Next.vue | 13 ++-
.../components/Pagination/src/parts/Pager.vue | 42 ++-------
.../components/Pagination/src/parts/Prev.vue | 13 ++-
.../Pagination/src/tools/element.ts | 12 +++
.../Pagination/tests/Pagination.spec.ts | 34 ++++++++
.../Pagination/tests/entity/Pager.spec.ts | 86 ++++++++++++++++++-
.../Pagination/tests/tools/element.spec.ts | 14 +++
9 files changed, 265 insertions(+), 47 deletions(-)
create mode 100644 packages/element3/src/components/Pagination/src/tools/element.ts
create mode 100644 packages/element3/src/components/Pagination/tests/tools/element.spec.ts
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
index 2be08f411..a5b3b6440 100644
--- a/packages/element3/src/components/Pagination/src/Pagination.vue
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -14,10 +14,11 @@ import {
toRefs,
watch,
watchEffect,
- Ref
+ Ref,
+ getCurrentInstance
} from 'vue'
import { ElPaginationProps } from '../types'
-import { Pager as PagerCore } from './entity/Pager'
+import { Pager as PagerCore, PagerEventType } from './entity/Pager'
import { parseLayout } from './tools/parseLayout'
import Pager from './parts/Pager.vue'
@@ -42,8 +43,7 @@ export default defineComponent({
default: 1
},
pageCount: {
- type: Number,
- default: 0
+ type: Number
},
total: {
type: Number
@@ -57,6 +57,7 @@ export default defineComponent({
default: false
}
},
+ emits: ['update:currentPage', 'prev-click', 'next-click'],
components: {
Pager,
Prev,
@@ -89,6 +90,7 @@ export default defineComponent({
})
function usePager({ total, pageCount, pageSize, pagerCount, currentPage }) {
+ const { emit } = getCurrentInstance()
const pager = reactive(
new PagerCore({
total: total?.value ?? pageCount.value,
@@ -97,6 +99,33 @@ function usePager({ total, pageCount, pageSize, pagerCount, currentPage }) {
current: currentPage.value
})
)
+ pager.on(PagerEventType.CHANGE, (v) => {
+ emit('update:currentPage', v)
+ })
+ pager.on(PagerEventType.PREV, (v) => {
+ emit('prev-click', v)
+ })
+ pager.on(PagerEventType.NEXT, (v) => {
+ emit('next-click', v)
+ })
+ if (total)
+ watch(total, (v: number) => {
+ pager.total = v
+ })
+ if (pageCount)
+ watch(pageCount, (v: number) => {
+ pager.total = v
+ pager.changeSize(1)
+ })
+ watch(pagerCount, (v: number) => {
+ pager.viewCount = v
+ })
+ watch(pageSize, (v: number) => {
+ pager.size = v
+ })
+ watch(currentPage, (v: number) => {
+ pager.current = v
+ })
return { pager }
}
diff --git a/packages/element3/src/components/Pagination/src/entity/Pager.ts b/packages/element3/src/components/Pagination/src/entity/Pager.ts
index 630141f25..2a192cc1e 100644
--- a/packages/element3/src/components/Pagination/src/entity/Pager.ts
+++ b/packages/element3/src/components/Pagination/src/entity/Pager.ts
@@ -1,5 +1,14 @@
+import mitt from 'mitt'
import { makerArray } from '../tools/makerArray'
+export enum PagerEventType {
+ CHANGE = 'change',
+ PREV = 'prev',
+ NEXT = 'next'
+}
+
+export type PagerEventCb = (pageNumber: number) => void
+
export interface PagerParam {
size: number
total: number
@@ -12,11 +21,24 @@ export class Pager {
private _total: number
private _size: number
private _viewCount: number
+ private _event = mitt()
+
+ get size(): number {
+ return this._size
+ }
+
+ set size(v: number) {
+ this.changeSize(v)
+ }
get total(): number {
return this._total
}
+ set total(v: number) {
+ this._total = v
+ }
+
get pagerCountNotSelf(): number {
return this._viewCount - 1
}
@@ -38,12 +60,7 @@ export class Pager {
}
set current(v: number) {
- if (v > this.count || v < 1) {
- console.warn(`[Pager Warn] class Pager.current not over value ${v}.`)
- }
- v = Math.min(this.count, v)
- v = Math.max(1, v)
- this._current = v
+ this.jump(v)
}
get count(): number {
@@ -54,6 +71,10 @@ export class Pager {
return this._viewCount
}
+ set viewCount(v: number) {
+ this._viewCount = v
+ }
+
get pages(): number[] {
return makerArray(1, this.count)
}
@@ -93,4 +114,32 @@ export class Pager {
catOut(start: number, end: number): number[] {
return this.pages.slice(Math.max(start - 1, 0), Math.min(end, this.count))
}
+
+ jump(v: number): void {
+ if (v > this.count || v < 1) {
+ console.warn(`[Pager Warn] class Pager.current not over value ${v}.`)
+ }
+ v = Math.min(this.count, v)
+ v = Math.max(1, v)
+ this._current = v
+ this._event.emit(PagerEventType.CHANGE, v)
+ }
+
+ prev(): void {
+ this.jump(this._current - 1)
+ this._event.emit(PagerEventType.PREV, this._current)
+ }
+
+ next(): void {
+ this.jump(this._current + 1)
+ this._event.emit(PagerEventType.NEXT, this._current)
+ }
+
+ on(type: PagerEventType, cb: PagerEventCb): void {
+ this._event.on(type, cb)
+ }
+
+ changeSize(v: number): void {
+ this._size = v
+ }
}
diff --git a/packages/element3/src/components/Pagination/src/parts/Next.vue b/packages/element3/src/components/Pagination/src/parts/Next.vue
index 34252089d..558a6c4e9 100644
--- a/packages/element3/src/components/Pagination/src/parts/Next.vue
+++ b/packages/element3/src/components/Pagination/src/parts/Next.vue
@@ -1,5 +1,16 @@
-
+
+
diff --git a/packages/element3/src/components/Pagination/src/parts/Pager.vue b/packages/element3/src/components/Pagination/src/parts/Pager.vue
index a2ae68da8..af372c9bb 100644
--- a/packages/element3/src/components/Pagination/src/parts/Pager.vue
+++ b/packages/element3/src/components/Pagination/src/parts/Pager.vue
@@ -10,18 +10,8 @@
import { Pager } from '../entity/Pager'
+import { switchClass } from '../tools/element'
import { defineComponent } from 'vue'
export default defineComponent({
@@ -68,16 +49,9 @@ export default defineComponent({
pager: Pager
},
setup() {
- const switchClass = (el, flag, [a, b]) => {
- if (flag) {
- el.classList.add(a)
- el.classList.remove(b)
- } else {
- el.classList.remove(a)
- el.classList.add(b)
- }
- }
- return { switchClass }
+ const switchLeft = switchClass(['el-icon-d-arrow-left', 'el-icon-more'])
+ const switchRight = switchClass(['el-icon-d-arrow-right', 'el-icon-more'])
+ return { switchLeft, switchRight }
}
})
diff --git a/packages/element3/src/components/Pagination/src/parts/Prev.vue b/packages/element3/src/components/Pagination/src/parts/Prev.vue
index 7959fa812..21f29b5fe 100644
--- a/packages/element3/src/components/Pagination/src/parts/Prev.vue
+++ b/packages/element3/src/components/Pagination/src/parts/Prev.vue
@@ -1,5 +1,16 @@
-
+
+
+
diff --git a/packages/element3/src/components/Pagination/src/tools/element.ts b/packages/element3/src/components/Pagination/src/tools/element.ts
new file mode 100644
index 000000000..c1b2c08d4
--- /dev/null
+++ b/packages/element3/src/components/Pagination/src/tools/element.ts
@@ -0,0 +1,12 @@
+type SwitchFun = (el: Element | EventTarget) => void
+export function switchClass([a, b]: [string, string]): SwitchFun {
+ return (el: Element) => {
+ if (!el.className.includes(a)) {
+ el.classList.add(a)
+ el.classList.remove(b)
+ } else {
+ el.classList.remove(a)
+ el.classList.add(b)
+ }
+ }
+}
diff --git a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
index b7537ed86..04b02034d 100644
--- a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
@@ -4,6 +4,7 @@ import Prev from '../src/parts/Prev.vue'
import Next from '../src/parts/Next.vue'
import Pager from '../src/parts/Pager.vue'
import Total from '../src/parts/Total.vue'
+import { nextTick, ref } from 'vue'
describe('Pagination.vue', () => {
it('Realize layout prev,pager,next', () => {
@@ -65,4 +66,37 @@ describe('Pagination.vue', () => {
expect(wrapper.findComponent(Pager).exists()).toBeFalsy()
})
+
+ it('click prev/next button currentPage', async () => {
+ const currentPage = ref(2)
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'prev, next',
+ currentPage: (currentPage as unknown) as number,
+ pageCount: 10,
+ 'onUpdate:currentPage': (v) => (currentPage.value = v)
+ }
+ })
+
+ await wrapper.findComponent(Prev).trigger('click')
+ expect(currentPage.value).toBe(1)
+ await wrapper.findComponent(Next).trigger('click')
+ expect(currentPage.value).toBe(2)
+ })
+
+ it('vModel currentPage', async () => {
+ const currentPage = ref(2)
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'pager',
+ currentPage: (currentPage as unknown) as number,
+ pageCount: 10,
+ 'onUpdate:currentPage': (v) => (currentPage.value = v)
+ }
+ })
+
+ currentPage.value = 3
+ await nextTick()
+ expect(wrapper.find('.active').text()).toBe('3')
+ })
})
diff --git a/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
index 18ae58df4..0b3737ba9 100644
--- a/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/entity/Pager.spec.ts
@@ -1,4 +1,4 @@
-import { Pager } from '../../src/entity/Pager'
+import { Pager, PagerEventType } from '../../src/entity/Pager'
describe('Pager.ts', () => {
it('instance Pager', () => {
@@ -142,4 +142,88 @@ describe('Pager.ts', () => {
expect(pager.count).toBe(10)
})
+
+ it('jump page', () => {
+ const pageCount = 6
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
+
+ pager.jump(5)
+ expect(pager.current).toBe(5)
+ })
+
+ it('jump page when currentPage Crossing the line', () => {
+ const pageCount = 6
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
+
+ pager.jump(7)
+ expect(pager.current).toBe(6)
+ pager.jump(0)
+ expect(pager.current).toBe(1)
+ })
+
+ it('change prev/next page', () => {
+ const pageCount = 6
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
+
+ pager.prev()
+ expect(pager.current).toBe(1)
+ pager.next()
+ expect(pager.current).toBe(2)
+ })
+
+ it('when current change, trigger event', () => {
+ const changeHandler = jest.fn()
+ const prevHandler = jest.fn()
+ const nextHandler = jest.fn()
+
+ const pageCount = 6
+ const currentPage = 2
+ const pagerCount = 7
+ const pager = new Pager({
+ total: pageCount,
+ size: 1,
+ current: currentPage,
+ viewCount: pagerCount
+ })
+
+ pager.on(PagerEventType.CHANGE, changeHandler)
+ pager.on(PagerEventType.PREV, prevHandler)
+ pager.on(PagerEventType.NEXT, nextHandler)
+
+ pager.jump(5)
+ pager.prev()
+ pager.next()
+
+ expect(changeHandler).toBeCalledTimes(3)
+ expect(changeHandler).toHaveBeenNthCalledWith(1, 5)
+ expect(changeHandler).toHaveBeenNthCalledWith(2, 4)
+ expect(changeHandler).toHaveBeenNthCalledWith(3, 5)
+
+ expect(prevHandler).toBeCalledTimes(1)
+ expect(prevHandler).toHaveBeenNthCalledWith(1, 4)
+
+ expect(nextHandler).toBeCalledTimes(1)
+ expect(nextHandler).toHaveBeenNthCalledWith(1, 5)
+ })
})
diff --git a/packages/element3/src/components/Pagination/tests/tools/element.spec.ts b/packages/element3/src/components/Pagination/tests/tools/element.spec.ts
new file mode 100644
index 000000000..61decbdc1
--- /dev/null
+++ b/packages/element3/src/components/Pagination/tests/tools/element.spec.ts
@@ -0,0 +1,14 @@
+import { switchClass } from '../../src/tools/element'
+
+describe('element.ts', () => {
+ it('switch tow class state', () => {
+ const element = document.createElement('div')
+ const switchTrigger = switchClass(['a', 'b'])
+
+ switchTrigger(element)
+ expect(element.className).toBe('a')
+
+ switchTrigger(element)
+ expect(element.className).toBe('b')
+ })
+})
From a9b055b21bb32db047460278dfecaead5fc5e3bb Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 4 Mar 2021 17:44:43 +0800
Subject: [PATCH 14/30] refactor(Pagination): size change
---
.../components/Pagination/src/Pagination.vue | 12 ++++++++++-
.../components/Pagination/src/entity/Pager.ts | 4 +++-
.../Pagination/tests/Pagination.spec.ts | 20 +++++++++++++++++++
3 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
index a5b3b6440..a97e721c6 100644
--- a/packages/element3/src/components/Pagination/src/Pagination.vue
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -57,7 +57,13 @@ export default defineComponent({
default: false
}
},
- emits: ['update:currentPage', 'prev-click', 'next-click'],
+ emits: [
+ 'update:currentPage',
+ 'prev-click',
+ 'next-click',
+ 'size-change',
+ 'update:pageSize'
+ ],
components: {
Pager,
Prev,
@@ -108,6 +114,10 @@ function usePager({ total, pageCount, pageSize, pagerCount, currentPage }) {
pager.on(PagerEventType.NEXT, (v) => {
emit('next-click', v)
})
+ pager.on(PagerEventType.SIZE_CHANGE, (v) => {
+ emit('size-change', v)
+ emit('update:pageSize', v)
+ })
if (total)
watch(total, (v: number) => {
pager.total = v
diff --git a/packages/element3/src/components/Pagination/src/entity/Pager.ts b/packages/element3/src/components/Pagination/src/entity/Pager.ts
index 2a192cc1e..4306d0985 100644
--- a/packages/element3/src/components/Pagination/src/entity/Pager.ts
+++ b/packages/element3/src/components/Pagination/src/entity/Pager.ts
@@ -4,7 +4,8 @@ import { makerArray } from '../tools/makerArray'
export enum PagerEventType {
CHANGE = 'change',
PREV = 'prev',
- NEXT = 'next'
+ NEXT = 'next',
+ SIZE_CHANGE = 'size_change'
}
export type PagerEventCb = (pageNumber: number) => void
@@ -141,5 +142,6 @@ export class Pager {
changeSize(v: number): void {
this._size = v
+ this._event.emit(PagerEventType.SIZE_CHANGE, v)
}
}
diff --git a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
index 04b02034d..07b21023a 100644
--- a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
@@ -99,4 +99,24 @@ describe('Pagination.vue', () => {
await nextTick()
expect(wrapper.find('.active').text()).toBe('3')
})
+
+ it('change size by ref', async () => {
+ const currentPage = ref(2)
+ const total = ref(100)
+ const size = ref(10)
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'pager',
+ currentPage: (currentPage as unknown) as number,
+ total: (total as unknown) as number,
+ pageSize: (size as unknown) as number,
+ 'onUpdate:currentPage': (v) => (currentPage.value = v)
+ }
+ })
+
+ expect(wrapper.vm.pager.count).toBe(10)
+ size.value = 20
+ await nextTick()
+ expect(wrapper.vm.pager.count).toBe(5)
+ })
})
From 34d6103e9fa1a3486c98af0fdb4bb6c9e644abba Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 4 Mar 2021 17:58:55 +0800
Subject: [PATCH 15/30] refactor(Pagination): Implement pager paging
---
.../components/Pagination/src/Pagination.vue | 1 -
.../components/Pagination/src/entity/Pager.ts | 8 ++++----
.../components/Pagination/src/parts/Pager.vue | 5 +++++
.../Pagination/tests/Pagination.spec.ts | 19 +++++++++++++++++++
4 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
index a97e721c6..933aa6a95 100644
--- a/packages/element3/src/components/Pagination/src/Pagination.vue
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -13,7 +13,6 @@ import {
reactive,
toRefs,
watch,
- watchEffect,
Ref,
getCurrentInstance
} from 'vue'
diff --git a/packages/element3/src/components/Pagination/src/entity/Pager.ts b/packages/element3/src/components/Pagination/src/entity/Pager.ts
index 4306d0985..fcdc76ed0 100644
--- a/packages/element3/src/components/Pagination/src/entity/Pager.ts
+++ b/packages/element3/src/components/Pagination/src/entity/Pager.ts
@@ -126,13 +126,13 @@ export class Pager {
this._event.emit(PagerEventType.CHANGE, v)
}
- prev(): void {
- this.jump(this._current - 1)
+ prev(step = 1): void {
+ this.jump(this._current - step)
this._event.emit(PagerEventType.PREV, this._current)
}
- next(): void {
- this.jump(this._current + 1)
+ next(step = 1): void {
+ this.jump(this._current + step)
this._event.emit(PagerEventType.NEXT, this._current)
}
diff --git a/packages/element3/src/components/Pagination/src/parts/Pager.vue b/packages/element3/src/components/Pagination/src/parts/Pager.vue
index af372c9bb..eb15ec683 100644
--- a/packages/element3/src/components/Pagination/src/parts/Pager.vue
+++ b/packages/element3/src/components/Pagination/src/parts/Pager.vue
@@ -4,6 +4,7 @@
v-if="pager.count >= 1"
class="number"
:class="{ active: pager.current === 1 }"
+ @click="pager.jump(1)"
>
1
@@ -12,6 +13,7 @@
class="el-icon more btn-quickprev el-icon-more"
@mouseenter="switchLeft($event.target)"
@mouseleave="switchLeft($event.target)"
+ @click="pager.prev(pager.halfPager)"
>
diff --git a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
index 07b21023a..ca297f978 100644
--- a/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
+++ b/packages/element3/src/components/Pagination/tests/Pagination.spec.ts
@@ -119,4 +119,23 @@ describe('Pagination.vue', () => {
await nextTick()
expect(wrapper.vm.pager.count).toBe(5)
})
+
+ it('click pager button currentPage', async () => {
+ const currentPage = ref(2)
+ const wrapper = mount(Pagination, {
+ props: {
+ layout: 'pager',
+ currentPage: (currentPage as unknown) as number,
+ pageCount: 100,
+ 'onUpdate:currentPage': (v) => (currentPage.value = v)
+ }
+ })
+ const pages = wrapper.findAll('.number')
+
+ await pages[0].trigger('click')
+ expect(currentPage.value).toBe(1)
+
+ await wrapper.find('.btn-quicknext').trigger('click')
+ expect(currentPage.value).toBe(4)
+ })
})
From fdc830b830e901262fe559d4346968f7edbcdbaa Mon Sep 17 00:00:00 2001
From: WumaCoder <1829913225@qq.com>
Date: Thu, 4 Mar 2021 18:11:55 +0800
Subject: [PATCH 16/30] refactor(Pagination): change hideOnSinglePage code
---
packages/element3/src/components/Pagination/index.js | 7 +++++++
.../element3/src/components/Pagination/src/Pagination.vue | 6 +++---
2 files changed, 10 insertions(+), 3 deletions(-)
create mode 100644 packages/element3/src/components/Pagination/index.js
diff --git a/packages/element3/src/components/Pagination/index.js b/packages/element3/src/components/Pagination/index.js
new file mode 100644
index 000000000..10b050909
--- /dev/null
+++ b/packages/element3/src/components/Pagination/index.js
@@ -0,0 +1,7 @@
+import ElPagination from './src/Pagination.vue'
+
+ElPagination.install = function (app) {
+ app.component(ElPagination.name, ElPagination)
+}
+
+export { ElPagination }
diff --git a/packages/element3/src/components/Pagination/src/Pagination.vue b/packages/element3/src/components/Pagination/src/Pagination.vue
index 933aa6a95..4fb472bcb 100644
--- a/packages/element3/src/components/Pagination/src/Pagination.vue
+++ b/packages/element3/src/components/Pagination/src/Pagination.vue
@@ -1,5 +1,5 @@
-