@spaced-out/ui-design-system
Version:
Sense UI components library
126 lines (125 loc) • 5.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Chip = exports.CHIP_SEMANTIC = void 0;
var React = _interopRequireWildcard(require("react"));
var _classify = require("../../utils/classify");
var _Icon = require("../Icon");
var _StatusIndicator = require("../StatusIndicator");
var _Truncate = require("../Truncate");
var _ChipModule = _interopRequireDefault(require("./Chip.module.css"));
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); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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,
iconName = '',
iconType = 'regular',
showStatusIndicator,
dismissable = false,
onDismiss = () => null,
onClick,
disabled,
line = 1,
wordBreak,
disableHoverState = !onClick,
// There is no reason for hover state to be active when there is no click handler attached
...restProps
} = _ref;
/**
* 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__*/React.createElement("div", _extends({
"data-testid": "Chip"
}, 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'
}), showStatusIndicator && size !== 'small' && /*#__PURE__*/React.createElement(_StatusIndicator.StatusIndicator, {
status: semantic,
classNames: {
wrapper: (0, _classify.classify)(_ChipModule.default.statusIndicatorBlock, classNames?.statusIndicator)
},
disabled: disabled
}), iconName && size !== 'small' && /*#__PURE__*/React.createElement(_Icon.Icon, {
className: (0, _classify.classify)(_ChipModule.default.chipIcon, {
[_ChipModule.default.alignTop]: line > 1
}, classNames?.icon),
name: iconName,
type: iconType,
size: "small"
}), /*#__PURE__*/React.createElement(_Truncate.Truncate, {
line: line,
wordBreak: wordBreak
}, children), dismissable && size !== 'small' && /*#__PURE__*/React.createElement(_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,
onClick: event => {
event.stopPropagation();
if (!disabled && onDismiss) {
onDismiss(event);
}
}
}));
});
Chip.displayName = 'Chip';