Skip to content

Migrate Pagination to TypeScript #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-lamps-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `Pagination` to TypeScript
43 changes: 33 additions & 10 deletions src/Pagination/Pagination.js → src/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import styled from 'styled-components'
import sx from '../sx'
import {get, COMMON} from '../constants'
import {ComponentProps} from '../utils/types'
import theme from '../theme'
import Box from '../Box'
import {buildPaginationModel, buildComponentData} from './model'
Expand Down Expand Up @@ -109,6 +110,17 @@ const Page = styled.a`
${COMMON};
`

type UsePaginationPagesParameters = {
theme?: object // set to theme type once /src/theme.js is converted
pageCount: number
currentPage: number
onPageChange: (e: React.MouseEvent, n: number) => void
hrefBuilder: (n: number) => string
marginPageCount: number
showPages?: boolean
surroundingPageCount: number
}

function usePaginationPages({
theme,
pageCount,
Expand All @@ -118,11 +130,11 @@ function usePaginationPages({
marginPageCount,
showPages,
surroundingPageCount
}) {
const pageChange = React.useCallback(n => e => onPageChange(e, n), [onPageChange])
}: UsePaginationPagesParameters) {
const pageChange = React.useCallback(n => (e: React.MouseEvent) => onPageChange(e, n), [onPageChange])

const model = React.useMemo(() => {
return buildPaginationModel(pageCount, currentPage, showPages, marginPageCount, surroundingPageCount)
return buildPaginationModel(pageCount, currentPage, !!showPages, marginPageCount, surroundingPageCount)
}, [pageCount, currentPage, showPages, marginPageCount, surroundingPageCount])

const children = React.useMemo(() => {
Expand All @@ -146,17 +158,28 @@ const PaginationContainer = styled.nav`
${sx};
`

export type PaginationProps = {
theme: object
pageCount: number
currentPage: number
onPageChange?: (e: React.MouseEvent, n: number) => void
hrefBuilder?: (n: number) => string
marginPageCount: number
showPages?: boolean
surroundingPageCount?: number
}

function Pagination({
theme,
pageCount,
currentPage,
onPageChange,
hrefBuilder,
marginPageCount,
showPages,
surroundingPageCount,
onPageChange = noop,
hrefBuilder = defaultHrefBuilder,
marginPageCount = 1,
showPages = true,
surroundingPageCount = 2,
...rest
}) {
}: PaginationProps) {
const pageElements = usePaginationPages({
theme,
pageCount,
Expand All @@ -176,7 +199,7 @@ function Pagination({
)
}

function defaultHrefBuilder(pageNum) {
function defaultHrefBuilder(pageNum: number) {
return `#${pageNum}`
}

Expand Down
File renamed without changes.
27 changes: 22 additions & 5 deletions src/Pagination/model.js → src/Pagination/model.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export function buildPaginationModel(pageCount, currentPage, showPages, marginPageCount, surroundingPageCount) {
export function buildPaginationModel(
pageCount: number,
currentPage: number,
showPages: boolean,
marginPageCount: number,
surroundingPageCount: number
) {
const pages = []

if (showPages) {
const pageNums = []
const addPage = n => {
const pageNums: Array<number> = []
const addPage = (n: number) => {
if (n >= 1 && n <= pageCount) {
pageNums.push(n)
}
Expand Down Expand Up @@ -113,7 +119,18 @@ export function buildPaginationModel(pageCount, currentPage, showPages, marginPa
return [prev, ...pages, next]
}

export function buildComponentData(page, hrefBuilder, onClick) {
type PageType = {
type: string
num: number
disabled?: boolean
selected?: boolean
}

export function buildComponentData(
page: PageType,
hrefBuilder: (n: number) => string,
onClick: (e: React.MouseEvent) => void
) {
const props = {}
let content = ''
let key = ''
Expand Down Expand Up @@ -151,7 +168,7 @@ export function buildComponentData(page, hrefBuilder, onClick) {
}
case 'NUM': {
key = `page-${page.num}`
content = page.num
content = String(page.num)
if (page.selected) {
Object.assign(props, {as: 'em', 'aria-current': 'page'})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'babel-polyfill'
import {buildPaginationModel} from '../../Pagination/model'

function first(array, count = 1) {
function first(array: Array<any>, count = 1) {
const slice = array.slice(0, count)
return count === 1 ? slice[0] : slice
}

function last(array, count = 1) {
function last(array: Array<any>, count = 1) {
const len = array.length
const slice = array.slice(len - count, len)
return count === 1 ? slice[0] : slice
Expand Down