From 3885fd00ed1fb63fe9bc05cf8d2e05145d0b7b17 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 19 Nov 2021 10:52:16 +0800 Subject: [PATCH] feat(Pagination): refactor DOM to improve a11y (#9901) --- packages/vant/src/pagination/Pagination.tsx | 154 ++++++------ packages/vant/src/pagination/index.less | 63 ++--- .../test/__snapshots__/demo.spec.ts.snap | 223 +++++++++++------- .../test/__snapshots__/index.spec.ts.snap | 128 ++++++---- 4 files changed, 336 insertions(+), 232 deletions(-) diff --git a/packages/vant/src/pagination/Pagination.tsx b/packages/vant/src/pagination/Pagination.tsx index 1ecc4a0cc..daa33cf26 100644 --- a/packages/vant/src/pagination/Pagination.tsx +++ b/packages/vant/src/pagination/Pagination.tsx @@ -1,10 +1,11 @@ -import { computed, watch, defineComponent, ExtractPropTypes } from 'vue'; +import { computed, watchEffect, defineComponent, ExtractPropTypes } from 'vue'; import { - BORDER, + clamp, makeStringProp, makeNumberProp, makeNumericProp, createNamespace, + BORDER_SURROUND, } from '../utils'; const [name, bem, t] = createNamespace('pagination'); @@ -57,10 +58,6 @@ export default defineComponent({ const showPageSize = +props.showPageSize; const { modelValue, forceEllipses } = props; - if (props.mode !== 'multi') { - return items; - } - // Default page limits let startPage = 1; let endPage = pageCount; @@ -101,85 +98,96 @@ export default defineComponent({ return items; }); - const select = (page: number, emitChange?: boolean) => { - page = Math.min(count.value, Math.max(1, page)); + const updateModelValue = (value: number, emitChange?: boolean) => { + value = clamp(value, 1, count.value); - if (props.modelValue !== page) { - emit('update:modelValue', page); + if (props.modelValue !== value) { + emit('update:modelValue', value); if (emitChange) { - emit('change', page); + emit('change', value); } } }; - watch( - () => props.modelValue, - (value) => { - select(value); - }, - { immediate: true } + // format modelValue + watchEffect(() => updateModelValue(props.modelValue)); + + const renderDesc = () => ( +
  • + {slots.pageDesc + ? slots.pageDesc() + : `${props.modelValue}/${count.value}`} +
  • ); - const renderDesc = () => { - if (props.mode !== 'multi') { - return ( -
  • - {slots.pageDesc - ? slots.pageDesc() - : `${props.modelValue}/${count.value}`} -
  • - ); - } - }; - - return () => { - const value = props.modelValue; - const simple = props.mode !== 'multi'; - - const onSelect = (value: number) => () => select(value, true); - + const renderPrevButton = () => { + const { mode, modelValue } = props; + const slot = slots['prev-text']; + const disabled = modelValue === 1; return ( - + {slot ? slot() : props.prevText || t('prev')} + + ); }; + + const renderNextButton = () => { + const { mode, modelValue } = props; + const slot = slots['next-text']; + const disabled = modelValue === count.value; + return ( +
  • + +
  • + ); + }; + + const renderPages = () => + pages.value.map((page) => ( +
  • + +
  • + )); + + return () => ( + + ); }, }); diff --git a/packages/vant/src/pagination/index.less b/packages/vant/src/pagination/index.less index 33f67be1c..2d50a9540 100644 --- a/packages/vant/src/pagination/index.less +++ b/packages/vant/src/pagination/index.less @@ -13,9 +13,12 @@ } .van-pagination { - display: flex; font-size: var(--van-pagination-font-size); + &__items { + display: flex; + } + &__item, &__page-desc { display: flex; @@ -33,58 +36,56 @@ cursor: pointer; user-select: none; + button { + flex: 1; + height: 100%; + border: none; + padding: 0; + background: transparent; + + &[disabled] { + cursor: not-allowed; + } + } + &:active { color: var(--van-white); background-color: var(--van-pagination-item-default-color); } - &::after { - border-width: var(--van-border-width-base) 0 var(--van-border-width-base) - var(--van-border-width-base); - } - - &:last-child::after { - border-right-width: var(--van-border-width-base); + &:not(:last-child)::after { + border-right-width: 0; } &--active { color: var(--van-white); background-color: var(--van-pagination-item-default-color); } - } - &__prev, - &__next { - padding: 0 var(--van-padding-base); - cursor: pointer; - } + &--page { + flex-grow: 0; + } - &__item--disabled { - &, - &:active { + &--prev, + &--next { + padding: 0 var(--van-padding-base); + cursor: pointer; + } + + &--border::after { + border-width: var(--van-border-width-base); + } + + &--disabled { color: var(--van-pagination-item-disabled-color); background-color: var(--van-pagination-item-disabled-background-color); - cursor: not-allowed; opacity: var(--van-pagination-disabled-opacity); } } - &__page { - flex-grow: 0; - } - &__page-desc { flex: 1; height: var(--van-pagination-height); color: var(--van-pagination-desc-color); } - - &--simple { - .van-pagination__prev, - .van-pagination__next { - &::after { - border-width: var(--van-border-width-base); - } - } - } } diff --git a/packages/vant/src/pagination/test/__snapshots__/demo.spec.ts.snap b/packages/vant/src/pagination/test/__snapshots__/demo.spec.ts.snap index 1887362a9..48fbe2ebb 100644 --- a/packages/vant/src/pagination/test/__snapshots__/demo.spec.ts.snap +++ b/packages/vant/src/pagination/test/__snapshots__/demo.spec.ts.snap @@ -2,92 +2,151 @@ exports[`should render demo and match snapshot 1`] = `
    - -
    -
    - + +
    - +
    - + +
    +
    +
    `; diff --git a/packages/vant/src/pagination/test/__snapshots__/index.spec.ts.snap b/packages/vant/src/pagination/test/__snapshots__/index.spec.ts.snap index 88bddc7ed..cdbd2a479 100644 --- a/packages/vant/src/pagination/test/__snapshots__/index.spec.ts.snap +++ b/packages/vant/src/pagination/test/__snapshots__/index.spec.ts.snap @@ -1,53 +1,89 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should render page slot correctly 1`] = ` - + `; exports[`should render prev-text、next-text slot correctly 1`] = ` - + `;