UNPKG

2.66 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import * as React from 'react';
3import classNames from 'classnames';
4import KeyCode from "rc-util/es/KeyCode";
5
6function TabNode(_ref, ref) {
7 var _classNames;
8
9 var prefixCls = _ref.prefixCls,
10 id = _ref.id,
11 active = _ref.active,
12 _ref$tab = _ref.tab,
13 key = _ref$tab.key,
14 tab = _ref$tab.tab,
15 disabled = _ref$tab.disabled,
16 closeIcon = _ref$tab.closeIcon,
17 closable = _ref.closable,
18 renderWrapper = _ref.renderWrapper,
19 removeAriaLabel = _ref.removeAriaLabel,
20 editable = _ref.editable,
21 onClick = _ref.onClick,
22 onRemove = _ref.onRemove,
23 onFocus = _ref.onFocus,
24 style = _ref.style;
25 var tabPrefix = "".concat(prefixCls, "-tab");
26 React.useEffect(function () {
27 return onRemove;
28 }, []);
29 var removable = editable && closable !== false && !disabled;
30
31 function onInternalClick(e) {
32 if (disabled) {
33 return;
34 }
35
36 onClick(e);
37 }
38
39 function onRemoveTab(event) {
40 event.preventDefault();
41 event.stopPropagation();
42 editable.onEdit('remove', {
43 key: key,
44 event: event
45 });
46 }
47
48 var node = /*#__PURE__*/React.createElement("div", {
49 key: key,
50 ref: ref,
51 className: classNames(tabPrefix, (_classNames = {}, _defineProperty(_classNames, "".concat(tabPrefix, "-with-remove"), removable), _defineProperty(_classNames, "".concat(tabPrefix, "-active"), active), _defineProperty(_classNames, "".concat(tabPrefix, "-disabled"), disabled), _classNames)),
52 style: style,
53 onClick: onInternalClick
54 }, /*#__PURE__*/React.createElement("div", {
55 role: "tab",
56 "aria-selected": active,
57 id: id && "".concat(id, "-tab-").concat(key),
58 className: "".concat(tabPrefix, "-btn"),
59 "aria-controls": id && "".concat(id, "-panel-").concat(key),
60 "aria-disabled": disabled,
61 tabIndex: disabled ? null : 0,
62 onClick: function onClick(e) {
63 e.stopPropagation();
64 onInternalClick(e);
65 },
66 onKeyDown: function onKeyDown(e) {
67 if ([KeyCode.SPACE, KeyCode.ENTER].includes(e.which)) {
68 e.preventDefault();
69 onInternalClick(e);
70 }
71 },
72 onFocus: onFocus
73 }, tab), removable && /*#__PURE__*/React.createElement("button", {
74 type: "button",
75 "aria-label": removeAriaLabel || 'remove',
76 tabIndex: 0,
77 className: "".concat(tabPrefix, "-remove"),
78 onClick: function onClick(e) {
79 e.stopPropagation();
80 onRemoveTab(e);
81 }
82 }, closeIcon || editable.removeIcon || '×'));
83 return renderWrapper ? renderWrapper(node) : node;
84}
85
86export default /*#__PURE__*/React.forwardRef(TabNode);
\No newline at end of file