@carbon/react
Version:
React components for the Carbon Design System
66 lines (62 loc) • 2.06 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { extends as _extends } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { useId } from '../../../internal/useId.js';
import Select from '../../Select/Select.js';
import '../../Select/Select.Skeleton.js';
import SelectItem from '../../SelectItem/SelectItem.js';
import { usePrefix } from '../../../internal/usePrefix.js';
function PageSelector({
className = null,
currentPage,
id = 1,
labelText = 'Current page number',
totalPages,
...other
}) {
const prefix = usePrefix();
const namespace = `${prefix}--unstable-pagination__page-selector`;
const instanceId = `${namespace}__select-${useId()}`;
const renderPages = total => {
const pages = [];
for (let counter = 1; counter <= total; counter += 1) {
pages.push(/*#__PURE__*/React.createElement(SelectItem, {
key: counter,
value: counter,
text: String(counter)
}));
}
return pages;
};
return /*#__PURE__*/React.createElement(Select, _extends({
className: cx(namespace, className),
hideLabel: true,
id: instanceId || id,
inline: true,
labelText: labelText,
value: currentPage
}, other), renderPages(totalPages));
}
PageSelector.propTypes = {
/** Extra class names to add. */
className: PropTypes.string,
/** The current page. */
currentPage: PropTypes.number.isRequired,
/** The unique ID of this component instance. */
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** Translatable string to label the page selector element. */
labelText: PropTypes.string,
/**
* Total number of pages.
* This value is calculated using a valid `totalItems` prop passed to the parent `Unstable_Pagination`.
*/
totalPages: PropTypes.number.isRequired
};
export { PageSelector as default };