@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
127 lines (119 loc) • 4.21 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var PropTypes = require('prop-types');
var cx = require('classnames');
var index = require('../Layout/index.js');
var useId = require('../../internal/useId.js');
var usePrefix = require('../../internal/usePrefix.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
const variants = ['on-page', 'disclosed'];
function filterChildren(children) {
if (Array.isArray(children)) {
return children?.filter(child => !['Search', 'ExpandableSearch'].includes(child?.type?.displayName));
}
if (children && !['Search', 'ExpandableSearch'].includes(children?.type?.displayName)) {
return children;
}
return null;
}
function renderChildren(children) {
if (Array.isArray(children)) {
children.map((child, index) => {
if (index === 0 && child.type?.displayName === 'Search') {
return child;
}
return child;
});
}
if (children && children.type?.displayName === 'Search') {
return children;
}
return children;
}
function ContainedList(_ref) {
let {
action,
children,
className,
isInset,
kind = variants[0],
label,
size
} = _ref;
const labelId = `${useId.useId('contained-list')}-header`;
const prefix = usePrefix.usePrefix();
const classes = cx__default["default"](`${prefix}--contained-list`, {
[`${prefix}--contained-list--inset-rulers`]: isInset,
[`${prefix}--contained-list--${size}`]: size,
// TODO: V12 - Remove this class
[`${prefix}--layout--size-${size}`]: size
}, `${prefix}--contained-list--${kind}`, className);
const filteredChildren = filterChildren(children);
const isActionSearch = ['Search', 'ExpandableSearch'].includes(action?.type?.displayName);
const renderedChildren = renderChildren(children);
return /*#__PURE__*/React__default["default"].createElement("div", {
className: classes
}, /*#__PURE__*/React__default["default"].createElement("div", {
className: `${prefix}--contained-list__header`
}, /*#__PURE__*/React__default["default"].createElement("div", {
id: labelId,
className: `${prefix}--contained-list__label`
}, label), /*#__PURE__*/React__default["default"].createElement(index.LayoutConstraint, {
size: {
min: 'sm',
max: 'xl'
},
className: `${prefix}--contained-list__action`
}, action)), children &&
/*#__PURE__*/
/**
* Webkit removes implicit "list" semantics when "list-style-type: none" is set.
* Explicitly setting the "list" role ensures assistive technology in webkit
* browsers correctly announce the semantics.
*
* Ref https://bugs.webkit.org/show_bug.cgi?id=170179#c1
*/
// eslint-disable-next-line jsx-a11y/no-redundant-roles
React__default["default"].createElement("ul", {
role: "list",
"aria-labelledby": labelId
}, isActionSearch ? filteredChildren : renderedChildren));
}
ContainedList.propTypes = {
/**
* A slot for a possible interactive element to render.
*/
action: PropTypes__default["default"].node,
/**
* A collection of ContainedListItems to be rendered in the ContainedList
*/
children: PropTypes__default["default"].node,
/**
* Additional CSS class names.
*/
className: PropTypes__default["default"].string,
/**
* Specify whether the dividing lines in between list items should be inset.
*/
isInset: PropTypes__default["default"].bool,
/**
* The kind of ContainedList you want to display
*/
kind: PropTypes__default["default"].oneOf(variants),
/**
* A label describing the contained list.
*/
label: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].node]).isRequired,
/**
* Specify the size of the contained list.
*/
size: PropTypes__default["default"].oneOf(['sm', 'md', 'lg', 'xl'])
};
exports["default"] = ContainedList;