Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 2x 22x 22x 22x 22x 2x 2x 2x | import React, { useState } from "react"; import classNames from "classnames"; import { StyledProps, Combine } from "../_type"; import { Icon } from "../icon"; import { Tooltip } from "../tooltip"; import { useTranslation } from "../i18n"; import { Select } from "../select"; import { useConfig } from "../_util/config-context"; /** 表示分页信息 */ export interface PagingQuery { /** 页码:从 1 开始索引 */ pageIndex?: number; /** 页长:表示每页记录数 */ pageSize?: number; } export interface PaginationProps extends Combine<StyledProps, Partial<PagingQuery>> { /** * 必传,数据总个数,用于计算页数 */ recordCount: number; /** * 切换分页回调函数 */ onPagingChange?: (query: PagingQuery) => void; /** * 切换页长时,是否自动把页码重设为 1 * @default true */ isPagingReset?: boolean; /** 分页组件显示的说明信息,不传将渲染数据个数信息 */ stateText?: React.ReactNode; /** * 支持的页长设置 * @default [10, 20, 30, 50, 100, 200] */ pageSizeOptions?: number[]; /** * 是否显示状态文案 * @default true */ stateTextVisible?: boolean; /** * 是否显示页长选择 * @default true */ pageSizeVisible?: boolean; /** * 是否显示页码输入 * @default true */ pageIndexVisible?: boolean; /** * 是否显示切页按钮(上一页/下一页) * @default true */ jumpVisible?: boolean; /** * 是否显示第一页和最后一页按钮 * @default true */ endJumpVisible?: boolean; } export const defaultPageSizeOptions = [10, 20, 30, 50, 100, 200]; const noop = () => {}; export function Pagination({ pageIndex, pageSize, pageSizeOptions, recordCount, onPagingChange = noop, isPagingReset = true, stateText, stateTextVisible, pageSizeVisible, pageIndexVisible, jumpVisible, endJumpVisible, style, className, }: PaginationProps) { const { classPrefix } = useConfig(); const t = useTranslation(); Eif (!Array.isArray(pageSizeOptions) || pageSizeOptions.length === 0) { pageSizeOptions = defaultPageSizeOptions; // eslint-disable-line no-param-reassign } // 为非受控组件提供状态管理 const [internalPaging, setInternalPaging] = useState<PagingQuery>({ pageIndex: 1, pageSize: pageSizeOptions[0], }); // 非受控组件,使用内部状态 Eif (typeof pageIndex === "undefined") { // eslint-disable-next-line prefer-destructuring,no-param-reassign pageIndex = internalPaging.pageIndex; } Eif (typeof pageSize === "undefined") { // eslint-disable-next-line prefer-destructuring,no-param-reassign pageSize = internalPaging.pageSize; } // 状态改变同时更新内部状态和通知外部 // eslint-disable-next-line no-param-reassign onPagingChange = (onPagingChange => (query: PagingQuery) => { setInternalPaging(query); onPagingChange(query); })(onPagingChange); // 总页数 const pageCount = Math.max(1, Math.ceil(recordCount / pageSize)); // 改变页长 const changePageSize = (pageSize: number) => { onPagingChange({ pageIndex: isPagingReset ? 1 : pageIndex, pageSize, }); }; // 改变页码 const changePageIndex = (pageIndex: number) => onPagingChange({ pageIndex, pageSize, }); // 渲染分页状态信息 const stateElement = (() => { Iif (stateTextVisible === false) { return null; } return ( <div className={`${classPrefix}-pagination__state`}> <span className={`${classPrefix}-pagination__text`}> {typeof stateText === "undefined" ? t.paginationRecordCount(recordCount) : stateText} </span> </div> ); })(); // 操作区元素 let operatationElements: JSX.Element[] = []; // 页码输入 Eif (pageIndexVisible !== false) { operatationElements = [ <PageIndexInput key="page-index-input" pageIndex={pageIndex} pageCount={pageCount} onChange={changePageIndex} classPrefix={classPrefix} />, ]; } // 页码跳转 Eif (jumpVisible !== false) { operatationElements = [ <PaginationButton key="jump-prev" type="pre" icon="arrowleft" disabled={pageIndex <= 1} title={pageIndex > 1 ? t.paginationPrevPage : t.paginationAtFirst} onClick={() => changePageIndex(pageIndex - 1)} classPrefix={classPrefix} />, ...operatationElements, <PaginationButton key="jump-next" type="next" icon="arrowright" disabled={pageIndex >= pageCount} title={ pageIndex < pageCount ? t.paginationNextPage : t.paginationAtLast } onClick={() => changePageIndex(pageIndex + 1)} classPrefix={classPrefix} />, ]; } // 页头页尾跳转 Eif (endJumpVisible !== false) { operatationElements = [ <PaginationButton key="jump-first" type="first" icon="firstpage" disabled={pageIndex <= 1} title={pageIndex > 1 ? t.paginationToFirstPage : t.paginationAtFirst} onClick={() => changePageIndex(1)} classPrefix={classPrefix} />, ...operatationElements, <PaginationButton key="jump-last" type="last" icon="lastpage" disabled={pageIndex >= pageCount} title={ pageIndex < pageCount ? t.paginationToLastPage : t.paginationAtLast } onClick={() => changePageIndex(pageCount)} classPrefix={classPrefix} />, ]; } // 页长选择 Eif (pageSizeVisible !== false) { operatationElements = [ <span key="page-size-text" className={`${classPrefix}-pagination__text`}> {t.paginationRecordPerPage} </span>, <Select key="page-size-select" type="simulate" options={pageSizeOptions.map(page => ({ value: String(page), text: page, }))} value={String(pageSize)} onChange={pageSize => changePageSize(Number(pageSize))} placement="top" boxClassName="size-auto-width" />, ...operatationElements, ]; } return ( <div className={classNames(`${classPrefix}-pagination`, className)} style={style} > {stateElement} <div className={`${classPrefix}-pagination__operate`}> {operatationElements} </div> </div> ); } export interface PaginationButtonProps { type: "pre" | "next" | "first" | "last" | "cur"; icon: string; disabled?: boolean; title?: string; onClick?: React.MouseEventHandler; classPrefix: string; } export function PaginationButton({ type, disabled, icon, title, onClick, classPrefix, }: PaginationButtonProps) { const buttonClassName = classNames({ [`${classPrefix}-pagination__turnbtn`]: true, [`${classPrefix}-pagination__${type}btn`]: type, "is-disabled": disabled, }); const button = ( <a className={buttonClassName} onClick={disabled ? null : onClick} onDoubleClick={evt => evt.preventDefault()} style={{ userSelect: "none" }} > <Icon type={icon} /> </a> ); Eif (title) { return <Tooltip title={title}>{button}</Tooltip>; } return button; } interface PageIndexInputProps { pageIndex: number; pageCount: number; onChange: (pageIndex: number) => void; classPrefix: string; } function PageIndexInput({ pageIndex, pageCount, onChange, classPrefix, }: PageIndexInputProps) { const [inputValue, setInputValue] = useState(null); const t = useTranslation(); return ( <div className={`${classPrefix}-pagination__manualinput`}> <input type="text" className={`${classPrefix}-input ${classPrefix}-pagination__inputpagenum`} value={inputValue === null ? String(pageIndex) : inputValue} onChange={evt => setInputValue(evt.target.value)} onBlur={() => setInputValue(null)} onKeyDown={evt => { // enter to confirm if (evt.keyCode === 13) { const parsedPageIndex = parseInt(inputValue, 10); if ( parsedPageIndex !== pageIndex && parsedPageIndex >= 1 && parsedPageIndex <= pageCount ) { onChange(parsedPageIndex); } setInputValue(null); } // up/down key else if (evt.keyCode === 38 || evt.keyCode === 40) { let nextPageValue = parseInt(inputValue, 10); if (Number.isNaN(nextPageValue)) { nextPageValue = pageIndex; } if (evt.keyCode === 38) { nextPageValue = Math.max(1, nextPageValue + 1); } else if (evt.keyCode === 40) { nextPageValue = Math.min(pageCount, nextPageValue - 1); } setInputValue(String(nextPageValue)); } }} /> <span className={`${classPrefix}-pagination__totalpage`}> {t.paginationPageCount(pageCount)} </span> </div> ); } |