/** * @description vue version pagination * @author 阿怪 * @date 2023/05/25 23:29 * @version v1.0.0 * * 江湖的业务千篇一律,复杂的代码好几百行。 */ import { defineComponent, ref, watch } from 'vue'; import { props } from './api.ts'; import { usePagination } from './usePagination.ts'; import './pagination.css'; import { PaginationProps } from './index'; export default defineComponent((_props: PaginationProps, { emit }) => { const props = _props as Required; const currentValueRef = ref(props.modelValue ?? 1); watch(() => props.modelValue, val => { currentValueRef.value = val; }); const { getPageNumList, getPageBtnLength } = usePagination({ props, value: { currentValue: currentValueRef } }); const changePage = (page: number) => { currentValueRef.value = page; emit('update:modelValue', page); emit('change', page); }; const toPrev = () => { if (currentValueRef.value === 1) return; changePage(currentValueRef.value - 1); }; const toNext = () => { if (currentValueRef.value === getPageBtnLength()) return; changePage(currentValueRef.value + 1); }; return () => { const pages = getPageNumList(); const layoutKeys = props.layout.split(',').map(key => key.trim()); const btnGetter = { total: () =>
{props.total}
, prev: () =>
toPrev()}/>, pager: () => pages.map(page => { return
changePage(page.jump!)}>{page.value}
; }), next: () =>
toNext()}/>, }; return
{layoutKeys.map(key => btnGetter[key as keyof typeof btnGetter]?.())}
; }; }, { name: 'MPagination', props, emits: ['update:modelValue', 'change'], });