feat(pagination): item-count, by the way fix page not updated in uncontrolled mode

This commit is contained in:
07akioni 2021-05-18 20:21:14 +08:00
parent 3539427bb8
commit 808c568066
7 changed files with 65 additions and 13 deletions

View File

@ -13,11 +13,13 @@
- `n-tree` add `virtual-scroll` prop. - `n-tree` add `virtual-scroll` prop.
- `n-data-table` add `virtual-scroll` prop. - `n-data-table` add `virtual-scroll` prop.
- `n-cascader` add `virtual-scroll` prop. - `n-cascader` add `virtual-scroll` prop.
- `n-pagination` add `item-count` prop.
### Fixes ### Fixes
- Fix `n-layout-sider` doesn't show menu after collapsed. - Fix `n-layout-sider` doesn't show menu after collapsed.
- Fix `n-input-number` doesn't reset to origin value when blur with invalid value. - Fix `n-input-number` doesn't reset to origin value when blur with invalid value.
- Fix `n-pagination` doesn't update page in uncontrolled mode.
## 2.7.4 ## 2.7.4

View File

@ -13,11 +13,13 @@
- `n-tree` 新增 `virtual-scroll` 属性 - `n-tree` 新增 `virtual-scroll` 属性
- `n-data-table` 新增 `virtual-scroll` 属性 - `n-data-table` 新增 `virtual-scroll` 属性
- `n-cascader` 新增 `virtual-scroll` 属性 - `n-cascader` 新增 `virtual-scroll` 属性
- `n-pagination` 新增 `item-count` 属性
### Fixes ### Fixes
- 修正 `n-layout-sider` 折叠后不显示菜单 - 修正 `n-layout-sider` 折叠后不显示菜单
- 修正 `n-input-number` 在输入不合法 blur 时没有重设回原始值 - 修正 `n-input-number` 在输入不合法 blur 时没有重设回原始值
- 修正 `n-pagination` 在非受控模式下不更新页数
## 2.7.4 ## 2.7.4

View File

@ -12,6 +12,7 @@ slot
quick-jumper quick-jumper
size-picker size-picker
disabled disabled
item-count
``` ```
## Props ## Props

View File

@ -0,0 +1,7 @@
# Use Item Count
Sometime you don't want set `page-count`. At that time you may use `item-count` and `page-size`.
```html
<n-pagination :item-count="201" :page-sizes="[10, 20, 30, 40]" showSizePicker />
```

View File

@ -12,6 +12,7 @@ slot
quick-jumper quick-jumper
size-picker size-picker
disabled disabled
item-count
``` ```
## Props ## Props

View File

@ -0,0 +1,7 @@
# 使用元素个数控制分页
有的时候你不想传递 `page-count`,这个时候你可以设定 `item-count``page-size`
```html
<n-pagination :item-count="201" :page-sizes="[10, 20, 30, 40]" showSizePicker />
```

View File

