Skip to content

Commit

Permalink
fix(Pagination): enhance Pagination and InfinityScroller TypeScript d…
Browse files Browse the repository at this point in the history
…efinitions
  • Loading branch information
tujoworker committed May 31, 2023
1 parent 35c9efb commit 5a7de9d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const InfinityPaginationTable = ({ tableItems, ...props }) => {

const onToggleExpanded = (
{ ssn: _ssn },
{ pageNumber, element = null, onExpanded = null } = {}
{ pageNumber, element = null, onExpanded = null }
) => {
const index = tableItems.findIndex(({ ssn }) => ssn === _ssn)
if (index > -1) {
Expand Down Expand Up @@ -223,7 +223,7 @@ export const InfinityPaginationTable = ({ tableItems, ...props }) => {
marker_element="tr"
fallback_element={({ className, ...props }) => (
<TableRow className={className}>
<TableData colSpan="2" {...props} />
<TableData colSpan={2} {...props} />
</TableRow>
)} // in order to show the injected "indicator" and "load button" in the middle of the orw
current_page={currentPage}
Expand All @@ -241,7 +241,7 @@ InfinityPaginationTable.propTypes = {
}

const InfinityPagination = ({
children,
children = null,
items,
currentPage,
perPageCount,
Expand Down Expand Up @@ -270,8 +270,8 @@ const InfinityPagination = ({
const params = {
onClick: (e) => {
if (
!hasSelectedText(e.currentTarget) ||
/button/.test(document.activeElement.type)
!hasSelectedText() ||
/button/.test(document.activeElement.tagName)
) {
let element = e.currentTarget
onToggleExpanded(item, {
Expand All @@ -293,7 +293,7 @@ const InfinityPagination = ({
}

// we do this only to have a working useEffect, so we can call onMounted
const trRef = React.createRef(null)
const trRef = React.createRef<HTMLTableRowElement>()
mountedItems.push({ ...item, element: trRef })

return (
Expand Down Expand Up @@ -326,9 +326,9 @@ const InfinityPagination = ({
className={`expanded-content dnb-no-focus ${
item.expanded ? 'expanded' : ''
}`}
tabIndex="-1"
tabIndex={-1}
>
<TableData colSpan="2">
<TableData colSpan={2}>
{item.expanded && (
<div className="expanded-content__outer">
<div className="expanded-content__inner">
Expand Down Expand Up @@ -428,11 +428,7 @@ const TableData = styled.td`
}
`

const setHeight = ({
element,
expanded = false,
animation = true,
} = {}) => {
const setHeight = ({ element, expanded = false, animation = true }) => {
if (
element &&
typeof window !== 'undefined' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import { H1, P, Ul } from '@dnb/eufemia/src/elements'
import Table from '@dnb/eufemia/src/components/Table'
import { hasSelectedText } from '@dnb/eufemia/src/shared/helpers'

import { createPagination } from '@dnb/eufemia/src/components/Pagination'
import {
createPagination,
CreatePaginationReturn,
} from '@dnb/eufemia/src/components/Pagination'

import { Pagination } from '@dnb/eufemia/src'

const HeightLimit = styled.div`
type HeightLimitProps = {
height?: string
}

const HeightLimit = styled.div<HeightLimitProps>`
height: ${(props) => props.height || '20rem'};
overflow-y: scroll;
background-color: var(--color-white);
Expand Down Expand Up @@ -157,7 +164,7 @@ export const InfinityPaginationTable = ({ tableItems, ...props }) => {
// create our Pagination instance
const [
{ Pagination, setContent, resetContent, resetInfinity, endInfinity },
] = React.useState(createPagination)
] = React.useState<CreatePaginationReturn>(createPagination)
const [orderDirection, setOrderDirection] = React.useState('asc')
const [currentPage, setLocalPage] = React.useState(null)
const [cacheHash, forceRerender] = React.useState(null) // eslint-disable-line
Expand All @@ -172,7 +179,7 @@ export const InfinityPaginationTable = ({ tableItems, ...props }) => {

const onToggleExpanded = (
{ ssn: _ssn },
{ pageNumber, element = null, onExpanded = null } = {}
{ pageNumber, element = null, onExpanded = null }
) => {
const index = tableItems.findIndex(({ ssn }) => ssn === _ssn)
if (index > -1) {
Expand Down Expand Up @@ -292,7 +299,7 @@ export const InfinityPaginationTable = ({ tableItems, ...props }) => {
marker_element="tr"
fallback_element={({ className, ...props }) => (
<TableRow className={className}>
<TableData colSpan="2" {...props} />
<TableData colSpan={2} {...props} />
</TableRow>
)} // in order to show the injected "indicator" and "load button" in the middle of the row
current_page={currentPage}
Expand All @@ -310,7 +317,7 @@ InfinityPaginationTable.propTypes = {
}

const InfinityPagination = ({
children,
children = null,
items,
currentPage,
perPageCount,
Expand All @@ -337,19 +344,18 @@ const InfinityPagination = ({

return items.map((item, i) => {
const params = {
onClick: (e) => {
onClick: (e: React.MouseEvent) => {
if (
!hasSelectedText(e.currentTarget) ||
/button/.test(document.activeElement.type)
!hasSelectedText() ||
/button/.test(document.activeElement.tagName)
) {
let element = e.currentTarget
let element = e.currentTarget as HTMLButtonElement
onToggleExpanded(item, {
pageNumber: currentPage,
// element,
onExpanded: () => {
try {
// rather find the next tr
element = element.nextElementSibling
element = element.nextElementSibling as HTMLButtonElement
setHeight({ element, expanded: !item.expanded })
element.focus() // for better ally we set the focus to the new content
} catch (e) {
Expand All @@ -362,7 +368,7 @@ const InfinityPagination = ({
}

// we do this only to have a working useEffect, so we can call onMounted
const trRef = React.createRef(null)
const trRef = React.createRef<HTMLTableRowElement>()
mountedItems.push({ ...item, element: trRef })

return (
Expand Down Expand Up @@ -395,9 +401,9 @@ const InfinityPagination = ({
className={`expanded-content dnb-no-focus ${
item.expanded ? 'expanded' : ''
}`}
tabIndex="-1"
tabIndex={-1}
>
<TableData colSpan="2">
<TableData colSpan={2}>
{item.expanded && (
<div className="expanded-content__outer">
<div className="expanded-content__inner">
Expand Down Expand Up @@ -497,19 +503,25 @@ const TableData = styled.td`
}
`

type HeightProps = {
element: HTMLButtonElement | HTMLTableCellElement
expanded?: boolean
animation?: boolean
}

const setHeight = ({
element,
expanded = false,
animation = true,
} = {}) => {
}: HeightProps) => {
if (
element &&
typeof window !== 'undefined' &&
window.requestAnimationFrame
) {
// get tr element
if (String(element.nodeName).toLowerCase() === 'td') {
element = element.parentElement
if (/td/.test(element.nodeName)) {
element = element.parentElement as HTMLTableCellElement
}

// get the new height
Expand Down
12 changes: 12 additions & 0 deletions packages/dnb-eufemia/src/components/pagination/Pagination.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,15 @@ export interface PaginationContentProps {
children: PaginationContentChildren;
}
declare const PaginationContent: React.FC<PaginationContentProps>;

export type CreatePaginationReturn = {
Pagination: (props?: Record<string, unknown>) => JSX.Element;
InfinityMarker: (props?: Record<string, unknown>) => void;
setContent: (pageNumber: number, content: React.ReactDom) => void;
resetContent: () => void;
resetInfinity: () => void;
endInfinity: () => void;
};

export const createPagination = (initProps?: Record<string, unknown>) =>
CreatePaginationReturn;
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ const InfinityPagination = ({
const params = {
onClick: (e) => {
if (
!hasSelectedText(e.currentTarget) ||
/button/.test(document.activeElement.type)
!hasSelectedText() ||
/button/.test(document.activeElement.tagName)
) {
let element = e.currentTarget
onToggleExpanded(item, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const InfinityPagination = ({
return items.map((item) => {
const params = {
onClick: (e) => {
if (!hasSelectedText(e.currentTarget)) {
if (!hasSelectedText()) {
onToggleExpanded(item, currentPage, e.currentTarget)
}
},
Expand Down

0 comments on commit 5a7de9d

Please sign in to comment.