@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
124 lines (119 loc) • 4.39 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React__default from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix.js';
import setupGetInstanceId from '../../tools/setupGetInstanceId.js';
const getInstanceId = setupGetInstanceId();
function CheckboxGroup(_ref) {
let {
children,
className,
helperText,
invalid,
invalidText,
legendId,
legendText,
orientation,
readOnly,
size = 'default',
warn,
warnText,
disableFocus,
...rest
} = _ref;
const prefix = usePrefix();
const showWarning = !readOnly && !invalid && warn;
const showHelper = !invalid && !warn;
const checkboxGroupInstanceId = getInstanceId();
const helperId = !helperText ? undefined : `checkbox-group-helper-text-${checkboxGroupInstanceId}`;
const helper = helperText ? /*#__PURE__*/React__default.createElement("div", {
id: helperId,
className: `${prefix}--form__helper-text`
}, helperText) : null;
const fieldsetClasses = cx(`${prefix}--fieldset ${prefix}--checkbox-group`, className, {
[`${prefix}--checkbox-group--readonly`]: readOnly,
[`${prefix}--checkbox-group--invalid`]: !readOnly && invalid,
[`${prefix}--checkbox-group--warning`]: showWarning,
[`msk-checkbox-group-horizontal`]: orientation === 'horizontal',
[`msk-checkbox-group-md`]: size === 'md',
[`msk-checkbox-group-lg`]: size === 'lg',
'msk-checkbox-group--disable-focus': disableFocus
});
return /*#__PURE__*/React__default.createElement("fieldset", _extends({
className: fieldsetClasses,
"data-invalid": invalid ? true : undefined,
"aria-labelledby": rest['aria-labelledby'] || legendId,
"aria-readonly": readOnly,
"aria-describedby": !invalid && !warn && helper ? helperId : undefined
}, rest), /*#__PURE__*/React__default.createElement("legend", {
className: `${prefix}--label`,
id: legendId || rest['aria-labelledby']
}, legendText), children, /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--checkbox-group__validation-msg`
}, !readOnly && invalid && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("span", {
className: `${prefix}--checkbox__invalid-icon msk-icon`
}, "error"), /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, invalidText)), showWarning && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("span", {
className: `${prefix}--checkbox__invalid-icon ${prefix}--checkbox__invalid-icon--warning msk-icon`
}, "warning"), /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, warnText))), showHelper && helper);
}
CheckboxGroup.propTypes = {
/**
* Provide the children form elements to be rendered inside of the `<fieldset>`
*/
children: PropTypes.node,
/**
* Provide a custom className to be applied to the containing `<fieldset>` node
*/
className: PropTypes.string,
/**
* Specify whether the form group is currently in warning state
*/
// warn: PropTypes.bool,
/**
* Provide the text that is displayed when the form group is in warning state
*/
// warnText: PropTypes.node,
disableFocus: PropTypes.bool,
/**
* Provide text for the form group for additional help
*/
helperText: PropTypes.node,
/**
* Specify whether the form group is currently invalid
*/
invalid: PropTypes.bool,
/**
* Provide the text that is displayed when the form group is in an invalid state
*/
invalidText: PropTypes.node,
/**
* Provide id for the fieldset <legend> which corresponds to the fieldset
* `aria-labelledby`
*/
legendId: PropTypes.node,
/**
* Provide the text to be rendered inside of the fieldset `<legend>`
*/
legendText: PropTypes.node.isRequired,
/**
* Specify the orientation of them items
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* Whether the CheckboxGroup should be read-only
*/
readOnly: PropTypes.bool,
/**
* Set the size of the checkbox
*/
size: PropTypes.oneOf(['default', 'md', 'lg'])
};
export { CheckboxGroup as default };