@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
230 lines (229 loc) • 8.18 kB
JavaScript
"use client";
import React, { useContext, useRef, useEffect, useState } from 'react';
import { dispatchCustomElementEvent, extendPropsWithContext } from "../../shared/component-helper.js";
import { calculatePagination, getDotsAriaLabel } from "./PaginationCalculation.js";
import PaginationContext from "./PaginationContext.js";
import Context from "../../shared/Context.js";
import Button from "../button/Button.js";
import IconPrimary from "../icon-primary/IconPrimary.js";
import styleProperties from "../../style/themes/ui/properties.js";
import { applySpacing } from "../space/SpacingUtils.js";
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const defaultProps = {
buttonTitle: null,
prevTitle: null,
nextTitle: null,
morePages: null,
contentRef: null,
children: null,
space: null
};
const PaginationBar = localProps => {
const context = useContext(PaginationContext);
const props = extendPropsWithContext(localProps, defaultProps, context.pagination);
const {
currentPageInternal,
pageCountInternal,
disabled,
skeleton,
space
} = props;
const focusPage = () => {
const {
onPageUpdate,
contentRef
} = props;
onPageUpdate(() => {
try {
const elem = contentRef.current;
elem.focus();
} catch (e) {}
});
};
const keepPageHeight = () => {
try {
const elem = props.contentRef.current;
const pageHeight = elem.offsetHeight;
elem.style.height = `${pageHeight / 16}rem`;
elem.style.minHeight = elem.style.height;
} catch (e) {}
const {
onPageUpdate
} = props;
onPageUpdate(() => {
try {
const elem = props.contentRef.current;
elem.style.height = 'auto';
elem.style.minHeight = elem.style.height;
} catch (e) {}
});
};
const setPage = (currentPageInternal, event = null) => {
keepPageHeight();
const {
setState: setContextState,
updatePageContent
} = props;
setContextState({
currentPageInternal
});
updatePageContent(currentPageInternal);
dispatchCustomElementEvent(props, 'onChange', {
pageNumber: currentPageInternal,
...props,
event
});
};
const setPrevPage = () => {
setPage(props.currentPageInternal - 1);
};
const setNextPage = () => {
setPage(props.currentPageInternal + 1);
};
const clickHandler = ({
pageNumber,
event
}) => {
setPage(pageNumber, event);
focusPage();
};
const {
getTranslation
} = useContext(Context);
const {
buttonTitle,
prevTitle,
nextTitle,
morePages
} = extendPropsWithContext(props, defaultProps, getTranslation(props).Pagination);
const prevIsDisabled = currentPageInternal > -1 ? currentPageInternal === 1 : true;
const nextIsDisabled = currentPageInternal > -1 ? currentPageInternal === pageCountInternal || pageCountInternal === 0 : true;
const paginationBarRef = useRef(null);
const currentScreenSize = useResizeObserver(paginationBarRef);
const pageNumberGroups = calculatePagination(pageCountInternal, currentPageInternal, currentScreenSize === 'small');
return _jsxs("div", {
ref: paginationBarRef,
...applySpacing({
space
}, {
className: 'dnb-pagination__bar' + (pageCountInternal >= 8 ? " dnb-pagination--many-pages" : "")
}),
children: [_jsxs("div", {
className: "dnb-pagination__bar__wrapper",
children: [_jsxs("div", {
className: "dnb-pagination__bar__skip",
children: [_jsx(Button, {
disabled: disabled || prevIsDisabled,
skeleton: skeleton,
variant: "tertiary",
icon: "chevron_left",
iconPosition: "left",
text: prevTitle,
onClick: setPrevPage,
title: prevIsDisabled ? null : prevTitle
}, "left-arrow"), _jsx(Button, {
disabled: disabled || nextIsDisabled,
skeleton: skeleton,
variant: "tertiary",
icon: "chevron_right",
iconPosition: "right",
text: nextTitle,
onClick: setNextPage,
title: nextIsDisabled ? null : nextTitle
}, "right-arrow")]
}), _jsxs("div", {
className: "dnb-pagination__bar__inner",
children: [((pageNumberGroups === null || pageNumberGroups === void 0 ? void 0 : pageNumberGroups[0]) || []).map(pageNumber => _jsx(Button, {
className: "dnb-pagination__button",
text: String(pageNumber),
"aria-label": buttonTitle.replace('%s', String(pageNumber)),
variant: pageNumber === currentPageInternal ? 'primary' : 'secondary',
disabled: disabled,
skeleton: skeleton,
"aria-current": pageNumber === currentPageInternal ? 'page' : null,
onClick: event => clickHandler({
pageNumber,
event
})
}, pageNumber)), pageNumberGroups.slice(1).map((numbersList, idx) => _jsxs(React.Fragment, {
children: [_jsx(IconPrimary, {
role: "separator",
"aria-orientation": "vertical",
"aria-hidden": false,
title: getDotsAriaLabel({
morePages,
numbersList,
pageNumberGroups
}),
className: "dnb-pagination__dots",
icon: "more",
size: "medium"
}), numbersList.map(pageNumber => {
return _jsx(Button, {
className: 'dnb-pagination__button' + (String(pageNumber).length > 3 ? " dnb-pagination__button--large-number" : ""),
text: String(pageNumber),
"aria-label": buttonTitle.replace('%s', String(pageNumber)),
variant: pageNumber === currentPageInternal ? 'primary' : 'secondary',
disabled: disabled,
skeleton: skeleton,
"aria-current": pageNumber === currentPageInternal ? 'page' : null,
onClick: event => clickHandler({
pageNumber,
event
})
}, (pageNumber || 0) + idx);
})]
}, idx))]
})]
}), _jsx("span", {
className: "dnb-sr-only",
"aria-live": "assertive",
children: buttonTitle.replace('%s', String(currentPageInternal))
})]
});
};
export const useResizeObserver = element => {
const [currentSize, setSize] = useState('large');
const resizeObserver = useRef(null);
useEffect(() => {
try {
var _resizeObserver$curre;
const handleSizeChange = width => {
if (width <= getSizeInPx('small') && currentSize !== 'small') {
setSize('small');
} else if (width <= getSizeInPx('medium') && currentSize !== 'medium') {
setSize('medium');
} else if (width <= getSizeInPx('large') && currentSize !== 'large') {
setSize('large');
} else if (width <= getSizeInPx('x-large') && currentSize !== 'x-large') {
setSize('x-large');
} else if (width <= getSizeInPx('xx-large') && currentSize !== 'xx-large') {
setSize('xx-large');
}
};
resizeObserver.current = new ResizeObserver(entries => {
handleSizeChange(entries[0].contentRect.width);
});
(_resizeObserver$curre = resizeObserver.current) === null || _resizeObserver$curre === void 0 || _resizeObserver$curre.observe(element.current);
handleSizeChange(element.current.clientWidth);
} catch (e) {}
return () => {
var _resizeObserver$curre2;
(_resizeObserver$curre2 = resizeObserver.current) === null || _resizeObserver$curre2 === void 0 || _resizeObserver$curre2.disconnect();
};
}, [element]);
return currentSize;
};
const getSizeInPx = size => {
const styleSize = styleProperties[`--layout-${size}`];
if (styleSize.includes('em')) {
return parseFloat(styleSize.replace(/(rem|em)$/, '')) * 16;
}
return parseFloat(styleSize.replace(/(px)$/, ''));
};
withComponentMarkers(PaginationBar, {
_supportsSpacingProps: true
});
export default PaginationBar;
//# sourceMappingURL=PaginationBar.js.map