@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
254 lines • 11.3 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _InfinityScroller;
const _excluded = ["align", "children", "className", "barSpace", "paginationBarLayout", "disabled", "skeleton", "tagName", "page_count", "current_page", "startup_page", "mode", "hide_progress_indicator", "use_load_button", "currentPage"],
_excluded2 = ["children"],
_excluded3 = ["children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import PaginationContext from './PaginationContext';
import PaginationProvider from './PaginationProvider';
import { validateDOMAttributes, extendPropsWithContextInClassComponent } from '../../shared/component-helper';
import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper';
import { PaginationIndicator } from './PaginationHelpers';
import InfinityScroller from './PaginationInfinity';
import PaginationBar from './PaginationBar';
const paginationPropTypes = process.env.NODE_ENV !== "production" ? _objectSpread(_objectSpread({
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 {
render() {
return React.createElement(PaginationProvider, _extends({
tagName: "dnb-pagination",
internalContent: this.props.children
}, this.props), React.createElement(PaginationInstance, this.props));
}
}
_defineProperty(Pagination, "defaultProps", paginationDefaultProps);
process.env.NODE_ENV !== "production" ? Pagination.propTypes = _objectSpread({}, paginationPropTypes) : void 0;
class PaginationInstance extends React.PureComponent {
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
} = props,
attributes = _objectWithoutProperties(props, _excluded);
const {
currentPage,
items,
fallback_element,
indicator_element
} = this.context.pagination;
if (this.context.pagination.mode === 'pagination') {
var _items$find;
const mainParams = _objectSpread({
className: classnames('dnb-pagination', createSpacingClasses(props), className, align && `dnb-pagination--${align}`, paginationBarLayout && `dnb-pagination--layout-${paginationBarLayout}`)
}, attributes);
validateDOMAttributes(props, mainParams);
const content = (_items$find = items.find(_ref => {
let {
pageNumber
} = _ref;
return pageNumber === currentPage;
})) === null || _items$find === void 0 ? void 0 : _items$find.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));
}
}
_defineProperty(PaginationInstance, "defaultProps", paginationDefaultProps);
_defineProperty(PaginationInstance, "contextType", PaginationContext);
process.env.NODE_ENV !== "production" ? PaginationInstance.propTypes = _objectSpread({}, paginationPropTypes) : void 0;
export class InfinityMarker extends React.PureComponent {
render() {
const _this$props = this.props,
{
children
} = _this$props,
props = _objectWithoutProperties(_this$props, _excluded2);
return React.createElement(PaginationProvider, _extends({
useMarkerOnly: true,
tagName: "dnb-infinity-marker"
}, props), React.createElement(InfinityScroller, props, children));
}
}
_defineProperty(InfinityMarker, "defaultProps", paginationDefaultProps);
process.env.NODE_ENV !== "production" ? InfinityMarker.propTypes = _objectSpread({}, paginationPropTypes) : void 0;
const PaginationContent = React.forwardRef((_ref2, ref) => {
let {
children
} = _ref2,
props = _objectWithoutProperties(_ref2, _excluded3);
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 = function () {
let initProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
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 = _objectSpread(_objectSpread({}, 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 => _objectSpread(_objectSpread({}, _objectSpread(_objectSpread({}, 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 === null || InfinityMarkerWrapper === void 0 ? void 0 : InfinityMarkerWrapper.tagName
}, args(props)));
return {
Pagination,
InfinityMarker,
setContent,
resetContent,
resetInfinity,
endInfinity
};
};
Pagination._supportsSpacingProps = true;
//# sourceMappingURL=Pagination.js.map