TabNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import classNames from 'classnames';
  3. import * as React from 'react';
  4. import { genDataNodeKey, getRemovable } from "../util";
  5. var TabNode = function TabNode(props) {
  6. var prefixCls = props.prefixCls,
  7. id = props.id,
  8. active = props.active,
  9. focus = props.focus,
  10. _props$tab = props.tab,
  11. key = _props$tab.key,
  12. label = _props$tab.label,
  13. disabled = _props$tab.disabled,
  14. closeIcon = _props$tab.closeIcon,
  15. icon = _props$tab.icon,
  16. closable = props.closable,
  17. renderWrapper = props.renderWrapper,
  18. removeAriaLabel = props.removeAriaLabel,
  19. editable = props.editable,
  20. onClick = props.onClick,
  21. onFocus = props.onFocus,
  22. onBlur = props.onBlur,
  23. onKeyDown = props.onKeyDown,
  24. onMouseDown = props.onMouseDown,
  25. onMouseUp = props.onMouseUp,
  26. style = props.style,
  27. tabCount = props.tabCount,
  28. currentPosition = props.currentPosition;
  29. var tabPrefix = "".concat(prefixCls, "-tab");
  30. var removable = getRemovable(closable, closeIcon, editable, disabled);
  31. function onInternalClick(e) {
  32. if (disabled) {
  33. return;
  34. }
  35. onClick(e);
  36. }
  37. function onRemoveTab(event) {
  38. event.preventDefault();
  39. event.stopPropagation();
  40. editable.onEdit('remove', {
  41. key: key,
  42. event: event
  43. });
  44. }
  45. var labelNode = React.useMemo(function () {
  46. return icon && typeof label === 'string' ? /*#__PURE__*/React.createElement("span", null, label) : label;
  47. }, [label, icon]);
  48. var btnRef = React.useRef(null);
  49. React.useEffect(function () {
  50. if (focus && btnRef.current) {
  51. btnRef.current.focus();
  52. }
  53. }, [focus]);
  54. var node = /*#__PURE__*/React.createElement("div", {
  55. key: key,
  56. "data-node-key": genDataNodeKey(key),
  57. className: classNames(tabPrefix, _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(tabPrefix, "-with-remove"), removable), "".concat(tabPrefix, "-active"), active), "".concat(tabPrefix, "-disabled"), disabled), "".concat(tabPrefix, "-focus"), focus)),
  58. style: style,
  59. onClick: onInternalClick
  60. }, /*#__PURE__*/React.createElement("div", {
  61. ref: btnRef,
  62. role: "tab",
  63. "aria-selected": active,
  64. id: id && "".concat(id, "-tab-").concat(key),
  65. className: "".concat(tabPrefix, "-btn"),
  66. "aria-controls": id && "".concat(id, "-panel-").concat(key),
  67. "aria-disabled": disabled,
  68. tabIndex: disabled ? null : active ? 0 : -1,
  69. onClick: function onClick(e) {
  70. e.stopPropagation();
  71. onInternalClick(e);
  72. },
  73. onKeyDown: onKeyDown,
  74. onMouseDown: onMouseDown,
  75. onMouseUp: onMouseUp,
  76. onFocus: onFocus,
  77. onBlur: onBlur
  78. }, focus && /*#__PURE__*/React.createElement("div", {
  79. "aria-live": "polite",
  80. style: {
  81. width: 0,
  82. height: 0,
  83. position: 'absolute',
  84. overflow: 'hidden',
  85. opacity: 0
  86. }
  87. }, "Tab ".concat(currentPosition, " of ").concat(tabCount)), icon && /*#__PURE__*/React.createElement("span", {
  88. className: "".concat(tabPrefix, "-icon")
  89. }, icon), label && labelNode), removable && /*#__PURE__*/React.createElement("button", {
  90. type: "button",
  91. role: "tab",
  92. "aria-label": removeAriaLabel || 'remove',
  93. tabIndex: active ? 0 : -1,
  94. className: "".concat(tabPrefix, "-remove"),
  95. onClick: function onClick(e) {
  96. e.stopPropagation();
  97. onRemoveTab(e);
  98. }
  99. }, closeIcon || editable.removeIcon || '×'));
  100. return renderWrapper ? renderWrapper(node) : node;
  101. };
  102. export default TabNode;