import * as React from "react" import withTranslate from "jamplay-common/i18n/withTranslate" import styled from "styled-components" import _Link from "next/link" import { ResultLinkComponent } from "jamplay-common/routing" import bp from "styled-components-breakpoint" const Link: any = _Link export type PageInfo = { total: number currentPage: number lastPage: number } export interface PaginationPropTypes extends PageInfo, withTranslatePropType { onClick?: (page: number) => void customLink?: ResultLinkComponent customLinkParams?: any url?: { pathname: string query: any } } const PaginationContainer = styled.div.attrs({ className: "PaginationContainer" })` display: flex; align-items: center; a { color: ${(props: any) => props.theme.matteBlack}; } border-radius: 5px; overflow: hidden; background-color: ${(props) => props.theme.matteWhite}; ` const NumberLabel = styled.div.attrs({ className: `heavent NumberLabel` })` display: inline-block; padding: 10px 12px; ${bp("mobile")` font-size: 1rem; `} ${bp("tablet")` font-size: 1rem; `} font-weight: bold; color: ${(props) => props.theme.matteBlack}; cursor: pointer; &:hover { opacity: 0.8; background-color: white; } &.active { border-radius: 5px; color: white; background-color: ${(props: any) => props.theme.pumpkin}; cursor: default; &:hover { opacity: 1; } } &.active-point { border-radius: 5px; color: ${(props) => props.theme.matteBlack}; cursor: default; &:hover { opacity: 1; } } &.ellipsis { padding: 10px 0px; } &.disable { color: lightgray; } ` export interface LinkPaginationPropTypes extends PaginationPropTypes, withTranslatePropType { pageNumber: any bindClickPage: any className: any innerHtml: string aStyle?: any } export const LinkPagination: React.SFC = ( props: LinkPaginationPropTypes ) => { let { t } = props let onClick: any = props.onClick if (!t) { t = (s) => s } if (!onClick || typeof onClick !== "function") { onClick = (s) => s } const bindClick = (page) => () => onClick(page) if (props.customLink) { return ( {props.aStyle ? ( ) : ( )} ) } else if (props.url) { return ( {props.aStyle ? ( ) : ( )} ) } else { return
} } export const Pagination: React.SFC = ( props: PaginationPropTypes ) => { if ( props.lastPage <= 1 || (props.currentPage < 1 || props.currentPage > props.lastPage) ) { return
} if (props.customLink && props.url) { console.warn( "ถ้าส่งมาทั้ง url และ customLink จะใช้ customLink เป็นหลัก เอา url ออกได้เลยนะ" ) } if (!props.customLink && !props.url) { throw new Error("ถ้าไม่ส่ง customLink มาจะต้องส่ง url มานะจ้ะ") } let { t } = props let onClick: any = props.onClick if (!t) { t = (s) => s } if (!onClick || typeof onClick !== "function") { onClick = (s) => s } const pagesNumber: any[] = [] pagesNumber.push( ) if (props.currentPage - 3 < 1) { for (let i = props.currentPage - 3; i < props.currentPage + 2; i++) { if (i < props.lastPage && i >= 1) { const currentPageclassName = props.currentPage === i ? "active" : "" pagesNumber.push( ) } } } else { pagesNumber.push( ) if (props.currentPage - 2 === 2) { for (let i = props.currentPage - 2; i < props.currentPage - 1; i++) { if (i < props.lastPage && i >= 1) { const pageClassName = `${props.currentPage === i ? "active" : ""}` pagesNumber.push( ) } } } else { pagesNumber.push( ) } for (let i = props.currentPage - 1; i < props.currentPage + 2; i++) { if (i < props.lastPage && i >= 1) { const pageClassName = `${props.currentPage === i ? "active" : ""}` pagesNumber.push( ) } } } if (props.currentPage < props.lastPage - 2) { if (props.lastPage - props.currentPage === 3) { for (let i = props.currentPage + 2; i < props.lastPage; i++) { if (i < props.lastPage && i >= 1) { pagesNumber.push( ) } } } else { pagesNumber.push( ) } } const className = props.currentPage === props.lastPage ? "active" : "" pagesNumber.push( ) pagesNumber.push( "} /> ) return {pagesNumber} } export default withTranslate(Pagination) as React.StatelessComponent< PaginationPropTypes >