import ConfigProvider from '../config-provider'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import cls from 'classnames'; import hoistNonReactStatics from 'hoist-non-react-statics'; import { CommonThemeProps } from '../types'; import { Input } from '../index'; import { Pagination as NextPagination } from '@alifd/next'; import { PaginationProps as NextPaginationProps } from '@alifd/next/types/pagination'; import { getTheme } from '../utils/getTheme'; interface PageStates { currentPage:number; total:number; pageSize:number; inputValue:number; } interface PaginationProps extends NextPaginationProps, CommonThemeProps {} class Pagination extends Component { constructor(props) { super(props); this.state = { currentPage: this.props.current || this.props.defaultCurrent || 1, total: this.props.total || 100, pageSize: this.props.pageSize || 10, inputValue: this.props.current || this.props.defaultCurrent || 1, }; } onInputChange = value => { this.setState({ inputValue: value, }); }; handleJump = e => { const { currentPage, inputValue } = this.state; const { total, pageSize } = this.state; let value = parseInt(String(inputValue), 10); const totalPage = Math.ceil(total / pageSize) <= 0 ? 1 : Math.ceil(total / pageSize); if (isNaN(value)) { value = currentPage; } else if (value < 1) { value = 1; } else if (value > totalPage) { value = totalPage; } if (value && value !== currentPage) { this.onChange(value, e); } }; onKeyDown = e => { if (e.keyCode === 13) { this.handleJump(e); } }; pageNumberRender = index => { const { inputValue } = this.state; const { prefix = 'next-' } = this.props; if (index === this.state.currentPage) { return (
{index}
); } return index; }; onChange = (current, e) => { const { onChange } = this.props; this.setState({ currentPage: current, inputValue: current, }); onChange && onChange(current, e); }; // contextTypes static contextTypes = { theme: PropTypes.string, }; render() { const { prefix = 'next-', type, className } = this.props; const theme = getTheme(this.context, this.props); // mini 模式 if (type === 'mini') { const { prefix = 'next-', className, ...otherProps } = this.props; return ( ); } if (type === 'simple') { const { prefix = 'next-', className, ...otherProps } = this.props; return ( ); } return ; } } hoistNonReactStatics(Pagination, NextPagination); export default ConfigProvider.config(Pagination);