import React, { useState, useEffect, FC, KeyboardEvent, useRef } from 'react' import { getLocale } from '@gm-pc/locales' import { InputNumber } from '../input_number' import { Flex } from '../flex' import { PaginationProps } from './types' import { getIndex } from './util' const Right: FC = ({ paging, onChange }) => { const [index, setIndex] = useState(getIndex(paging)) // input focus 的时候存起来,blur 的时候如果一样就不更新 const refIndex = useRef(null) // 响应外部的 index 变化 useEffect(() => { setIndex(getIndex(paging)) }, [paging.offset, paging.limit]) const handleInput = (value: number) => { setIndex(value) } const doEnsureIndex = () => { // 如果不合理,则还原 if (index === null) { setIndex(getIndex(paging)) return } onChange({ ...paging, offset: (index - 1) * paging.limit, }) } const handleFocus = () => { refIndex.current = index } const handleBlur = () => { // 和 focus 一样,值没变,就不触发更新了 if (index === refIndex.current) { return } refIndex.current = index doEnsureIndex() } // @todo useKeyPress const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter') { doEnsureIndex() } } const all = Math.ceil((paging.count || 1) / paging.limit) return (
{`/${all}${getLocale('页')}`}
) } export default Right