UNPKG

@spaced-out/ui-design-system

Version:
151 lines (149 loc) 6.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chip = exports.CHIP_SEMANTIC = void 0; var React = _interopRequireWildcard(require("react")); var _classify = require("../../utils/classify"); var _qa = require("../../utils/qa"); var _Icon = require("../Icon"); var _StatusIndicator = require("../StatusIndicator"); var _Truncate = require("../Truncate"); var _ChipModule = _interopRequireDefault(require("./Chip.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const CHIP_SEMANTIC = exports.CHIP_SEMANTIC = Object.freeze({ primary: 'primary', information: 'information', success: 'success', warning: 'warning', danger: 'danger', secondary: 'secondary' }); const Chip = exports.Chip = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { classNames, semantic = 'primary', size = 'medium', children, showStatusIndicator, onClick, disabled, line = 1, wordBreak, testId, // There is no reason for hover state to be active when there is no click handler attached disableHoverState = !onClick, ...restProps } = _ref; // Type narrowing to safely access properties only when they exist const iconName = 'iconName' in restProps ? restProps.iconName : ''; const iconType = 'iconType' in restProps ? restProps.iconType : 'regular'; const dismissable = 'dismissable' in restProps ? restProps.dismissable : false; const onDismiss = 'onDismiss' in restProps ? restProps.onDismiss : () => null; /** * Note (Nishant): Why we are using a `div` to render a onclick element instead of a `button`? * * Rendering the `Chip` component as a button directly would have been ideal, as it would * have naturally handled interactivity and accessibility for clickable chips(which has an onClick). However, * the `Chip` component includes a `CloseIcon`, which itself is a button. Nesting a `<button>` * inside another `<button>` is semantically incorrect and would lead to improper HTML structure. * * Instead, we use a `<div>` with `role="button"` to maintain proper semantic behavior and * avoid nesting buttons. While `role="button"` provides the appropriate semantics, it does * not automatically handle keyboard interactions. Therefore, we manually handle `Enter` * and `Space` key events to ensure the component is fully accessible and keyboard-compliant. * * Although this method might seem less conventional, it simplifies implementation and ensures * backward compatibility while adhering to accessibility standards. */ const handleKeyDown = event => { const { key } = event; if (key === 'Enter' || key === ' ') { event.preventDefault(); // Prevent default action for Enter and Space keys onClick?.(event); } }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'root' }), ...restProps, ref: ref, className: (0, _classify.classify)(_ChipModule.default.chipWrapper, { [_ChipModule.default.primary]: semantic === CHIP_SEMANTIC.primary, [_ChipModule.default.information]: semantic === CHIP_SEMANTIC.information, [_ChipModule.default.success]: semantic === CHIP_SEMANTIC.success, [_ChipModule.default.warning]: semantic === CHIP_SEMANTIC.warning, [_ChipModule.default.danger]: semantic === CHIP_SEMANTIC.danger, [_ChipModule.default.secondary]: semantic === CHIP_SEMANTIC.secondary, [_ChipModule.default.large]: size === 'large', [_ChipModule.default.medium]: size === 'medium', [_ChipModule.default.small]: size === 'small', [_ChipModule.default.dismissable]: dismissable, [_ChipModule.default.withIcon]: !!iconName && size !== 'small', [_ChipModule.default.disabled]: disabled, [_ChipModule.default.noHoverState]: showStatusIndicator || disableHoverState || disabled }, classNames?.wrapper), onClick: onClick, onKeyDown: handleKeyDown, tabIndex: showStatusIndicator || disableHoverState || disabled ? undefined : 0, role: showStatusIndicator || disableHoverState ? undefined : 'button', children: [showStatusIndicator && size !== 'small' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_StatusIndicator.StatusIndicator, { status: semantic, classNames: { wrapper: (0, _classify.classify)(_ChipModule.default.statusIndicatorBlock, classNames?.statusIndicator) }, disabled: disabled, testId: (0, _qa.generateTestId)({ base: testId, slot: 'status' }) }), iconName && size !== 'small' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { className: (0, _classify.classify)(_ChipModule.default.chipIcon, { [_ChipModule.default.alignTop]: line > 1 }, classNames?.icon), name: iconName, type: iconType, size: "small", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, { line: line, wordBreak: wordBreak, testId: (0, _qa.generateTestId)({ base: testId, slot: 'text' }), children: children }), dismissable && size !== 'small' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.CloseIcon, { classNames: { icon: _ChipModule.default.dismissIcon, button: (0, _classify.classify)({ [_ChipModule.default.alignTop]: line > 1 }, _ChipModule.default.dismissIconWrapper) }, type: iconType, size: "small", ariaLabel: "Dismiss", disabled: disabled, testId: (0, _qa.generateTestId)({ base: testId, slot: 'dismiss' }), onClick: event => { event.stopPropagation(); if (!disabled && onDismiss) { onDismiss(event); } } })] }); }); Chip.displayName = 'Chip';