UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

254 lines 9.46 kB
import _extends from "@babel/runtime/helpers/esm/extends"; var _InfinityScroller; import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import PaginationContext from "./PaginationContext.js"; import PaginationProvider from "./PaginationProvider.js"; import { validateDOMAttributes, extendPropsWithContextInClassComponent } from "../../shared/component-helper.js"; import { spacingPropTypes, createSpacingClasses } from "../space/SpacingHelper.js"; import { PaginationIndicator } from "./PaginationHelpers.js"; import InfinityScroller from "./PaginationInfinity.js"; import PaginationBar from "./PaginationBar.js"; const paginationPropTypes = process.env.NODE_ENV !== "production" ? { startup_page: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), current_page: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), page_count: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), startup_count: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), parallel_load_count: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), place_maker_before_content: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), place_marker_before_content: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), min_wait_time: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), mode: PropTypes.oneOf(['pagination', 'infinity']), paginationBarLayout: PropTypes.oneOf(['vertical', 'horizontal']), use_load_button: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), items: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), hide_progress_indicator: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), set_content_handler: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), reset_content_handler: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), reset_pagination_handler: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), end_infinity_handler: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), page_element: PropTypes.oneOfType([PropTypes.object, PropTypes.node, PropTypes.func, PropTypes.string]), fallback_element: PropTypes.oneOfType([PropTypes.object, PropTypes.node, PropTypes.func, PropTypes.string]), marker_element: PropTypes.oneOfType([PropTypes.object, PropTypes.node, PropTypes.func, PropTypes.string]), indicator_element: PropTypes.oneOfType([PropTypes.node, PropTypes.func, PropTypes.string]), align: PropTypes.string, button_title: PropTypes.string, prev_title: PropTypes.string, next_title: PropTypes.string, more_pages: PropTypes.string, is_loading_text: PropTypes.string, load_button_text: PropTypes.string, loadButton: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), barSpace: spacingPropTypes.space, ...spacingPropTypes, className: PropTypes.string, children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), on_change: PropTypes.func, on_startup: PropTypes.func, on_load: PropTypes.func, on_end: PropTypes.func } : {}; const paginationDefaultProps = { startup_page: null, current_page: null, page_count: null, mode: 'pagination', paginationBarLayout: 'vertical', use_load_button: false, items: null, hide_progress_indicator: false, set_content_handler: null, reset_content_handler: null, page_element: undefined, fallback_element: undefined, marker_element: undefined, indicator_element: undefined, align: 'left', button_title: null, prev_title: null, next_title: null, more_pages: null, is_loading_text: null, load_button_text: null, loadButton: null, barSpace: null, startup_count: 1, parallel_load_count: 1, place_maker_before_content: false, place_marker_before_content: false, min_wait_time: 400, disabled: null, skeleton: null, className: null, children: null, on_change: null, on_startup: null, on_load: null, on_end: null }; export default class Pagination extends React.PureComponent { static defaultProps = paginationDefaultProps; render() { return React.createElement(PaginationProvider, _extends({ tagName: "dnb-pagination", internalContent: this.props.children }, this.props), React.createElement(PaginationInstance, this.props)); } } process.env.NODE_ENV !== "production" ? Pagination.propTypes = { ...paginationPropTypes } : void 0; class PaginationInstance extends React.PureComponent { static defaultProps = paginationDefaultProps; static contextType = PaginationContext; constructor(props) { super(props); this._contentRef = React.createRef(); } render() { const props = extendPropsWithContextInClassComponent(this.props, paginationDefaultProps, this.context.getTranslation(this.props).Pagination, this.context.Pagination); const { align, children, className, barSpace, paginationBarLayout, disabled: _disabled, skeleton: _skeleton, tagName: _tagName, page_count: _page_count, current_page: _current_page, startup_page: _startup_page, mode: _mode, hide_progress_indicator: _hide_progress_indicator, use_load_button: _use_load_button, currentPage: _currentPage, ...attributes } = props; const { currentPage, items, fallback_element, indicator_element } = this.context.pagination; if (this.context.pagination.mode === 'pagination') { const mainParams = { className: classnames('dnb-pagination', createSpacingClasses(props), className, align && `dnb-pagination--${align}`, paginationBarLayout && `dnb-pagination--layout-${paginationBarLayout}`), ...attributes }; validateDOMAttributes(props, mainParams); const content = items.find(({ pageNumber }) => pageNumber === currentPage)?.content; return React.createElement("div", mainParams, React.createElement(PaginationBar, { contentRef: this._contentRef, space: barSpace }, children), items.length > 0 && React.createElement(PaginationContent, { ref: this._contentRef }, content || React.createElement(PaginationIndicator, { indicator_element: indicator_element || fallback_element }))); } return _InfinityScroller || (_InfinityScroller = React.createElement(InfinityScroller, null)); } } process.env.NODE_ENV !== "production" ? PaginationInstance.propTypes = { ...paginationPropTypes } : void 0; export class InfinityMarker extends React.PureComponent { static defaultProps = paginationDefaultProps; render() { const { children, ...props } = this.props; return React.createElement(PaginationProvider, _extends({ useMarkerOnly: true, tagName: "dnb-infinity-marker" }, props), React.createElement(InfinityScroller, props, children)); } } process.env.NODE_ENV !== "production" ? InfinityMarker.propTypes = { ...paginationPropTypes } : void 0; const PaginationContent = React.forwardRef(({ children, ...props }, ref) => { return React.createElement("div", _extends({ className: "dnb-pagination__content dnb-no-focus", tabIndex: "-1" }, props, { ref: ref }), children); }); process.env.NODE_ENV !== "production" ? PaginationContent.propTypes = { children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired } : void 0; Pagination.Bar = PaginationBar; Pagination.Content = PaginationContent; const PaginationWrapper = Pagination; const InfinityMarkerWrapper = InfinityMarker; export const Bar = props => React.createElement(Pagination, _extends({ fallback_element: () => null }, props)); export const createPagination = (initProps = {}) => { const store = React.createRef({}); const rerender = React.createRef(null); const _setContent = React.createRef(null); const _resetContent = React.createRef(null); const _resetInfinity = React.createRef(null); const _endInfinity = React.createRef(null); const setContent = (pageNumber, content) => { if (pageNumber > 0) { store.current = { ...store.current, ...{ pageNumber, content } }; rerender.current && rerender.current(store); } }; const resetContent = () => { _resetContent.current && _resetContent.current(); }; const resetInfinity = () => { _resetInfinity.current && _resetInfinity.current(); }; const endInfinity = () => { _endInfinity.current && _endInfinity.current(); }; const args = props => ({ ...{ ...initProps, ...props }, store, rerender, set_content_handler: fn => _setContent.current = fn, reset_content_handler: fn => _resetContent.current = fn, reset_pagination_handler: fn => _resetInfinity.current = fn, end_infinity_handler: fn => _endInfinity.current = fn }); const Pagination = props => React.createElement(PaginationWrapper, _extends({ tagName: "dnb-pagination" }, args(props))); const InfinityMarker = props => React.createElement(InfinityMarkerWrapper, _extends({ tagName: InfinityMarkerWrapper?.tagName }, args(props))); return { Pagination, InfinityMarker, setContent, resetContent, resetInfinity, endInfinity }; }; Pagination._supportsSpacingProps = true; //# sourceMappingURL=Pagination.js.map