Skip to content

Commit a760a10

Browse files
author
逆寒
authored
feat(pagination): upgrade to v3 (#2541)
* feat(pagination): upgrade to v3 * feat(pagination): code review
1 parent c469853 commit a760a10

File tree

6 files changed

+62
-55
lines changed

6 files changed

+62
-55
lines changed

components/locale-provider/LocaleReceiver.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ export default {
4141
const { $slots } = this;
4242
const children = this.children || $slots.default;
4343
const { antLocale } = this.localeData;
44-
return children(this.getLocale(), this.getLocaleCode(), antLocale);
44+
return children?.(this.getLocale(), this.getLocaleCode(), antLocale);
4545
},
4646
};

components/pagination/MiniSelect.jsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import VcSelect, { SelectProps } from '../select';
2-
import { getOptionProps, filterEmpty, getListeners } from '../_util/props-util';
2+
import { getOptionProps, getSlot } from '../_util/props-util';
33

44
export default {
5+
inheritAttrs: false,
56
props: {
67
...SelectProps,
78
},
89
Option: VcSelect.Option,
910
render() {
1011
const selectOptionsProps = getOptionProps(this);
1112
const selelctProps = {
12-
props: {
13-
...selectOptionsProps,
14-
size: 'small',
15-
},
16-
on: getListeners(this),
13+
...selectOptionsProps,
14+
size: 'small',
15+
...this.$attrs,
1716
};
18-
return <VcSelect {...selelctProps}>{filterEmpty(this.$slots.default)}</VcSelect>;
17+
return <VcSelect {...selelctProps}>{getSlot(this)}</VcSelect>;
1918
},
2019
};

components/pagination/Pagination.jsx

+23-24
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import PropTypes from '../_util/vue-types';
22
import VcSelect from '../select';
33
import MiniSelect from './MiniSelect';
44
import LocaleReceiver from '../locale-provider/LocaleReceiver';
5-
import { getOptionProps, getListeners } from '../_util/props-util';
5+
import { getOptionProps } from '../_util/props-util';
66
import VcPagination from '../vc-pagination';
77
import enUS from '../vc-pagination/locale/en_US';
88
import LeftOutlined from '@ant-design/icons-vue/LeftOutlined';
99
import RightOutlined from '@ant-design/icons-vue/RightOutlined';
1010
import DoubleLeftOutlined from '@ant-design/icons-vue/DoubleLeftOutlined';
1111
import DoubleRightOutlined from '@ant-design/icons-vue/DoubleRightOutlined';
1212
import { ConfigConsumerProps } from '../config-provider';
13+
import { inject } from 'vue';
14+
import classNames from 'classnames';
1315