@ -4,10 +4,10 @@ import {
computed, computed,
ref, ref,
toRef, toRef,
watch,
defineComponent, defineComponent,
PropType, PropType,
CSSProperties CSSProperties,
watchEffect
} from 'vue' } from 'vue'
import { useMergedState } from 'vooks' import { useMergedState } from 'vooks'
import { NSelect } from '../../select' import { NSelect } from '../../select'
@ -26,7 +26,7 @@ import { paginationLight, PaginationTheme } from '../styles'
import { pageItems } from './utils' import { pageItems } from './utils'
import type { PageItem } from './utils' import type { PageItem } from './utils'
import style from './styles/index.cssr' import style from './styles/index.cssr'
import { call, ExtractPublicPropTypes, MaybeArray } from '../../_utils' import { call, ExtractPublicPropTypes, MaybeArray, warn } from '../../_utils'
import type { Size as InputSize } from '../../input/src/interface' import type { Size as InputSize } from '../../input/src/interface'
import type { Size as SelectSize } from '../../select/src/interface' import type { Size as SelectSize } from '../../select/src/interface'
@ -91,6 +91,16 @@ export default defineComponent({
name: 'Pagination', name: 'Pagination',
props: paginationProps, props: paginationProps,
setup (props) { setup (props) {
if (__DEV__) {
watchEffect(() => {
if (props.pageCount !== undefined && props.itemCount !== undefined) {
warn(
'pagination',
"`pageCount` and `itemCount` should't be specified together."
)
}
})
}
const { NConfigProvider, mergedClsPrefixRef } = useConfig(props) const { NConfigProvider, mergedClsPrefixRef } = useConfig(props)
const themeRef = useTheme( const themeRef = useTheme(
'Pagination', 'Pagination',
@ -116,6 +126,15 @@ export default defineComponent({
toRef(props, 'pageSize'), toRef(props, 'pageSize'),
uncontrolledPageSizeRef uncontrolledPageSizeRef
) )
const mergedPageCountRef = computed(() => {
const { pageCount } = props
if (pageCount !== undefined) return pageCount
const { itemCount } = props
if (itemCount !== undefined) {
return Math.ceil(itemCount / mergedPageSizeRef.value)
}
return 1
})
const showFastForwardRef = ref(false) const showFastForwardRef = ref(false)
const showFastBackwardRef = ref(false) const showFastBackwardRef = ref(false)
@ -148,7 +167,6 @@ export default defineComponent({
selfEl.classList.remove('transition-disabled') selfEl.classList.remove('transition-disabled')
}) })
} }
watch(mergedPageRef, disableTransitionOneTick)
function doUpdatePage (page: number): void { function doUpdatePage (page: number): void {
if (page === mergedPageRef.value) return if (page === mergedPageRef.value) return
const { 'onUpdate:page': _onUpdatePage, onUpdatePage, onChange } = props const { 'onUpdate:page': _onUpdatePage, onUpdatePage, onChange } = props
@ -156,6 +174,7 @@ export default defineComponent({
if (onUpdatePage) call(onUpdatePage, page) if (onUpdatePage) call(onUpdatePage, page)
// deprecated // deprecated
if (onChange) call(onChange, page) if (onChange) call(onChange, page)
uncontrolledPageRef.value = page
} }
function doUpdatePageSize (pageSize: number): void { function doUpdatePageSize (pageSize: number): void {
if (pageSize === mergedPageSizeRef.value) return if (pageSize === mergedPageSizeRef.value) return
@ -168,10 +187,16 @@ export default defineComponent({
if (onUpdatePageSize) call(onUpdatePageSize, pageSize) if (onUpdatePageSize) call(onUpdatePageSize, pageSize)
// deprecated // deprecated
if (onPageSizeChange) call(onPageSizeChange, pageSize) if (onPageSizeChange) call(onPageSizeChange, pageSize)
uncontrolledPageSizeRef.value = pageSize
// update new page when overflows.
// we may have different update strategy, but i've no time to impl it
if (mergedPageCountRef.value < mergedPageRef.value) {
doUpdatePage(mergedPageCountRef.value)
}
} }
function forward (): void { function forward (): void {
if (props.disabled) return if (props.disabled) return
const page = Math.min(mergedPageRef.value + 1, props.pageCount || 1) const page = Math.min(mergedPageRef.value + 1, mergedPageCountRef.value)
doUpdatePage(page) doUpdatePage(page)
} }
function backward (): void { function backward (): void {
@ -183,7 +208,7 @@ export default defineComponent({
if (props.disabled) return if (props.disabled) return
const page = Math.min( const page = Math.min(
mergedPageRef.value + (props.pageSlot - 4), mergedPageRef.value + (props.pageSlot - 4),
props.pageCount || 1 mergedPageCountRef.value
) )
doUpdatePage(page) doUpdatePage(page)
} }
@ -201,7 +226,7 @@ export default defineComponent({
if ( if (
!Number.isNaN(page) && !Number.isNaN(page) &&
page >= 1 && page >= 1 &&
page <= (props.pageCount || 1) page <= mergedPageCountRef.value
) { ) {
doUpdatePage(page) doUpdatePage(page)
jumperValueRef.value = '' jumperValueRef.value = ''
@ -254,6 +279,11 @@ export default defineComponent({
function handleJumperInput (value: string): void { function handleJumperInput (value: string): void {
jumperValueRef.value = value jumperValueRef.value = value
} }
watchEffect(() => {
void mergedPageRef.value
void mergedPageSizeRef.value
disableTransitionOneTick()
})
return { return {
mergedClsPrefix: mergedClsPrefixRef, mergedClsPrefix: mergedClsPrefixRef,
locale: localeRef, locale: localeRef,
@ -263,13 +293,15 @@ export default defineComponent({
showFastBackward: showFastBackwardRef, showFastBackward: showFastBackwardRef,
showFastForward: showFastForwardRef, showFastForward: showFastForwardRef,
pageItems: computed(() => pageItems: computed(() =>
pageItems(mergedPageRef.value, props.pageCount || 1, props.pageSlot) pageItems(mergedPageRef.value, mergedPageCountRef.value, props.pageSlot)
), ),
jumperValue: jumperValueRef, jumperValue: jumperValueRef,
pageSizeOptions: pageSizeOptionsRef, pageSizeOptions: pageSizeOptionsRef,
mergedPageSize: mergedPageSizeRef,
inputSize: inputSizeRef, inputSize: inputSizeRef,
selectSize: selectSizeRef, selectSize: selectSizeRef,
mergedTheme: themeRef, mergedTheme: themeRef,
mergedPageCount: mergedPageCountRef,
handleJumperInput, handleJumperInput,
handleBackwardClick: backward, handleBackwardClick: backward,
handleForwardClick: forward, handleForwardClick: forward,
@ -367,7 +399,7 @@ export default defineComponent({
disabled, disabled,
cssVars, cssVars,
mergedPage, mergedPage,
pageCount, mergedPageCount,
pageItems, pageItems,
showFastBackward, showFastBackward,
showFastForward, showFastForward,
@ -377,7 +409,7 @@ export default defineComponent({
locale, locale,
inputSize, inputSize,
selectSize, selectSize,
pageSize, mergedPageSize,
pageSizeOptions, pageSizeOptions,
jumperValue, jumperValue,
handleJumperInput, handleJumperInput,
@ -401,7 +433,7 @@ export default defineComponent({
<div <div
class={[ class={[
`${mergedClsPrefix}-pagination-item ${mergedClsPrefix}-pagination-item--button`, `${mergedClsPrefix}-pagination-item ${mergedClsPrefix}-pagination-item--button`,
(mergedPage <= 1 || mergedPage > (pageCount || 1) || disabled) && (mergedPage <= 1 || mergedPage > mergedPageCount || disabled) &&
`${mergedClsPrefix}-pagination-item--disabled` `${mergedClsPrefix}-pagination-item--disabled`
]} ]}
onClick={handleBackwardClick} onClick={handleBackwardClick}
@ -456,7 +488,7 @@ export default defineComponent({
`${mergedClsPrefix}-pagination-item ${mergedClsPrefix}-pagination-item--button`, `${mergedClsPrefix}-pagination-item ${mergedClsPrefix}-pagination-item--button`,
{ {
[`${mergedClsPrefix}-pagination-item--disabled`]: [`${mergedClsPrefix}-pagination-item--disabled`]:
mergedPage < 1 || mergedPage >= (pageCount || 1) || disabled mergedPage < 1 || mergedPage >= mergedPageCount || disabled
} }
]} ]}
onClick={handleForwardClick} onClick={handleForwardClick}
@ -470,7 +502,7 @@ export default defineComponent({
size={selectSize} size={selectSize}
placeholder="" placeholder=""
options={pageSizeOptions} options={pageSizeOptions}
value={pageSize} value={mergedPageSize}
disabled={disabled} disabled={disabled}
theme={mergedTheme.peers.Select} theme={mergedTheme.peers.Select}
themeOverrides={mergedTheme.peerOverrides.Select} themeOverrides={mergedTheme.peerOverrides.Select}