baseui
Version:
A React Component library implementing the Base design language
146 lines (142 loc) • 6.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _overrides = require("../helpers/overrides");
var _styledComponents = require("./styled-components");
var _constants = require("./constants");
var _utils = require("./utils");
var _delete = _interopRequireDefault(require("../icon/delete"));
var _focusVisible = require("../utils/focusVisible");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// Previously, Tag used a hardcoded SVG as its 'close' icon. Replacing it with
// Delete requires modifying Delete's viewbox to prevent visual regressions.
// @ts-ignore
const ModifiedViewBoxDeleteIcon = props => /*#__PURE__*/React.createElement(_delete.default, _extends({
viewBox: "5 5 13.186 13.186"
}, props));
const Tag = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
children,
closeable = true,
color,
contentMaxWidth,
size = _constants.SIZE.small,
disabled = false,
isFocused = false,
isHovered = false,
kind = _constants.KIND.primary,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onActionClick = event => {},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onActionKeyDown = event => {},
onClick = null,
onKeyDown = null,
overrides = {},
startEnhancer,
title,
hierarchy = _constants.HIERARCHY.secondary
} = props;
const [focusVisible, setFocusVisible] = React.useState(false);
function handleFocus(event) {
if ((0, _focusVisible.isFocusVisible)(event)) {
setFocusVisible(true);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function handleBlur(event) {
if (focusVisible !== false) {
setFocusVisible(false);
}
}
function handleKeyDown(event) {
if (event.currentTarget !== event.target) {
return;
}
const key = event.key;
if (onClick && key === 'Enter') {
onClick(event);
}
if (closeable && (key === 'Backspace' || key === 'Delete')) {
onActionClick(event);
onActionKeyDown(event);
}
if (onKeyDown) {
onKeyDown(event);
}
}
const [Root, rootProps] = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.Root);
const [Action, actionProps] = (0, _overrides.getOverrides)(overrides.Action, _styledComponents.Action);
const [ActionIcon, actionIconProps] = (0, _overrides.getOverrides)(overrides.ActionIcon, ModifiedViewBoxDeleteIcon);
const [StartEnhancerContainer, startEnhancerContainerProps] = (0, _overrides.getOverrides)(overrides.StartEnhancerContainer, _styledComponents.StartEnhancerContainer);
const [Text, textProps] = (0, _overrides.getOverrides)(overrides.Text, _styledComponents.Text);
const clickable = typeof onClick === 'function';
const rootHandlers = disabled ? {} : {
onClick: onClick,
onKeyDown: handleKeyDown
};
const actionHandlers = disabled ? {} : {
// @ts-ignore
onClick: event => {
// we don't want onClick to be called when the close icon is clicked
event.stopPropagation();
onActionClick(event);
}
};
const sharedProps = {
$clickable: clickable,
$closeable: closeable,
$color: color,
$disabled: disabled,
$isFocused: isFocused,
$isHovered: isHovered,
$kind: kind,
$hierarchy: hierarchy,
$isFocusVisible: focusVisible,
$size: size
};
const titleText = title || (0, _utils.getTextFromChildren)(children);
const isButton = (clickable || closeable) && !disabled;
const actionSize = {
[_constants.SIZE.small]: 12,
[_constants.SIZE.medium]: 16,
[_constants.SIZE.large]: 20
}[size];
// Capitalize for JSX
const StartEnhancer = startEnhancer;
return /*#__PURE__*/React.createElement(Root
// eslint-disable-next-line @typescript-eslint/no-explicit-any
, _extends({
ref: ref,
"data-baseweb": "tag",
"aria-label": isButton && closeable ? `${typeof children === 'string' ? `${children}, ` : ''}close by backspace` : null,
"aria-disabled": disabled ? true : null,
role: isButton ? 'button' : null,
tabIndex: isButton ? 0 : null
}, rootHandlers, sharedProps, rootProps, {
onFocus: (0, _focusVisible.forkFocus)(rootProps, handleFocus),
onBlur: (0, _focusVisible.forkBlur)(rootProps, handleBlur),
$contentMaxWidth: contentMaxWidth
}), StartEnhancer &&
// @ts-expect-error todo(flow->ts) it is not expected to be a number
StartEnhancer !== 0 && /*#__PURE__*/React.createElement(StartEnhancerContainer, startEnhancerContainerProps, /*#__PURE__*/React.createElement(StartEnhancer, null)), /*#__PURE__*/React.createElement(Text, _extends({
$contentMaxWidth: contentMaxWidth,
title: titleText
}, textProps), children), closeable ? /*#__PURE__*/React.createElement(Action, _extends({
"aria-hidden": true,
role: "presentation"
}, actionHandlers, sharedProps, actionProps), /*#__PURE__*/React.createElement(ActionIcon, _extends({
size: actionSize
}, actionIconProps))) : null);
});
Tag.displayName = 'Tag';
var _default = exports.default = Tag;