UNPKG

@carbon/react

Version:

React components for the Carbon Design System

79 lines (75 loc) 2.57 kB
/** * 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 { LayoutConstraint } from '../../Layout/index.js'; import { usePrefix } from '../../../internal/usePrefix.js'; const ContainedListItem = ({ action, children, className, disabled = false, onClick, renderIcon: IconElement, ...rest }) => { const prefix = usePrefix(); const isClickable = onClick !== undefined; const classes = cx(`${prefix}--contained-list-item`, className, { [`${prefix}--contained-list-item--clickable`]: isClickable, [`${prefix}--contained-list-item--with-icon`]: IconElement, [`${prefix}--contained-list-item--with-action`]: action }); const content = /*#__PURE__*/React.createElement(React.Fragment, null, IconElement && /*#__PURE__*/React.createElement("div", { className: `${prefix}--contained-list-item__icon` }, /*#__PURE__*/React.createElement(IconElement, null)), /*#__PURE__*/React.createElement("div", null, children)); return /*#__PURE__*/React.createElement("li", _extends({ className: classes }, rest), isClickable ? /*#__PURE__*/React.createElement("button", { className: `${prefix}--contained-list-item__content`, type: "button", disabled: disabled, onClick: onClick }, content) : /*#__PURE__*/React.createElement("div", { className: `${prefix}--contained-list-item__content` }, content), action && /*#__PURE__*/React.createElement(LayoutConstraint, { size: { min: 'sm', max: 'lg' }, className: `${prefix}--contained-list-item__action` }, action)); }; ContainedListItem.propTypes = { /** * A slot for a possible interactive element to render within the item. */ action: PropTypes.node, /** * The content of this item. Must not contain any interactive elements. Use props.action to include those. */ children: PropTypes.node, /** * Additional CSS class names. */ className: PropTypes.string, /** * Whether this item is disabled. */ disabled: PropTypes.bool, /** * Provide an optional function to be called when the item is clicked. */ onClick: PropTypes.func, /** * A component used to render an icon. */ renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]) }; export { ContainedListItem as default };