@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
125 lines (117 loc) • 3.87 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var PropTypes = require('prop-types');
var React = require('react');
var deprecate = require('../../prop-types/deprecate.js');
var usePrefix = require('../../internal/usePrefix.js');
var useMergedRefs = require('../../internal/useMergedRefs.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);
/** @type any */
const InlineCheckbox = /*#__PURE__*/React__default["default"].forwardRef(function InlineCheckbox(props, forwardRef) {
const {
['aria-label']: ariaLabel,
ariaLabel: deprecatedAriaLabel,
checked = false,
disabled,
id,
indeterminate,
name,
onChange = () => {},
onClick,
onKeyDown,
title
} = props;
const prefix = usePrefix.usePrefix();
const inputRef = React.useRef(null);
const ref = useMergedRefs.useMergedRefs([inputRef, forwardRef]);
const inputProps = {
checked,
className: `${prefix}--checkbox`,
disabled,
id,
name,
onClick,
onChange: evt => {
onChange(evt.target.checked, id, evt);
},
onKeyDown,
ref,
type: 'checkbox'
};
if (indeterminate) {
inputProps.checked = false;
}
React.useEffect(() => {
if (inputRef?.current) {
inputRef.current.indeterminate = indeterminate;
}
}, [indeterminate]);
return /*#__PURE__*/React__default["default"].createElement("div", {
className: `${prefix}--checkbox--inline`
}, /*#__PURE__*/React__default["default"].createElement("input", inputProps),
/*#__PURE__*/
/* eslint-disable jsx-a11y/label-has-for,jsx-a11y/label-has-associated-control,jsx-a11y/click-events-have-key-events,jsx-a11y/no-noninteractive-element-interactions */
React__default["default"].createElement("label", {
htmlFor: id,
className: `${prefix}--checkbox-label`,
"aria-label": deprecatedAriaLabel || ariaLabel,
title: title,
onClick: evt => {
evt.stopPropagation();
}
}));
});
InlineCheckbox.propTypes = {
/**
* Specify the label for the control
*/
['aria-label']: PropTypes__default["default"].string.isRequired,
/**
* Deprecated, please use `aria-label` instead.
* Specify the label for the control
*/
ariaLabel: deprecate["default"](PropTypes__default["default"].string.isRequired, 'The `ariaLabel` prop has been deprecated in favor of `aria-label`. This prop will be removed in the next major release.'),
/**
* Specify whether the underlying control is checked, or not
*/
checked: PropTypes__default["default"].bool,
/**
* Specify whether the underlying input control should be disabled
*/
disabled: PropTypes__default["default"].bool,
/**
* Provide an `id` for the underlying input control
*/
id: PropTypes__default["default"].string.isRequired,
/**
* Specify whether the control is in an indterminate state
*/
indeterminate: PropTypes__default["default"].bool,
/**
* Provide a `name` for the underlying input control
*/
name: PropTypes__default["default"].string.isRequired,
/**
* Provide an optional hook that is called each time the input is updated
*/
onChange: PropTypes__default["default"].func,
/**
* Provide a handler that is invoked when a user clicks on the control
*/
onClick: PropTypes__default["default"].func,
/**
* Provide a handler that is invoked on the key down event for the control
*/
onKeyDown: PropTypes__default["default"].func,
/**
* Provide an optional tooltip for the InlineCheckbox
*/
title: PropTypes__default["default"].string
};
var InlineCheckbox$1 = InlineCheckbox;
exports["default"] = InlineCheckbox$1;