UNPKG

@carbon/react

Version:

React components for the Carbon Design System

214 lines (206 loc) 8.09 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. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('react'); var cx = require('classnames'); require('../Text/index.js'); var deprecate = require('../../prop-types/deprecate.js'); var usePrefix = require('../../internal/usePrefix.js'); var iconsReact = require('@carbon/icons-react'); var useId = require('../../internal/useId.js'); var noopFn = require('../../internal/noopFn.js'); var index = require('../AILabel/index.js'); var utils = require('../../internal/utils.js'); var Text = require('../Text/Text.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); const Checkbox = /*#__PURE__*/React__default["default"].forwardRef(({ className, decorator, helperText, id, labelText, onChange = noopFn.noopFn, onClick, indeterminate = false, invalid, invalidText, hideLabel, readOnly, title = '', warn, warnText, slug, ...other }, ref) => { const prefix = usePrefix.usePrefix(); const showWarning = !readOnly && !invalid && warn; const showHelper = !invalid && !warn; const checkboxGroupInstanceId = useId.useId(); const helperId = !helperText ? undefined : `checkbox-helper-text-${checkboxGroupInstanceId}`; const helper = helperText ? /*#__PURE__*/React__default["default"].createElement("div", { id: helperId, className: `${prefix}--form__helper-text` }, helperText) : null; const wrapperClasses = cx__default["default"](`${prefix}--form-item`, `${prefix}--checkbox-wrapper`, className, { [`${prefix}--checkbox-wrapper--readonly`]: readOnly, [`${prefix}--checkbox-wrapper--invalid`]: !readOnly && invalid, [`${prefix}--checkbox-wrapper--warning`]: showWarning, [`${prefix}--checkbox-wrapper--slug`]: slug, [`${prefix}--checkbox-wrapper--decorator`]: decorator }); const innerLabelClasses = cx__default["default"](`${prefix}--checkbox-label-text`, { [`${prefix}--visually-hidden`]: hideLabel }); const candidate = slug ?? decorator; const candidateIsAILabel = utils.isComponentElement(candidate, index.AILabel); const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/React.cloneElement(candidate, { size: candidate.props.kind === 'inline' ? 'md' : 'mini' }) : null; return /*#__PURE__*/React__default["default"].createElement("div", { className: wrapperClasses }, /*#__PURE__*/React__default["default"].createElement("input", _rollupPluginBabelHelpers["extends"]({}, other, { type: "checkbox", "data-invalid": invalid ? true : undefined, onChange: evt => { if (!readOnly && onChange) { onChange(evt, { checked: evt.target.checked, id }); } }, className: `${prefix}--checkbox`, id: id, ref: el => { if (el) { el.indeterminate = indeterminate ?? false; } if (typeof ref === 'function') { ref(el); } else if (ref && 'current' in ref) { ref.current = el; } } // readonly attribute not applicable to type="checkbox" // see - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox , "aria-readonly": readOnly, onClick: evt => { if (readOnly) { // prevent default stops the checkbox being updated evt.preventDefault(); } // pass onClick event on to the user even if readonly if (onClick) { onClick(evt); } } })), /*#__PURE__*/React__default["default"].createElement("label", { htmlFor: id, className: `${prefix}--checkbox-label`, title: title }, /*#__PURE__*/React__default["default"].createElement(Text.Text, { className: innerLabelClasses }, labelText, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--checkbox-wrapper-inner--decorator` }, normalizedDecorator) : '')), /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--checkbox__validation-msg` }, !readOnly && invalid && /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement(iconsReact.WarningFilled, { className: `${prefix}--checkbox__invalid-icon` }), /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-requirement` }, invalidText)), showWarning && /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement(iconsReact.WarningAltFilled, { className: `${prefix}--checkbox__invalid-icon ${prefix}--checkbox__invalid-icon--warning` }), /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-requirement` }, warnText))), showHelper && helper); }); Checkbox.propTypes = { /** * Specify whether the underlying input should be checked */ checked: PropTypes__default["default"].bool, /** * Specify an optional className to be applied to the <label> node */ className: PropTypes__default["default"].string, /** * **Experimental**: Provide a decorator component to be rendered inside the `Checkbox` component */ decorator: PropTypes__default["default"].node, /** * Specify whether the underlying input should be checked by default */ defaultChecked: PropTypes__default["default"].bool, /** * Specify whether the Checkbox should be disabled */ disabled: PropTypes__default["default"].bool, /** * Provide text for the form group for additional help */ helperText: PropTypes__default["default"].node, /** * Specify whether the label should be hidden, or not */ hideLabel: PropTypes__default["default"].bool, /** * Provide an `id` to uniquely identify the Checkbox input */ id: PropTypes__default["default"].string.isRequired, /** * Specify whether the Checkbox is in an indeterminate state */ indeterminate: PropTypes__default["default"].bool, /** * Specify whether the Checkbox is currently invalid */ invalid: PropTypes__default["default"].bool, /** * Provide the text that is displayed when the Checkbox is in an invalid state */ invalidText: PropTypes__default["default"].node, /** * Provide a label to provide a description of the Checkbox input that you are * exposing to the user */ labelText: PropTypes__default["default"].node.isRequired, /** * Provide an optional handler that is called when the internal state of * Checkbox changes. This handler is called with event and state info. * `(event, { checked, id }) => void` */ onChange: PropTypes__default["default"].func, /** * Specify whether the Checkbox is read-only */ readOnly: PropTypes__default["default"].bool, /** * **Experimental**: Provide a `Slug` component to be rendered inside the `Checkbox` component */ slug: deprecate["default"](PropTypes__default["default"].node, 'The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead.'), /** * Specify a title for the <label> node for the Checkbox */ title: PropTypes__default["default"].string, /** * Specify whether the Checkbox is currently in warning state */ warn: PropTypes__default["default"].bool, /** * Provide the text that is displayed when the Checkbox is in warning state */ warnText: PropTypes__default["default"].node }; Checkbox.displayName = 'Checkbox'; exports["default"] = Checkbox;