import { CssProps, RefProps, bindGlobalStyle, getRenderPageProps } from 'lupine.web'; let _DEFAULT_PAGE_LIMIT = 10; export const getDefaultPageLimit = () => { return _DEFAULT_PAGE_LIMIT; }; export const setDefaultPageLimit = (limit: number) => { _DEFAULT_PAGE_LIMIT = limit; }; const pageLinkOptions = [10, 20, 50, 100, 200, 500]; export type PagingLinkProps = { itemsCount: number; pageLimit?: number; pageIndex?: number; baseLink: string; onClick?: (index: number) => void; // if onClick is set then use it instead of href textPerpage?: string; textOk?: string; textTo?: string; textPage?: string; showControl?: boolean; }; export const PagingLink = ({ itemsCount, pageLimit = getDefaultPageLimit(), pageIndex = 0, baseLink, onClick, textPerpage = '/Page', textOk = 'Go', textTo = 'To', textPage = 'Page', showControl, }: PagingLinkProps) => { const css: CssProps = { display: 'flex', justifyContent: 'end', alignItems: 'center', textAlign: 'right', padding: '6px 16px 6px 0', fontSize: '14px', '.paging-link-index a, .paging-link-index.current': { padding: '2px 6px', textDecoration: 'none', }, '.paging-link-index.current': { fontWeight: 'bold', }, 'span.paging-link-index a:hover, span.paging-link-go a:hover': { textDecoration: 'underline', }, '.paging-link-ctl-box': { display: 'flex', alignItems: 'center', }, '.paging-link-ctl-box .paging-link-jump': { width: '50px', padding: '1px 3px', margin: '0 3px', textAlign: 'right', }, '.paging-link-ctl-box .paging-link-limit': { width: '90px', padding: '1px 3px', margin: '0 3px', }, '.paging-link-ok': { margin: '0 3px', }, }; bindGlobalStyle('paging-link-box', css); pageIndex = pageIndex ?? (Number.parseInt(getRenderPageProps().query['pg_i'] || '') || 0); pageLimit = pageLimit || _DEFAULT_PAGE_LIMIT; let maxPages = Math.floor(itemsCount / pageLimit); if (itemsCount > 0 && pageLimit > 0) { if (itemsCount % pageLimit !== 0) { maxPages++; } if (pageIndex > maxPages) { pageIndex = maxPages - 1; } } const onPageLimitChange = (e: Event) => { const limit = Number((e.target as HTMLSelectElement).value || '0'); if (limit > 0) { setDefaultPageLimit(limit); onClick && onClick(pageIndex); } }; const onOkClick = () => { let index = Number((ref.$('.paging-link-jump') as HTMLInputElement).value || '0'); if (index < 1) { index = 1; } if (index > maxPages) { index = maxPages; } onClick && onClick(index - 1); }; const ref: RefProps = {}; return ( ); };