import { Component } from 'react'; import memoize from '../../utils/memorize-one'; import Select, { ISelectItem } from '../../select'; import { I18nReceiver as Receiver, II18nLocalePagination } from '../../i18n'; export interface IPaginationPageSizeCompoundOption { text: React.ReactNode; value: number; } export type PaginationPageSizeOption = | number | IPaginationPageSizeCompoundOption; export interface IPaginationPageSizeChangerBaseProps { total: number; formatTotal?: (total: number) => React.ReactNode; pageSize: number; } export interface IPaginationPageSizeChangerProps extends IPaginationPageSizeChangerBaseProps { pageSizeOptions?: PaginationPageSizeOption[]; onPageSizeChange?: (pageSize: number) => void; } const memoizedNormalizeSelectOptions = memoize(function normalizeSelectOptions( pageSizeOptions: PaginationPageSizeOption[], i18n: II18nLocalePagination ): ISelectItem[] { return (pageSizeOptions || []).map(opt => { if (typeof opt === 'number') { return { key: opt, text: `${opt} ${i18n.items}` }; } return { key: opt.value, text: opt.text, }; }); }); const Text: React.FunctionComponent<{ type: 'middle' | 'right' }> = props => { return ( {props.children} ); }; export default class PageSizeChanger extends Component< IPaginationPageSizeChangerProps, any > { render() { const { total, formatTotal, pageSize, pageSizeOptions, onPageSizeChange } = this.props; if (!pageSizeOptions || pageSizeOptions.length === 0) { return ( ); } const totalText = formatTotal ? formatTotal(total) : total; return ( {i18n => { const select = ( ); return (
{i18n.pageStats({ select, total: totalText, Text, })}
); }}
); } } class PageSizeSelect extends Component< { i18n: II18nLocalePagination; pageSize: number; pageSizeOptions: PaginationPageSizeOption[]; onPageSizeChange(pageSize: number): void; }, any > { render() { const { pageSize, i18n, pageSizeOptions } = this.props; const options = memoizedNormalizeSelectOptions(pageSizeOptions, i18n); return (