UNPKG

@carbon/react

Version:

React components for the Carbon Design System

114 lines (108 loc) 3.48 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'); 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'); 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(`${prefix}--table-column-checkbox`, { ...(className && { [className]: true }), [`${prefix}--table-column-radio`]: radio }); return /*#__PURE__*/React.createElement("td", { className: tableSelectRowClasses, "aria-live": "off" }, radio ? /*#__PURE__*/React.createElement(RadioButton.default, _rollupPluginBabelHelpers.extends({}, selectionInputProps, { labelText: labelValue, onChange: handleRadioChange, hideLabel: true })) : /*#__PURE__*/React.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.string, /** * Deprecated, please use `aria-label` instead. * Specify the aria label for the underlying input control */ ariaLabel: deprecate.deprecate(PropTypes.string, 'This prop syntax has been deprecated. Please use the new `aria-label`.'), /** * Specify whether this row is selected, or not */ checked: PropTypes.bool, /** * The CSS class names of the cell that wraps the underlying input control */ className: PropTypes.string, /** * Specify whether the control is disabled */ disabled: PropTypes.bool, /** * Provide an `id` for the underlying input control */ id: PropTypes.string.isRequired, /** * Provide a `name` for the underlying input control */ name: PropTypes.string.isRequired, /** * Provide an optional hook that is called each time the input is updated */ onChange: PropTypes.func, /** * Provide a handler to listen to when a user initiates a selection request */ onSelect: PropTypes.func.isRequired, /** * Specify whether the control should be a radio button or inline checkbox */ radio: PropTypes.bool }; exports.default = TableSelectRow;