1416
export const PaginationProps = () => ({
1517
total: PropTypes.number,
@@ -42,26 +44,27 @@ export const PaginationConfig = () => ({
4244

4345
export default {
4446
name: 'APagination',
45-
model: {
46-
prop: 'current',
47-
event: 'change.current',
48-
},
47+
inheritAttrs: false,
4948
props: {
5049
...PaginationProps(),
5150
},
52-
inject: {
53-
configProvider: { default: () => ConfigConsumerProps },
51+
52+
setup() {
53+
return {
54+
configProvider: inject('configProvider', ConfigConsumerProps),
55+
};
5456
},
57+
5558
methods: {
5659
getIconsProps(prefixCls) {
5760
const prevIcon = (
5861
<a class={`${prefixCls}-item-link`}>
59-
<LeftOutlined/>
62+
<LeftOutlined />
6063
</a>
6164
);
6265
const nextIcon = (
6366
<a class={`${prefixCls}-item-link`}>
64-
<RightOutlined/>
67+
<RightOutlined />
6568
</a>
6669
);
6770
const jumpPrevIcon = (
@@ -104,19 +107,15 @@ export default {
104107

105108
const isSmall = size === 'small';
106109
const paginationProps = {
107-
props: {
108-
prefixCls,
109-
selectPrefixCls,
110-
...restProps,
111-
...this.getIconsProps(prefixCls),
112-
selectComponentClass: isSmall ? MiniSelect : VcSelect,
113-
locale: { ...contextLocale, ...customLocale },
114-
buildOptionText: buildOptionText || this.$scopedSlots.buildOptionText,
115-
},
116-
class: {
117-
mini: isSmall,
118-
},
119-
on: getListeners(this),
110+
prefixCls,
111+
selectPrefixCls,
112+
...restProps,
113+
...this.getIconsProps(prefixCls),
114+
selectComponentClass: isSmall ? MiniSelect : VcSelect,
115+
locale: { ...contextLocale, ...customLocale },
116+
buildOptionText: buildOptionText || this.$slots.buildOptionText?.(),
117+
...this.$attrs,
118+
class: classNames({ mini: isSmall }, this.$attrs.class),
120119
};
121120

122121
return <VcPagination {...paginationProps} />;
@@ -127,8 +126,8 @@ export default {
127126
<LocaleReceiver
128127
componentName="Pagination"
129128
defaultLocale={enUS}
130-
scopedSlots={{ default: this.renderPagination }}
131-
/>
129+
slots={{ default: this.renderPagination }}
130+
></LocaleReceiver>
132131
);
133132
},
134133
};

components/pagination/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import Base from '../base';
44
export { PaginationProps, PaginationConfig } from './Pagination';
55

66
/* istanbul ignore next */
7-
Pagination.install = function(Vue) {
8-
Vue.use(Base);
9-
Vue.component(Pagination.name, Pagination);
7+
Pagination.install = function(app) {
8+
app.use(Base);
9+
app.component(Pagination.name, Pagination);
1010
};
1111

1212
export default Pagination;

components/vc-pagination/Pager.jsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classNames from 'classnames';
33

44
export default {
55
name: 'Pager',
6+
inheritAttrs: false,
67
props: {
78
rootPrefixCls: PropTypes.string,
89
page: PropTypes.number,
@@ -24,20 +25,27 @@ export default {
2425
},
2526
},
2627
render() {
28+
const { class: _cls, style } = this.$attrs;
2729
const props = this.$props;
2830
const prefixCls = `${props.rootPrefixCls}-item`;
29-
const cls = classNames(prefixCls, `${prefixCls}-${props.page}`, {
30-
[`${prefixCls}-active`]: props.active,
31-
[`${prefixCls}-disabled`]: !props.page,
32-
});
31+
const cls = classNames(
32+
prefixCls,
33+
`${prefixCls}-${props.page}`,
34+
{
35+
[`${prefixCls}-active`]: props.active,
36+
[`${prefixCls}-disabled`]: !props.page,
37+
},
38+
_cls,
39+
);
3340

3441
return (
3542
<li
36-
class={cls}
3743
onClick={this.handleClick}
3844
onKeypress={this.handleKeyPress}
3945
title={this.showTitle ? this.page : null}
4046
tabIndex="0"
47+
class={cls}
48+
style={style}
4149
>
4250
{this.itemRender(this.page, 'page', <a>{this.page}</a>)}
4351
</li>

components/vc-pagination/Pagination.jsx

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import PropTypes from '../_util/vue-types';
22
import BaseMixin from '../_util/BaseMixin';
3-
import { hasProp, getComponentFromProp, getOptionProps } from '../_util/props-util';
3+
import { hasProp, getOptionProps, getComponent } from '../_util/props-util';
44
import Pager from './Pager';
55
import Options from './Options';
66
import LOCALE from './locale/zh_CN';
77
import KEYCODE from './KeyCode';
8+
import classNames from 'classnames';
89

910
function noop() {}
1011

@@ -28,6 +29,7 @@ function calculatePage(p, state, props) {
2829
export default {
2930
name: 'Pagination',
3031
mixins: [BaseMixin],
32+
inheritAttrs: false,
3133
model: {
3234
prop: 'current',
3335
event: 'change.current',
@@ -151,7 +153,7 @@ export default {
151153
},
152154
getItemIcon(icon) {
153155
const { prefixCls } = this.$props;
154-
const iconNode = getComponentFromProp(this, icon, this.$props) || (
156+
const iconNode = getComponent(this, icon, this.$props) || (
155157
<a class={`${prefixCls}-item-link`} />
156158
);
157159
return iconNode;
@@ -359,8 +361,9 @@ export default {
359361
}
360362
const hasPrev = this.hasPrev();
361363
const hasNext = this.hasNext();
364+
362365
return (
363-
<ul class={`${prefixCls} ${prefixCls}-simple`}>
366+
<ul class={classNames(`${prefixCls} ${prefixCls}-simple`, this.$attrs.class)}>
364367
<li
365368
title={this.showTitle ? locale.prev_page : null}
366369
onClick={this.prev}
@@ -409,16 +412,12 @@ export default {
409412
}
410413
if (allPages <= 5 + pageBufferSize * 2) {
411414
const pagerProps = {
412-
props: {
413-
locale,
414-
rootPrefixCls: prefixCls,
415-
showTitle: props.showTitle,
416-
itemRender: props.itemRender,
417-
},
418-
on: {
419-
click: this.handleChange,
420-
keypress: this.runIfEnter,
421-
},
415+
locale,
416+
rootPrefixCls: prefixCls,
417+
showTitle: props.showTitle,
418+
itemRender: props.itemRender,
419+
onClick: this.handleChange,
420+
onKeypress: this.runIfEnter,
422421
};
423422
if (!allPages) {
424423
pagerList.push(
@@ -580,12 +579,14 @@ export default {
580579
}
581580
const prevDisabled = !this.hasPrev() || !allPages;
582581
const nextDisabled = !this.hasNext() || !allPages;
583-
const buildOptionText = this.buildOptionText || this.$scopedSlots.buildOptionText;
582+
const buildOptionText = this.buildOptionText || this.$slots.buildOptionText?.();
583+
const { class: _cls, style } = this.$attrs;
584584
return (
585585
<ul
586-
class={{ [`${prefixCls}`]: true, [`${prefixCls}-disabled`]: disabled }}
587586
unselectable="unselectable"
588587
ref="paginationNode"
588+
style={style}
589+
class={classNames({ [`${prefixCls}`]: true, [`${prefixCls}-disabled`]: disabled }, _cls)}
589590
>
590591
{totalText}
591592
<li

0 commit comments

Comments
 (0)