@carbon/react
Version:
React components for the Carbon Design System
120 lines (112 loc) • 4.08 kB
JavaScript
/**
* 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.
*/
;
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');
var InlineCheckbox = require('../InlineCheckbox/InlineCheckbox.js');
var RadioButton = require('../RadioButton/RadioButton.js');
var useId = require('../../internal/useId.js');
var usePrefix = require('../../internal/usePrefix.js');
var deprecate = require('../../prop-types/deprecate.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 TableSelectRow = ({
ariaLabel: deprecatedAriaLabel,
['aria-label']: ariaLabel,
checked,
id,
name,
onSelect,
onChange,
disabled,
radio,
className
}) => {
const prefix = usePrefix.usePrefix();
const uniqueNameId = useId.useId();
const handleRadioChange = onChange ? (value, name, event) => {
// Convert the radio value to boolean for consistency
onChange(!!value, name || '', event);
} : undefined;
const handleCheckboxChange = onChange ? (checked, name, event) => {
onChange(checked, name, event);
} : undefined;
const selectionInputProps = {
id,
name: name ? name : uniqueNameId,
onClick: onSelect,
checked,
disabled
};
const labelValue = ariaLabel || deprecatedAriaLabel || '';
const tableSelectRowClasses = cx__default["default"](`${prefix}--table-column-checkbox`, {
...(className && {
[className]: true
}),
[`${prefix}--table-column-radio`]: radio
});
return /*#__PURE__*/React__default["default"].createElement("td", {
className: tableSelectRowClasses,
"aria-live": "off"
}, radio ? /*#__PURE__*/React__default["default"].createElement(RadioButton["default"], _rollupPluginBabelHelpers["extends"]({}, selectionInputProps, {
labelText: labelValue,
onChange: handleRadioChange,
hideLabel: true
})) : /*#__PURE__*/React__default["default"].createElement(InlineCheckbox["default"], _rollupPluginBabelHelpers["extends"]({}, selectionInputProps, {
"aria-label": labelValue,
onChange: handleCheckboxChange
})));
};
TableSelectRow.propTypes = {
/**
* Specify the aria label for the underlying input control
*/
['aria-label']: PropTypes__default["default"].string,
/**
* Deprecated, please use `aria-label` instead.
* Specify the aria label for the underlying input control
*/
ariaLabel: deprecate["default"](PropTypes__default["default"].string, 'This prop syntax has been deprecated. Please use the new `aria-label`.'),
/**
* Specify whether this row is selected, or not
*/
checked: PropTypes__default["default"].bool,
/**
* The CSS class names of the cell that wraps the underlying input control
*/
className: PropTypes__default["default"].string,
/**
* Specify whether the control is disabled
*/
disabled: PropTypes__default["default"].bool,
/**
* Provide an `id` for the underlying input control
*/
id: PropTypes__default["default"].string.isRequired,
/**
* 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 to listen to when a user initiates a selection request
*/
onSelect: PropTypes__default["default"].func.isRequired,
/**
* Specify whether the control should be a radio button or inline checkbox
*/
radio: PropTypes__default["default"].bool
};
exports["default"] = TableSelectRow;