@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
117 lines (113 loc) • 3.56 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import React__default from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { LayoutConstraint } from '../Layout/index.js';
import { useId } from '../../internal/useId.js';
import { usePrefix } from '../../internal/usePrefix.js';
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('contained-list')}-header`;
const prefix = usePrefix();
const classes = cx(`${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.createElement("div", {
className: classes
}, /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--contained-list__header`
}, /*#__PURE__*/React__default.createElement("div", {
id: labelId,
className: `${prefix}--contained-list__label`
}, label), /*#__PURE__*/React__default.createElement(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.createElement("ul", {
role: "list",
"aria-labelledby": labelId
}, isActionSearch ? filteredChildren : renderedChildren));
}
ContainedList.propTypes = {
/**
* A slot for a possible interactive element to render.
*/
action: PropTypes.node,
/**
* A collection of ContainedListItems to be rendered in the ContainedList
*/
children: PropTypes.node,
/**
* Additional CSS class names.
*/
className: PropTypes.string,
/**
* Specify whether the dividing lines in between list items should be inset.
*/
isInset: PropTypes.bool,
/**
* The kind of ContainedList you want to display
*/
kind: PropTypes.oneOf(variants),
/**
* A label describing the contained list.
*/
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
/**
* Specify the size of the contained list.
*/
size: PropTypes.oneOf(['sm', 'md', 'lg', 'xl'])
};
export { ContainedList as default };