Skip to content

Commit dd03cef

Browse files
dongdong
authored andcommitted
fix: 优化分页子组件
1 parent 8cf8293 commit dd03cef

File tree

6 files changed

+517
-582
lines changed

6 files changed

+517
-582
lines changed

devui/pagination/src/components/jump-page.tsx

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,85 @@
1-
import { defineComponent, PropType } from 'vue';
1+
import { defineComponent, PropType, ref, watch, toRefs, ExtractPropTypes } from 'vue';
2+
3+
const jumpPageProps = {
4+
goToText: String,
5+
size: {
6+
type: String as PropType<'lg' | '' | 'sm'>,
7+
default: ''
8+
},
9+
pageIndex: Number,
10+
showJumpButton: Boolean,
11+
totalPages: Number,
12+
cursor: Number,
13+
onChangeCursorEmit: Function as PropType<(v: number) => void>
14+
}
15+
16+
type JumpPageProps = ExtractPropTypes<typeof jumpPageProps>
217

318
export default defineComponent({
4-
props: {
5-
goToText: String,
6-
size: {
7-
type: String as PropType<'lg' | '' | 'sm'>,
8-
default: ''
9-
},
10-
inputPageNum: Number,
11-
jump: Function,
12-
jumpPageChange: Function,
13-
showJumpButton: Boolean
14-
} as const,
19+
props: jumpPageProps,
20+
emits: ['changeCursorEmit'],
21+
setup(props: JumpPageProps, { emit }) {
22+
const {
23+
pageIndex,
24+
totalPages,
25+
cursor
26+
} = toRefs(props)
27+
28+
// 输入跳转页码
29+
const inputNum = ref(pageIndex.value)
30+
watch(
31+
() => pageIndex.value,
32+
(val: number) => {
33+
inputNum.value = val
34+
}
35+
)
36+
let curPage = pageIndex.value
37+
const jumpPageChange = (currentPage: number) => {
38+
curPage = +currentPage
39+
inputNum.value = currentPage
40+
if (isNaN(currentPage)) {
41+
setTimeout(() => {
42+
inputNum.value = pageIndex.value
43+
}, 300)
44+
}
45+
}
46+
// 跳转指定页码
47+
const jump = (e: KeyboardEvent | 'btn') => {
48+
if (curPage > totalPages.value) {
49+
return
50+
}
51+
if ((e === 'btn' || e.key === 'Enter') && cursor.value !== curPage) {
52+
emit('changeCursorEmit', curPage)
53+
}
54+
}
55+
56+
return {
57+
inputNum,
58+
jumpPageChange,
59+
jump
60+
}
61+
},
1562
render() {
1663
const {
1764
goToText,
1865
size,
19-
inputPageNum,
66+
inputNum,
2067
jumpPageChange,
2168
jump,
2269
showJumpButton
2370
} = this
2471

2572
return (
2673
<div class="devui-jump-container">
27-
{goToText}
74+
{goToText}
2875

2976
<d-input
30-
class={['devui-input', size ? 'devui-input-' + size : '']}
31-
value={String(inputPageNum)}
77+
class={['devui-pagination-input', size ? 'devui-pagination-input-' + size : '']}
78+
size={size}
79+
value={String(inputNum)}
3280
onUpdate:value={jumpPageChange}
3381
onKeydown={jump}
34-
/>
82+
/>
3583

3684
{
3785
// TODO 加入国际化后,替换为当前语言为中文的时候加上 '页'
@@ -42,7 +90,7 @@ export default defineComponent({
4290
<div
4391
class={['devui-jump-button', size ? 'devui-jump-size-' + size : 'devui-jump-size-default']}
4492
onClick={jump.bind(null, 'btn')}
45-
title="跳至"
93+
title={goToText}
4694
>
4795
<div class="devui-pagination-go"></div>
4896
</div>

devui/pagination/src/components/page-nums.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const pageNumBtnProps = {
1212
cursor: Number,
1313
maxItems: Number,
1414
totalPages: Number,
15-
onChangeCursorEmit: Function,
15+
onChangeCursorEmit: Function as PropType<(v: number) => void>,
1616
showTruePageIndex: Boolean
1717
} as const
1818

devui/pagination/src/pagination.scss

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
max-width: 100px;
2626

2727
.devui-select-input {
28-
height: 44px;
28+
height: 46px;
2929
}
3030

3131
.devui-select-item {
32-
height: 44px;
32+
height: 46px;
3333
}
3434
}
3535
}
@@ -124,25 +124,15 @@
124124
vertical-align: middle;
125125
align-items: center;
126126

127-
.devui-input {
127+
.devui-pagination-input {
128128
display: inline-block;
129129
width: 42px;
130-
height: 28px;
131130
vertical-align: middle;
132131
margin: 0 3px;
132+
}
133133

134-
&.devui-input-sm {
135-
height: 24px;
136-
padding: 4px 4px;
137-
font-size: $devui-font-size;
138-
line-height: 1.5;
139-
border-radius: $devui-border-radius;
140-
}
141-
142-
&.devui-input-lg {
143-
width: 56px;
144-
height: 46px;
145-
}
134+
.devui-pagination-input-lg {
135+
width: 56px;
146136
}
147137
}
148138

devui/pagination/src/pagination.tsx

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { defineComponent, computed, ref, nextTick } from 'vue'
1+
import { defineComponent, computed, nextTick } from 'vue'
22
import { ComponentProps, componentProps } from './use-pagination'
33
import { liteSelectOptions } from './utils'
44

5-
import clickoutsideDirective from '../../shared/devui-directive/clickoutside'
6-
75
import ConfigMenu from './components/config-menu'
86
import JumpPage from './components/jump-page'
97
import PageNumBtn from './components/page-nums'
@@ -12,9 +10,6 @@ import './pagination.scss'
1210

1311
export default defineComponent({
1412
name: 'DPagination',
15-
directives: {
16-
clickoutside: clickoutsideDirective
17-
},
1813
components: {
1914
ConfigMenu,
2015
JumpPage,
@@ -41,16 +36,6 @@ export default defineComponent({
4136
emit('update:pageIndex', val)
4237
}
4338
})
44-
const changePageNo = ref(props.pageIndex)
45-
// 输入框显示的页码
46-
const inputPageNum = computed({
47-
get() {
48-
return props.pageIndex
49-
},
50-
set(val: number) {
51-
changePageNo.value = val
52-
}
53-
})
5439
// 每页显示最大条目数量
5540
const currentPageSize = computed({
5641
get() {
@@ -65,27 +50,12 @@ export default defineComponent({
6550

6651
const changeCursorEmit = (val: number) => {
6752
cursor.value = val
68-
changePageNo.value = val
6953
emit('pageIndexChange', val)
7054
}
71-
// 输入跳转页码
72-
const jumpPageChange = (currentPage: string) => {
73-
const curPage = +currentPage
74-
if (isNaN(curPage) || curPage < 1 || curPage > totalPages.value) {
75-
inputPageNum.value = props.pageIndex
76-
return
77-
}
78-
inputPageNum.value = curPage
79-
}
80-
// 跳转指定页码
81-
const jump = (e: KeyboardEvent | 'btn') => {
82-
if ((e === 'btn' || e.key === 'Enter') && cursor.value !== changePageNo.value) {
83-
cursor.value = changePageNo.value
84-
}
85-
}
55+
8656
// 每页条数改变
87-
const pageSizeChange = (value: number) => {
88-
currentPageSize.value = value
57+
const pageSizeChange = (val: Record<string, string | number>) => {
58+
currentPageSize.value = val.value as number
8959
// 页数改变后,如果当前页码超出最大页码时修正
9060
if (props.autoFixPageIndex) {
9161
nextTick(() => {
@@ -94,7 +64,7 @@ export default defineComponent({
9464
}
9565
})
9666
}
97-
emit('pageSizeChange', value)
67+
emit('pageSizeChange', val.value as number)
9868
}
9969
// 极简模式下的跳转页码
10070
const litePageIndexChange = (page: {name: string; value: number;}) => {
@@ -104,10 +74,7 @@ export default defineComponent({
10474
return {
10575
cursor,
10676
totalPages,
107-
jump,
10877
changeCursorEmit,
109-
inputPageNum,
110-
jumpPageChange,
11178
currentPageSize,
11279
pageSizeChange,
11380
litePageOptions,
@@ -118,6 +85,7 @@ export default defineComponent({
11885

11986
const {
12087
total,
88+
pageIndex,
12189
pageSizeOptions,
12290
pageSizeDirection,
12391
preLink,
@@ -139,9 +107,6 @@ export default defineComponent({
139107

140108
cursor,
141109
totalPages,
142-
jump,
143-
inputPageNum,
144-
jumpPageChange,
145110
currentPageSize,
146111
pageSizeChange,
147112
changeCursorEmit,
@@ -206,11 +171,12 @@ export default defineComponent({
206171
{...{
207172
goToText,
208173
size,
209-
inputPageNum,
210-
jumpPageChange,
211-
jump,
174+
pageIndex,
175+
totalPages,
176+
cursor,
212177
showJumpButton
213178
}}
179+
onChangeCursorEmit={changeCursorEmit}
214180
/>
215181
}
216182
{

0 commit comments

Comments
 (0)