@befe/brick-comp-tabs 0.2.126 → 0.2.128

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/tabs.js CHANGED
@@ -1,4 +1,3 @@
1
- import _Reflect$construct from "@babel/runtime-corejs3/core-js-stable/reflect/construct";
2
1
  import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
3
2
  import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
4
3
  import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
@@ -6,626 +5,664 @@ import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/inst
6
5
  import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
7
6
  import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
8
7
  import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
8
+ import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
9
9
  import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/objectWithoutProperties";
10
10
  import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray";
11
- import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
12
- import _createClass from "@babel/runtime-corejs3/helpers/createClass";
13
- import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/possibleConstructorReturn";
14
- import _getPrototypeOf from "@babel/runtime-corejs3/helpers/getPrototypeOf";
15
- import _inherits from "@babel/runtime-corejs3/helpers/inherits";
16
11
  import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
17
12
  var _excluded = ["content"];
18
13
  function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = _filterInstanceProperty(o).call(o, function (r) { return _Object$getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
19
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context12, _context13; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context12 = ownKeys(Object(t), !0)).call(_context12, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context13 = ownKeys(Object(t))).call(_context13, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
20
- import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
14
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context0, _context1; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context0 = ownKeys(Object(t), !0)).call(_context0, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context1 = ownKeys(Object(t))).call(_context1, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
21
15
  import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
22
16
  import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
23
17
  import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
24
18
  import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
25
- import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some";
19
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
26
20
  import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
27
21
  import _findIndexInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find-index";
28
22
  import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
29
- function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? _Reflect$construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
30
- function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
31
- import { Component, createRef, Children, isValidElement } from 'react';
32
- // import * as PropTypes from 'prop-types'
33
- import c from 'classnames';
34
- import { addEventListener, ifNodeContains, pick, pickDerivedStateFromProps, safeInvoke } from '@befe/brick-utils';
35
- import { isUndefined } from 'lodash-es';
36
- import { ConfigContext, getDefaultValueUsingContextTheme } from '@befe/brick-core';
23
+ import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some";
24
+ import { Children, isValidElement, useState, useRef, useEffect } from 'react';
25
+ import { cx, addEventListener, ifNodeContains, pick, safeInvoke, isUndefined } from '@befe/brick-utils';
26
+ import { useTheme } from '@befe/brick-core';
37
27
  import { Icon } from '@befe/brick-comp-icon';
38
28
  import { SvgPlainDown, SvgPlainUp, SvgMarkPlus, SvgSignExclamation } from '@befe/brick-icon';
39
29
  import { Menu, MenuItem, MenuPopper } from '@befe/brick-comp-menu';
40
30
  import { RadioGroup } from '@befe/brick-comp-radio';
41
31
  import { Button } from '@befe/brick-comp-button';
42
32
  import { CloseX } from '@befe/brick-comp-close-x';
43
- import { TabPane } from './tab-pane';
33
+ import { TabPane, TAB_PANE_DEFAULT_STATUS } from './tab-pane';
44
34
  import { TabsContext } from './tabs-context';
45
- import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
35
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
46
36
  var BORDER_WIDTH = 1;
47
37
  var SIZED_TYPES = ['plain', 'tag'];
48
- export var Tabs = /*#__PURE__*/function (_Component) {
49
- function Tabs() {
50
- var _context;
51
- var _this;
52
- _classCallCheck(this, Tabs);
53
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
54
- args[_key] = arguments[_key];
55
- }
56
- _this = _callSuper(this, Tabs, _concatInstanceProperty(_context = []).call(_context, args));
57
- _defineProperty(_this, "context", void 0);
58
- _defineProperty(_this, "windowResizeHandler", void 0);
59
- _defineProperty(_this, "tabsWheelHandler", null);
60
- _defineProperty(_this, "state", {
61
- activeId: (isUndefined(_this.props.activeId) ? _this.props.defaultActiveId : _this.props.activeId) || null,
62
- visibleFrom: 0,
63
- visibleTo: 0,
64
- isEllipsisPopperVisible: false
65
- });
66
- _defineProperty(_this, "refTabsWrap", /*#__PURE__*/createRef());
67
- _defineProperty(_this, "refTabs", /*#__PURE__*/createRef());
68
- _defineProperty(_this, "tabsWrapWidth", 0);
69
- _defineProperty(_this, "tabsWidth", []);
70
- _defineProperty(_this, "tabsWrapMaxScrollLeft", 0);
71
- _defineProperty(_this, "tabsWrapScrollLeft", 0);
72
- _defineProperty(_this, "handleClickAdd", function (e) {
73
- var activeId = safeInvoke(_this.props.onClickAdd, e);
74
- activeId && _this.changeActive(activeId);
75
- });
76
- _defineProperty(_this, "handleClickTab", function (e, id) {
77
- var $removeBtn = e.currentTarget.querySelector('.brick-tabs-tab-delete');
78
- if ($removeBtn && ifNodeContains($removeBtn, e.target)) {
79
- _this.deleteTab(id);
80
- } else {
81
- _this.changeActive(id);
82
- }
83
- if (!_this.isTabPaneDisabled(id)) {
84
- safeInvoke(_this.props.onClickTab, e, id);
85
- }
86
- });
87
- _defineProperty(_this, "handleChangeRadioGroup", function (value) {
88
- value && _this.changeActive(value);
89
- });
90
- _defineProperty(_this, "handleMenuPopperChange", function (status) {
91
- _this.setState({
92
- isEllipsisPopperVisible: status
38
+ // 初始(非受控)activeId:受控值优先,其余回落 defaultActiveId,最终兜底 null
39
+ function getInitialActiveId(props) {
40
+ return (isUndefined(props.activeId) ? props.defaultActiveId : props.activeId) || null;
41
+ }
42
+
43
+ // 仅 plain / tag 受主题 baseSize 影响,其余固定 md
44
+ function resolveSize(props, type, baseSize) {
45
+ var _props$size;
46
+ if (!_includesInstanceProperty(SIZED_TYPES).call(SIZED_TYPES, type)) {
47
+ return 'md';
48
+ }
49
+ return (_props$size = props.size) !== null && _props$size !== void 0 ? _props$size : baseSize;
50
+ }
51
+ function getValidChildren(children) {
52
+ var _context;
53
+ if (!children) {
54
+ return null;
55
+ }
56
+ var validChildren = _filterInstanceProperty(_context = Children.toArray(children)).call(_context, function (child) {
57
+ return /*#__PURE__*/isValidElement(child) && child.type === TabPane;
58
+ });
59
+ return validChildren.length ? validChildren : null;
60
+ }
61
+
62
+ // resize / wheel 长订阅回调容器(latest-ref)
63
+
64
+ // 全部的 tabs(提供给 tabs-container-shadow / isTabPaneDisabled 判断用)
65
+ function getTabsAll(panes, validChildren) {
66
+ if (validChildren) {
67
+ return _mapInstanceProperty(validChildren).call(validChildren, function (child) {
68
+ var _child$props$status;
69
+ return _objectSpread(_objectSpread({}, pick(child.props, ['id', 'label', 'disabled', 'deletable'])), {}, {
70
+ // TabPane 不再挂 defaultProps(React18 废弃函数组件 defaultProps):
71
+ // 默认参数不进 element.props,故父组件读取时用 TabPane 导出的默认常量兜底
72
+ status: (_child$props$status = child.props.status) !== null && _child$props$status !== void 0 ? _child$props$status : TAB_PANE_DEFAULT_STATUS
93
73
  });
94
74
  });
95
- _defineProperty(_this, "handleWheelTabsWrap", function (e) {
96
- if (!_this.withEllipsis) {
97
- return;
75
+ }
76
+ return panes || [];
77
+ }
78
+
79
+ // 收到下拉里的 tabs
80
+ function getTabsEllipsis(tabsAll, visibleFrom, visibleTo) {
81
+ var _context2;
82
+ var from = visibleFrom || 0;
83
+ var to = visibleTo || tabsAll.length - 1;
84
+ var prevTabs = _sliceInstanceProperty(tabsAll).call(tabsAll, 0, from);
85
+ var afterTabs = _sliceInstanceProperty(tabsAll).call(tabsAll, to + 1, tabsAll.length);
86
+ return _concatInstanceProperty(_context2 = []).call(_context2, _toConsumableArray(prevTabs), _toConsumableArray(afterTabs));
87
+ }
88
+
89
+ // 删除按钮:结果可能作为 MenuItem 的 suffix 透传,null 与 element 对下游 DOM 不同,
90
+ // 故保持「纯函数返回 ReactNode(null / element)」而非子组件
91
+ function renderTabDelete(type, deletable) {
92
+ // 仅 type card 支持添加、删除
93
+ if (type !== 'card' || !deletable) {
94
+ return null;
95
+ }
96
+
97
+ // 点击由 handleClickTab 代理
98
+ return /*#__PURE__*/_jsx(CloseX, {
99
+ className: 'brick-tabs-tab-delete',
100
+ size: 'sm'
101
+ });
102
+ }
103
+
104
+ // 渲染上下文:把派生值 + 事件回调收拢,透传给外提的渲染子组件(子组件不产生额外 host DOM,快照不变)
105
+
106
+ function TabItemView(_ref) {
107
+ var tab = _ref.tab,
108
+ ctx = _ref.ctx;
109
+ var type = ctx.type,
110
+ activeId = ctx.activeId,
111
+ onClickTab = ctx.onClickTab,
112
+ onChangeActive = ctx.onChangeActive,
113
+ onFocusTab = ctx.onFocusTab;
114
+ var tabClass = cx('brick-tabs-tab', _defineProperty({
115
+ 'brick-tabs-tab-active': activeId === tab.id,
116
+ 'brick-tabs-tab-disabled': tab.disabled,
117
+ 'brick-tabs-tab-deletable': tab.deletable
118
+ }, "brick-tabs-tab-status-".concat(tab.status), tab.status));
119
+ return /*#__PURE__*/_jsxs("li", {
120
+ className: tabClass,
121
+ onClick: function onClick(e) {
122
+ return onClickTab(e, tab.id);
123
+ },
124
+ tabIndex: tab.disabled ? -1 : 0,
125
+ onFocus: function onFocus() {
126
+ return onFocusTab(tab.id);
127
+ },
128
+ onKeyDown: function onKeyDown(e) {
129
+ if (e.key === 'Enter' || e.key === ' ') {
130
+ onChangeActive(tab.id);
131
+ // 阻止 "空格" 触发的滚动
132
+ e.preventDefault();
98
133
  }
99
- var deltaX = e.deltaX;
100
- var deltaY = e.deltaY;
101
- var scrollLeft = Math.abs(deltaX) > Math.abs(deltaY) ? deltaX : deltaY;
102
- _this.scrollTabsWrap(_this.tabsWrapScrollLeft + scrollLeft);
103
- e.preventDefault();
104
- });
105
- _defineProperty(_this, "renderTab", function (tab) {
106
- var tabClass = c('brick-tabs-tab', _defineProperty({
107
- 'brick-tabs-tab-active': _this.state.activeId === tab.id,
108
- 'brick-tabs-tab-disabled': tab.disabled,
109
- 'brick-tabs-tab-deletable': tab.deletable
110
- }, "brick-tabs-tab-status-".concat(tab.status), tab.status));
111
- var tabProps = {
112
- className: tabClass,
134
+ },
135
+ children: [type === 'card' && !tab.disabled && tab.status === 'error' && /*#__PURE__*/_jsx(Icon, {
136
+ className: 'brick-tabs-tab-status-icon',
137
+ svg: SvgSignExclamation
138
+ }), tab.label, renderTabDelete(type, tab.deletable)]
139
+ });
140
+ }
141
+ function TabList(_ref2) {
142
+ var _context3;
143
+ var ctx = _ref2.ctx;
144
+ return /*#__PURE__*/_jsx("div", {
145
+ className: 'brick-tabs-wrap',
146
+ ref: ctx.refTabsWrap,
147
+ children: /*#__PURE__*/_jsx("ul", {
148
+ ref: ctx.refTabs,
149
+ children: _mapInstanceProperty(_context3 = ctx.tabsAll).call(_context3, function (tab) {
150
+ return /*#__PURE__*/_jsx(TabItemView, {
151
+ tab: tab,
152
+ ctx: ctx
153
+ }, String(tab.id));
154
+ })
155
+ })
156
+ });
157
+ }
158
+ function EllipsisMenu(_ref3) {
159
+ var ctx = _ref3.ctx;
160
+ var type = ctx.type,
161
+ tabsEllipsis = ctx.tabsEllipsis,
162
+ onClickTab = ctx.onClickTab;
163
+ // 只有 type card 支持状态
164
+ var isTypeCard = type === 'card';
165
+ return /*#__PURE__*/_jsx(Menu, {
166
+ type: 'common',
167
+ children: _mapInstanceProperty(tabsEllipsis).call(tabsEllipsis, function (tab) {
168
+ var className = cx('brick-tabs-ellipsis-item', isTypeCard && tab.status && "brick-tabs-ellipsis-item-status-".concat(tab.status));
169
+ return /*#__PURE__*/_jsx(MenuItem, {
170
+ className: className,
113
171
  onClick: function onClick(e) {
114
- _this.handleClickTab(e, tab.id);
115
- },
116
- tabIndex: tab.disabled ? -1 : 0,
117
- onFocus: function onFocus() {
118
- _this.scrollTabsWrapToTab(tab.id);
172
+ return onClickTab(e, tab.id);
119
173
  },
120
- onKeyDown: function onKeyDown(e) {
121
- if (e.key === 'Enter' || e.key === ' ') {
122
- _this.changeActive(tab.id);
123
- // 阻止 "空格" 触发的滚动
124
- e.preventDefault();
125
- }
126
- }
127
- };
128
- return /*#__PURE__*/_jsxs("li", _objectSpread(_objectSpread({}, tabProps), {}, {
129
- children: [_this.renderTabStatus(tab), tab.label, _this.renderTabDelete(tab)]
130
- }), String(tab.id));
174
+ suffix: renderTabDelete(type, tab.deletable),
175
+ disabled: tab.disabled,
176
+ children: tab.label
177
+ }, String(tab.id));
178
+ })
179
+ });
180
+ }
181
+ function EllipsisDropdown(_ref4) {
182
+ var ctx = _ref4.ctx;
183
+ var tabsEllipsis = ctx.tabsEllipsis,
184
+ size = ctx.size,
185
+ isEllipsisPopperVisible = ctx.isEllipsisPopperVisible,
186
+ ellipsisDropdownPlacement = ctx.ellipsisDropdownPlacement,
187
+ triggerType = ctx.triggerType,
188
+ onMenuPopperChange = ctx.onMenuPopperChange;
189
+ if (!tabsEllipsis.length) {
190
+ return null;
191
+ }
192
+ var targetNode = /*#__PURE__*/_jsx("div", {
193
+ className: 'brick-tabs-ellipsis',
194
+ children: /*#__PURE__*/_jsx(Icon, {
195
+ svg: isEllipsisPopperVisible ? SvgPlainUp : SvgPlainDown
196
+ })
197
+ });
198
+ return /*#__PURE__*/_jsx(MenuPopper, {
199
+ className: 'brick-tabs-ellipsis-dropdown-popper',
200
+ size: size,
201
+ targetNode: targetNode,
202
+ placement: ellipsisDropdownPlacement,
203
+ type: triggerType,
204
+ onChange: onMenuPopperChange,
205
+ children: /*#__PURE__*/_jsx(EllipsisMenu, {
206
+ ctx: ctx
207
+ })
208
+ });
209
+ }
210
+ function AddButton(_ref5) {
211
+ var ctx = _ref5.ctx;
212
+ var type = ctx.type,
213
+ hasOnClickAdd = ctx.hasOnClickAdd,
214
+ onClickAdd = ctx.onClickAdd;
215
+
216
+ // 仅 type card 支持添加、删除
217
+ if (type !== 'card' || !hasOnClickAdd) {
218
+ return null;
219
+ }
220
+ return /*#__PURE__*/_jsx("div", {
221
+ className: 'brick-tabs-add',
222
+ children: /*#__PURE__*/_jsx(Button, {
223
+ className: 'brick-tabs-add-button',
224
+ onClick: onClickAdd,
225
+ type: 'plain',
226
+ color: 'brand',
227
+ size: 'md',
228
+ icon: SvgMarkPlus
229
+ })
230
+ });
231
+ }
232
+ function ButtonGroupTabs(_ref6) {
233
+ var ctx = _ref6.ctx;
234
+ var type = ctx.type,
235
+ size = ctx.size,
236
+ activeId = ctx.activeId,
237
+ tabsAll = ctx.tabsAll,
238
+ onChangeRadioGroup = ctx.onChangeRadioGroup;
239
+ var value = activeId;
240
+ var options = _mapInstanceProperty(tabsAll).call(tabsAll, function (child) {
241
+ var id = child.id,
242
+ label = child.label,
243
+ disabled = child.disabled;
244
+ return {
245
+ value: id,
246
+ label: label,
247
+ disabled: disabled
248
+ };
249
+ });
250
+ return /*#__PURE__*/_jsx(RadioGroup, {
251
+ className: 'brick-tabs-group',
252
+ type: type === 'button-group-important' ? 'important' : 'intensive',
253
+ size: size,
254
+ value: value,
255
+ options: options,
256
+ onChange: onChangeRadioGroup
257
+ });
258
+ }
259
+ function TabsContainerView(_ref7) {
260
+ var _context4;
261
+ var ctx = _ref7.ctx;
262
+ if (_includesInstanceProperty(_context4 = ctx.type).call(_context4, 'button-group')) {
263
+ return /*#__PURE__*/_jsx("div", {
264
+ className: 'brick-tabs-container',
265
+ children: /*#__PURE__*/_jsx(ButtonGroupTabs, {
266
+ ctx: ctx
267
+ })
131
268
  });
132
- return _this;
133
269
  }
134
- _inherits(Tabs, _Component);
135
- return _createClass(Tabs, [{
136
- key: "className",
137
- get: function get() {
138
- var _ref = this.props,
139
- className = _ref.className,
140
- type = _ref.type;
141
- var size = this.size;
142
- return c('brick-tabs', "brick-tabs-type-".concat(type), "brick-tabs-size-".concat(size), {
143
- 'brick-tabs-with-ellipsis': this.tabsEllipsis.length
144
- }, className);
145
- }
146
- }, {
147
- key: "size",
148
- get: function get() {
149
- if (!_includesInstanceProperty(SIZED_TYPES).call(SIZED_TYPES, this.props.type)) {
150
- return 'md';
151
- }
152
- return getDefaultValueUsingContextTheme(this, 'size', 'baseSize');
153
- }
154
- }, {
155
- key: "contextValue",
156
- get: function get() {
157
- return {
158
- activeId: this.state.activeId
159
- };
160
- }
161
- }, {
162
- key: "validChildren",
163
- get: function get() {
164
- var _context2;
165
- var children = this.props.children;
166
- if (!children) {
167
- return null;
168
- }
169
- var validChildren = _filterInstanceProperty(_context2 = Children.toArray(children)).call(_context2, function (child) {
170
- return /*#__PURE__*/isValidElement(child) && child.type === TabPane;
270
+ return /*#__PURE__*/_jsxs("div", {
271
+ className: 'brick-tabs-container',
272
+ children: [/*#__PURE__*/_jsx(TabList, {
273
+ ctx: ctx
274
+ }), /*#__PURE__*/_jsx(EllipsisDropdown, {
275
+ ctx: ctx
276
+ }), /*#__PURE__*/_jsx(AddButton, {
277
+ ctx: ctx
278
+ })]
279
+ });
280
+ }
281
+ function getRenderedPanes(ctx) {
282
+ var panes = ctx.panes,
283
+ validChildren = ctx.validChildren,
284
+ shouldDestroyInactivePane = ctx.shouldDestroyInactivePane,
285
+ activeId = ctx.activeId;
286
+ if (validChildren) {
287
+ if (shouldDestroyInactivePane) {
288
+ return _filterInstanceProperty(validChildren).call(validChildren, function (tab) {
289
+ return activeId === tab.props.id;
171
290
  });
172
- return validChildren.length ? validChildren : null;
173
291
  }
292
+ return validChildren;
293
+ }
294
+ if (!panes || !panes.length) {
295
+ return null;
296
+ }
297
+ var panesResult = panes;
298
+ if (shouldDestroyInactivePane) {
299
+ panesResult = _filterInstanceProperty(panes).call(panes, function (tab) {
300
+ return activeId === tab.id;
301
+ });
302
+ }
303
+ return _mapInstanceProperty(panesResult).call(panesResult, function (tab) {
304
+ var content = tab.content,
305
+ tabProps = _objectWithoutProperties(tab, _excluded);
306
+ return /*#__PURE__*/_jsx(TabPane, _objectSpread(_objectSpread({}, tabProps), {}, {
307
+ children: content
308
+ }), String(tab.id));
309
+ });
310
+ }
311
+ function TabPanesContainerView(_ref8) {
312
+ var ctx = _ref8.ctx;
313
+ var panes = getRenderedPanes(ctx);
314
+ return panes ? /*#__PURE__*/_jsx("div", {
315
+ className: 'brick-tabs-panes-container',
316
+ children: panes
317
+ }) : null;
318
+ }
319
+ // tabs 省略号测量 + 横向滚动子系统:持有 DOM ref 与测量缓存、管理 resize / wheel 长订阅,
320
+ // 把这组命令式逻辑从组件体里抽出,保持 Tabs 渲染部分精简
321
+ function useTabsScroll(params) {
322
+ var tabsAll = params.tabsAll,
323
+ type = params.type,
324
+ canEllipsis = params.canEllipsis,
325
+ withEllipsis = params.withEllipsis,
326
+ activeId = params.activeId,
327
+ visibleFrom = params.visibleFrom,
328
+ visibleTo = params.visibleTo,
329
+ setVisibleRange = params.setVisibleRange;
330
+ var refTabsWrap = useRef(null);
331
+ var refTabs = useRef(null);
332
+ var tabsWrapWidthRef = useRef(0);
333
+ var tabsWidthRef = useRef([]);
334
+ var tabsWrapMaxScrollLeftRef = useRef(0);
335
+ var tabsWrapScrollLeftRef = useRef(0);
336
+ var mountedRef = useRef(false);
337
+ var prevActiveIdRef = useRef(activeId);
338
+ /* istanbul ignore next: 初始 noop 回调在挂载后被 latestRef effect 立即覆盖,二者均不可达 */
339
+ var latestRef = useRef({
340
+ onResize: function onResize() {/* noop until mounted */},
341
+ onWheel: function onWheel() {/* noop until mounted */}
342
+ });
174
343
 
175
- // 全部的 tabs, 主要提供给
176
- // - tabs-container-shadow
177
- // - isTabPaneDisabled 判断用
178
- }, {
179
- key: "tabsAll",
180
- get: function get() {
181
- var panes = this.props.panes;
182
- var validChildren = this.validChildren;
183
- if (validChildren) {
184
- return _mapInstanceProperty(validChildren).call(validChildren, function (child) {
185
- return pick(child.props, ['id', 'label', 'disabled', 'deletable', 'status']);
186
- });
187
- }
188
- return panes || [];
189
- }
344
+ // __HOOK_METHODS__
190
345
 
191
- // 收到下拉里的 tabs
192
- }, {
193
- key: "tabsEllipsis",
194
- get: function get() {
195
- var _context3;
196
- var tabs = this.tabsAll;
197
- var from = this.state.visibleFrom || 0;
198
- var to = this.state.visibleTo || tabs.length - 1;
199
- var prevTabs = _sliceInstanceProperty(tabs).call(tabs, 0, from);
200
- var afterTabs = _sliceInstanceProperty(tabs).call(tabs, to + 1, tabs.length);
201
- return _concatInstanceProperty(_context3 = []).call(_context3, _toConsumableArray(prevTabs), _toConsumableArray(afterTabs));
202
- }
203
- }, {
204
- key: "canEllipsis",
205
- get: function get() {
206
- var _context4;
207
- var _ref2 = this.props,
208
- type = _ref2.type;
209
- return _includesInstanceProperty(_context4 = ['plain', 'card']).call(_context4, type);
210
- }
211
- }, {
212
- key: "withEllipsis",
213
- get: function get() {
214
- return !!this.tabsEllipsis.length;
215
- }
216
- }, {
217
- key: "isTabPaneDisabled",
218
- value: function isTabPaneDisabled(id) {
219
- var _context5, _context6;
220
- return _someInstanceProperty(_context5 = _filterInstanceProperty(_context6 = this.tabsAll).call(_context6, function (tab) {
221
- return tab.disabled;
222
- })).call(_context5, function (tab) {
223
- return tab.id === id;
346
+ var updateWidth = function updateWidth() {
347
+ // 考虑初始 display none 没有宽度、tab 宽度变化的情况
348
+ // 暂不提前优化计算时机
349
+ var $tabsWrap = refTabsWrap.current;
350
+ var $tabs = refTabs.current;
351
+ /* istanbul ignore else: refs 仅在挂载提交后才被测量调用,此处为防御性判空,else 分支 jsdom 不可达 */
352
+ if ($tabsWrap && $tabs) {
353
+ var _context5;
354
+ tabsWrapWidthRef.current = $tabsWrap.getBoundingClientRect().width;
355
+ // .brick-tabs-wrap 因为 overflow: hidden
356
+ // 估四周给了一个边缘扩大(padding: x; margin: -x),以使 focus-ring 可以露出来,
357
+ // 详见 style.css 的 .brick-tabs-wrap 的修饰
358
+ // 最大滚动补正 4 * 2
359
+ var overlapMargin = 4 * 2 - BORDER_WIDTH;
360
+ tabsWrapMaxScrollLeftRef.current = $tabs.getBoundingClientRect().width - tabsWrapWidthRef.current + overlapMargin;
361
+ tabsWidthRef.current = _mapInstanceProperty(_context5 = _Array$from($tabs.querySelectorAll('li'))).call(_context5, function (tab) {
362
+ return tab.getBoundingClientRect().width;
224
363
  });
225
364
  }
226
- }, {
227
- key: "updateWidth",
228
- value: function updateWidth() {
229
- // 考虑初始 display none 没有宽度、tab 宽度变化的情况
230
- // 暂不提前优化计算时机
231
- var $tabsWrap = this.refTabsWrap.current;
232
- var $tabs = this.refTabs.current;
233
- if ($tabsWrap && $tabs) {
234
- var _context7;
235
- this.tabsWrapWidth = $tabsWrap.getBoundingClientRect().width;
236
- // .brick-tabs-wrap 因为 overflow: hidden
237
- // 估四周给了一个边缘扩大(padding: x; margin: -x),以使 focus-ring 可以露出来,
238
- // 详见 style.css 的 .brick-tabs-wrap 的修饰
239
- // 最大滚动补正 4 * 2
240
- var overlapMargin = 4 * 2 - BORDER_WIDTH;
241
- this.tabsWrapMaxScrollLeft = $tabs.getBoundingClientRect().width - this.tabsWrapWidth + overlapMargin;
242
- this.tabsWidth = _mapInstanceProperty(_context7 = _Array$from($tabs.querySelectorAll('li'))).call(_context7, function (tab) {
243
- return tab.getBoundingClientRect().width;
244
- });
365
+ };
366
+ var updateEllipsis = function updateEllipsis() {
367
+ var tabsCount = tabsAll.length;
368
+ if (!canEllipsis || !tabsCount) {
369
+ return;
370
+ }
371
+ updateWidth();
372
+ var visibleLeft = tabsWrapScrollLeftRef.current;
373
+ var visibleRight = visibleLeft + tabsWrapWidthRef.current;
374
+ var borderWidth = type === 'card' ? BORDER_WIDTH : 0;
375
+ var from = -1;
376
+ var to = 0;
377
+ var i = -1;
378
+ var currentLeft = 0;
379
+ var currentRight = 0;
380
+ while (++i < tabsCount) {
381
+ if (currentLeft > visibleRight) {
382
+ break;
245
383
  }
246
- }
247
- }, {
248
- key: "updateEllipsis",
249
- value: function updateEllipsis() {
250
- var _this$state = this.state,
251
- visibleFrom = _this$state.visibleFrom,
252
- visibleTo = _this$state.visibleTo;
253
- var tabsCount = this.tabsAll.length;
254
- if (!this.canEllipsis || !tabsCount) {
255
- return;
256
- }
257
- this.updateWidth();
258
- var visibleLeft = this.tabsWrapScrollLeft;
259
- var visibleRight = visibleLeft + this.tabsWrapWidth;
260
- var borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0;
261
- var from = -1;
262
- var to = 0;
263
- var i = -1;
264
- var currentLeft = 0;
265
- var currentRight = 0;
266
- while (++i < tabsCount) {
267
- if (currentLeft > visibleRight) {
268
- break;
269
- }
270
- currentRight += this.tabsWidth[i] - borderWidth;
271
- if (currentRight < visibleLeft) {
272
- continue;
273
- }
274
- var visible = currentLeft >= visibleLeft && currentRight <= visibleRight;
275
- if (visible) {
276
- from = from < 0 ? i : from;
277
- to = i;
278
- }
279
- currentLeft = currentRight;
280
- }
281
- from = from < 0 ? 0 : from;
282
- if (visibleFrom !== from || visibleTo !== to) {
283
- this.setState({
284
- visibleFrom: from,
285
- visibleTo: to
286
- });
287
- }
288
- }
289
- }, {
290
- key: "changeActive",
291
- value: function changeActive(id) {
292
- if (this.state.activeId === id || this.isTabPaneDisabled(id)) {
293
- return;
384
+ currentRight += tabsWidthRef.current[i] - borderWidth;
385
+ if (currentRight < visibleLeft) {
386
+ continue;
294
387
  }
295
- if (isUndefined(this.props.activeId)) {
296
- this.setState({
297
- activeId: id
298
- });
388
+ var visible = currentLeft >= visibleLeft && currentRight <= visibleRight;
389
+ if (visible) {
390
+ from = from < 0 ? i : from;
391
+ to = i;
299
392
  }
300
- safeInvoke(this.props.onChange, id);
393
+ currentLeft = currentRight;
301
394
  }
302
- }, {
303
- key: "deleteTab",
304
- value: function deleteTab(id) {
305
- safeInvoke(this.props.onTabDelete, id);
306
- }
307
- }, {
308
- key: "scrollTabsWrap",
309
- value: function scrollTabsWrap() {
310
- var scrollLeftTo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.tabsWrapScrollLeft;
311
- var scrollLeft = Math.min(this.tabsWrapMaxScrollLeft, scrollLeftTo);
312
- scrollLeft = Math.max(0, scrollLeft);
313
- if (scrollLeft !== this.tabsWrapScrollLeft) {
314
- this.refTabs.current.style.transform = "translate(-".concat(scrollLeft, "px, 0px)");
315
- this.tabsWrapScrollLeft = scrollLeft;
316
- this.updateEllipsis();
317
- }
395
+ from = from < 0 ? 0 : from;
396
+ if (visibleFrom !== from || visibleTo !== to) {
397
+ setVisibleRange({
398
+ from: from,
399
+ to: to
400
+ });
318
401
  }
319
- }, {
320
- key: "scrollTabsWrapToTab",
321
- value: function scrollTabsWrapToTab(id) {
322
- var _context8, _context9, _context0;
323
- var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
324
- if (!this.refTabs.current || !this.canEllipsis) {
325
- return;
326
- }
327
- var _this$state2 = this.state,
328
- visibleFrom = _this$state2.visibleFrom,
329
- visibleTo = _this$state2.visibleTo;
330
- var activeIndex = id ? _findIndexInstanceProperty(_context8 = this.tabsAll).call(_context8, function (tab) {
331
- return tab.id === id;
332
- }) : 0;
402
+ };
333
403
 
334
- // 在可视区域内,不滚动,除非强制(比如 window resize 时)
335
- if (!force && activeIndex >= visibleFrom && activeIndex <= visibleTo) {
336
- return;
337
- }
338
- var borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0;
339
- var right = this.tabsAll.length - 1;
340
- var left = 0;
341
- var totalWidth = this.tabsWidth[activeIndex];
342
- var from = activeIndex;
343
- var to = activeIndex;
404
+ // __HOOK_METHODS_2__
405
+
406
+ /* istanbul ignore next: 所有调用方都显式传入 scrollLeftTo,默认参数分支不可达 */
407
+ var scrollTabsWrap = function scrollTabsWrap() {
408
+ var scrollLeftTo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : tabsWrapScrollLeftRef.current;
409
+ var scrollLeft = Math.min(tabsWrapMaxScrollLeftRef.current, scrollLeftTo);
410
+ scrollLeft = Math.max(0, scrollLeft);
411
+ if (scrollLeft !== tabsWrapScrollLeftRef.current) {
412
+ refTabs.current.style.transform = "translate(-".concat(scrollLeft, "px, 0px)");
413
+ tabsWrapScrollLeftRef.current = scrollLeft;
414
+ updateEllipsis();
415
+ }
416
+ };
417
+ var scrollTabsWrapToTab = function scrollTabsWrapToTab(id) {
418
+ var _context6, _context7;
419
+ var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
420
+ if (!refTabs.current || !canEllipsis) {
421
+ return;
422
+ }
423
+ var activeIndex = id ? _findIndexInstanceProperty(tabsAll).call(tabsAll, function (tab) {
424
+ return tab.id === id;
425
+ }) : 0;
426
+
427
+ // 在可视区域内,不滚动,除非强制(比如 window resize 时)
428
+ if (!force && activeIndex >= visibleFrom && activeIndex <= visibleTo) {
429
+ return;
430
+ }
431
+ var borderWidth = type === 'card' ? BORDER_WIDTH : 0;
432
+ var right = tabsAll.length - 1;
433
+ var left = 0;
434
+ var totalWidth = tabsWidthRef.current[activeIndex];
435
+ var from = activeIndex;
436
+ var to = activeIndex;
344
437
 
345
- // from ... active ... to
346
- // 从 active 向两侧延展,以确定 from 进行,scroll
347
- // scroll 时虽然不会用到 to ,但 to 会影响 from 的确定,因为当切换时,希望以 active 为中心,尽量显示左右两侧
348
- while (totalWidth < this.tabsWrapWidth && (from > left || to < right)) {
349
- // 先扩展右边
350
- if (to < right) {
351
- var tempWidth = totalWidth + this.tabsWidth[++to] - borderWidth;
352
- if (tempWidth > this.tabsWrapWidth) {
353
- to--;
354
- break;
355
- }
356
- totalWidth = tempWidth;
438
+ // from ... active ... to
439
+ // 从 active 向两侧延展,以确定 from 进行,scroll
440
+ // scroll 时虽然不会用到 to ,但 to 会影响 from 的确定,因为当切换时,希望以 active 为中心,尽量显示左右两侧
441
+ while (totalWidth < tabsWrapWidthRef.current && (from > left || to < right)) {
442
+ // 先扩展右边
443
+ if (to < right) {
444
+ var tempWidth = totalWidth + tabsWidthRef.current[++to] - borderWidth;
445
+ if (tempWidth > tabsWrapWidthRef.current) {
446
+ to--;
447
+ break;
357
448
  }
358
- if (from > left) {
359
- var _tempWidth = totalWidth + this.tabsWidth[--from] - borderWidth;
360
- if (_tempWidth > this.tabsWrapWidth) {
361
- from++;
362
- break;
363
- }
364
- totalWidth = _tempWidth;
449
+ totalWidth = tempWidth;
450
+ }
451
+ if (from > left) {
452
+ var _tempWidth = totalWidth + tabsWidthRef.current[--from] - borderWidth;
453
+ if (_tempWidth > tabsWrapWidthRef.current) {
454
+ from++;
455
+ break;
365
456
  }
457
+ totalWidth = _tempWidth;
366
458
  }
367
- var scrollLeftTo = _reduceInstanceProperty(_context9 = _sliceInstanceProperty(_context0 = this.tabsWidth).call(_context0, 0, from)).call(_context9, function (accumulator, currentValue) {
368
- return accumulator + currentValue;
369
- }, 0);
370
- this.scrollTabsWrap(scrollLeftTo);
371
459
  }
460
+ var scrollLeftTo = _reduceInstanceProperty(_context6 = _sliceInstanceProperty(_context7 = tabsWidthRef.current).call(_context7, 0, from)).call(_context6, function (accumulator, currentValue) {
461
+ return accumulator + currentValue;
462
+ }, 0);
463
+ scrollTabsWrap(scrollLeftTo);
464
+ };
372
465
 
373
- // 滚动到可视区域
374
- }, {
375
- key: "scrollTabsWrapToActive",
376
- value: function scrollTabsWrapToActive() {
377
- var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
378
- var activeId = this.state.activeId;
379
- activeId && this.scrollTabsWrapToTab(activeId, force);
380
- }
381
- }, {
382
- key: "renderTabDelete",
383
- value: function renderTabDelete(tab) {
384
- // 仅 type card 只支持添加、删除
385
- if (this.props.type !== 'card' || !tab.deletable) {
386
- return null;
387
- }
466
+ // 滚动到可视区域
467
+ var scrollTabsWrapToActive = function scrollTabsWrapToActive() {
468
+ var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
469
+ activeId && scrollTabsWrapToTab(activeId, force);
470
+ };
471
+ var handleWheelTabsWrap = function handleWheelTabsWrap(e) {
472
+ if (!withEllipsis) {
473
+ return;
474
+ }
475
+ var deltaX = e.deltaX;
476
+ var deltaY = e.deltaY;
477
+ var scrollLeft = Math.abs(deltaX) > Math.abs(deltaY) ? deltaX : deltaY;
478
+ scrollTabsWrap(tabsWrapScrollLeftRef.current + scrollLeft);
479
+ e.preventDefault();
480
+ };
388
481
 
389
- // 点击由 handleClickTab 代理
390
- return /*#__PURE__*/_jsx(CloseX, {
391
- className: 'brick-tabs-tab-delete',
392
- size: 'sm'
393
- });
394
- }
395
- }, {
396
- key: "renderTabStatus",
397
- value: function renderTabStatus(tab) {
398
- // 仅 type card 支持
399
- if (this.props.type !== 'card' || tab.disabled) {
400
- return null;
401
- }
482
+ // __HOOK_EFFECTS__
402
483
 
403
- // 暂时只有 error 状态
404
- return tab.status === 'error' && /*#__PURE__*/_jsx(Icon, {
405
- className: 'brick-tabs-tab-status-icon',
406
- svg: SvgSignExclamation
407
- });
408
- }
409
- }, {
410
- key: "renderTabs",
411
- value: function renderTabs() {
412
- var _context1;
413
- return /*#__PURE__*/_jsx("div", {
414
- className: 'brick-tabs-wrap',
415
- ref: this.refTabsWrap,
416
- children: /*#__PURE__*/_jsx("ul", {
417
- ref: this.refTabs,
418
- children: _mapInstanceProperty(_context1 = this.tabsAll).call(_context1, this.renderTab)
419
- })
420
- });
421
- }
422
- }, {
423
- key: "renderTabsEllipsisMenu",
424
- value: function renderTabsEllipsisMenu() {
425
- var _context10,
426
- _this2 = this;
427
- // 只有 type card 支持状态
428
- var isTypeCard = this.props.type === 'card';
429
- return /*#__PURE__*/_jsx(Menu, {
430
- type: 'common',
431
- children: _mapInstanceProperty(_context10 = this.tabsEllipsis).call(_context10, function (tab) {
432
- var className = c('brick-tabs-ellipsis-item', isTypeCard && tab.status && "brick-tabs-ellipsis-item-status-".concat(tab.status));
433
- return /*#__PURE__*/_jsx(MenuItem, {
434
- className: className,
435
- onClick: function onClick(e) {
436
- _this2.handleClickTab(e, tab.id);
437
- },
438
- suffix: _this2.renderTabDelete(tab),
439
- disabled: tab.disabled,
440
- children: tab.label
441
- }, String(tab.id));
442
- })
443
- });
444
- }
445
- }, {
446
- key: "renderTabsEllipsis",
447
- value: function renderTabsEllipsis() {
448
- if (!this.tabsEllipsis.length) {
449
- return null;
450
- }
451
- var targetNode = /*#__PURE__*/_jsx("div", {
452
- className: 'brick-tabs-ellipsis',
453
- children: /*#__PURE__*/_jsx(Icon, {
454
- svg: this.state.isEllipsisPopperVisible ? SvgPlainUp : SvgPlainDown
455
- })
456
- });
457
- return /*#__PURE__*/_jsx(MenuPopper, {
458
- className: 'brick-tabs-ellipsis-dropdown-popper',
459
- size: this.size,
460
- targetNode: targetNode,
461
- placement: this.props.ellipsisDropdownPlacement,
462
- type: this.props.triggerType,
463
- onChange: this.handleMenuPopperChange,
464
- children: this.renderTabsEllipsisMenu()
465
- });
466
- }
467
- }, {
468
- key: "renderAddButton",
469
- value: function renderAddButton() {
470
- var _this$props = this.props,
471
- type = _this$props.type,
472
- onClickAdd = _this$props.onClickAdd;
484
+ // 镜像 componentDidMount + componentDidUpdate 的 updateEllipsis;
485
+ // activeId 变化(且非首次挂载)时追加 scrollTabsWrapToActive(首挂只测量不滚动)
486
+ useEffect(function () {
487
+ updateEllipsis();
488
+ if (mountedRef.current && prevActiveIdRef.current !== activeId) {
489
+ scrollTabsWrapToActive();
490
+ }
491
+ mountedRef.current = true;
492
+ prevActiveIdRef.current = activeId;
493
+ });
473
494
 
474
- // 仅 type card 支持添加、删除
475
- if (type !== 'card' || !onClickAdd) {
476
- return null;
477
- }
478
- return /*#__PURE__*/_jsx("div", {
479
- className: 'brick-tabs-add',
480
- children: /*#__PURE__*/_jsx(Button, {
481
- className: 'brick-tabs-add-button',
482
- onClick: this.handleClickAdd,
483
- type: 'plain',
484
- color: 'brand',
485
- size: 'md',
486
- icon: SvgMarkPlus
487
- })
488
- });
489
- }
490
- }, {
491
- key: "renderButtonGroupTabs",
492
- value: function renderButtonGroupTabs() {
493
- var _context11;
494
- var type = this.props.type;
495
- var value = this.state.activeId;
496
- var options = _mapInstanceProperty(_context11 = this.tabsAll).call(_context11, function (child) {
497
- var id = child.id,
498
- label = child.label,
499
- disabled = child.disabled;
500
- return {
501
- value: id,
502
- label: label,
503
- disabled: disabled
504
- };
505
- });
506
- return /*#__PURE__*/_jsx(RadioGroup, {
507
- className: 'brick-tabs-group',
508
- type: type === 'button-group-important' ? 'important' : 'intensive',
509
- size: this.size,
510
- value: value,
511
- options: options,
512
- onChange: this.handleChangeRadioGroup
513
- });
514
- }
515
- }, {
516
- key: "renderTabsContainer",
517
- value: function renderTabsContainer() {
518
- var _this$props$type;
519
- if ((_this$props$type = this.props.type) !== null && _this$props$type !== void 0 && _includesInstanceProperty(_this$props$type).call(_this$props$type, 'button-group')) {
520
- return /*#__PURE__*/_jsx("div", {
521
- className: 'brick-tabs-container',
522
- children: this.renderButtonGroupTabs()
523
- });
524
- }
525
- return /*#__PURE__*/_jsxs("div", {
526
- className: 'brick-tabs-container',
527
- children: [this.renderTabs(), this.renderTabsEllipsis(), this.renderAddButton()]
528
- });
529
- }
530
- }, {
531
- key: "renderTabPanes",
532
- value: function renderTabPanes() {
533
- var _this3 = this;
534
- var _this$props2 = this.props,
535
- panes = _this$props2.panes,
536
- shouldDestroyInactivePane = _this$props2.shouldDestroyInactivePane;
537
- var validChildren = this.validChildren;
538
- if (validChildren) {
539
- if (shouldDestroyInactivePane) {
540
- return _filterInstanceProperty(validChildren).call(validChildren, function (tab) {
541
- return _this3.state.activeId === tab.props.id;
542
- });
543
- }
544
- return validChildren;
545
- }
546
- if (!panes || !panes.length) {
547
- return null;
548
- }
549
- var panesResult = panes;
550
- if (shouldDestroyInactivePane) {
551
- panesResult = _filterInstanceProperty(panes).call(panes, function (tab) {
552
- return _this3.state.activeId === tab.id;
553
- });
554
- }
555
- return _mapInstanceProperty(panesResult).call(panesResult, function (tab) {
556
- var content = tab.content,
557
- tabProps = _objectWithoutProperties(tab, _excluded);
558
- return /*#__PURE__*/_jsx(TabPane, _objectSpread(_objectSpread({}, tabProps), {}, {
559
- children: content
560
- }), String(tab.id));
561
- });
562
- }
563
- }, {
564
- key: "renderTabPanesContainer",
565
- value: function renderTabPanesContainer() {
566
- var panes = this.renderTabPanes();
567
- return panes ? /*#__PURE__*/_jsx("div", {
568
- className: 'brick-tabs-panes-container',
569
- children: panes
570
- }) : null;
571
- }
572
- }, {
573
- key: "componentDidMount",
574
- value: function componentDidMount() {
575
- var _this4 = this;
576
- this.updateEllipsis();
577
- this.windowResizeHandler = addEventListener(window, 'resize', function () {
578
- _this4.updateEllipsis();
579
- // 需要强制滚动否则
580
- // - 除了 active 会消失在可视区,
581
- // - 更不能接受的是,如最后一个 tab active,wrap 宽度增大将使 tabs 和 ellipsis 之间出现空白
582
- _this4.scrollTabsWrapToActive(true);
583
- });
584
- if (this.refTabsWrap.current) {
585
- // react onWheel 事件无法设置 passive: false,而不能 `e.preventDefault()`
586
- this.tabsWheelHandler = addEventListener(this.refTabsWrap.current, 'wheel', this.handleWheelTabsWrap, {
587
- passive: false
588
- });
589
- }
590
- }
591
- }, {
592
- key: "componentWillUnmount",
593
- value: function componentWillUnmount() {
594
- this.windowResizeHandler && this.windowResizeHandler.remove();
595
- }
596
- }, {
597
- key: "componentDidUpdate",
598
- value: function componentDidUpdate(prevProps, prevState) {
599
- this.updateEllipsis();
600
- if (prevState.activeId !== this.state.activeId) {
601
- this.scrollTabsWrapToActive();
602
- }
603
- }
604
- }, {
605
- key: "render",
606
- value: function render() {
607
- return /*#__PURE__*/_jsx(TabsContext.Provider, {
608
- value: this.contextValue,
609
- children: /*#__PURE__*/_jsxs("div", {
610
- className: this.className,
611
- children: [this.renderTabsContainer(), this.renderTabPanesContainer()]
612
- })
495
+ // 让长订阅(resize / wheel)回调始终指向最新一次渲染的闭包,规避 [] 订阅捕获过期 state/props
496
+ useEffect(function () {
497
+ latestRef.current.onResize = function () {
498
+ updateEllipsis();
499
+ // 需要强制滚动否则
500
+ // - 除了 active 会消失在可视区,
501
+ // - 更不能接受的是,如最后一个 tab active,wrap 宽度增大将使 tabs 和 ellipsis 之间出现空白
502
+ scrollTabsWrapToActive(true);
503
+ };
504
+ latestRef.current.onWheel = handleWheelTabsWrap;
505
+ });
506
+ useEffect(function () {
507
+ var resizeHandler = addEventListener(window, 'resize', function () {
508
+ return latestRef.current.onResize();
509
+ });
510
+
511
+ // react onWheel 事件无法设置 passive: false,而不能 `e.preventDefault()`
512
+ var $tabsWrap = refTabsWrap.current;
513
+ var wheelHandler = null;
514
+ if ($tabsWrap) {
515
+ wheelHandler = addEventListener($tabsWrap, 'wheel', function (e) {
516
+ return latestRef.current.onWheel(e);
517
+ }, {
518
+ passive: false
613
519
  });
614
520
  }
615
- }], [{
616
- key: "getDerivedStateFromProps",
617
- value: function getDerivedStateFromProps(nextProps) {
618
- return pickDerivedStateFromProps(nextProps, ['activeId']);
619
- }
620
- }]);
621
- }(Component);
622
- _defineProperty(Tabs, "displayName", 'Tabs');
623
- // static propTypes = {}
624
- _defineProperty(Tabs, "defaultProps", {
625
- className: '',
626
- type: 'plain',
627
- defaultActiveId: null,
628
- shouldDestroyInactivePane: false,
629
- ellipsisDropdownPlacement: 'bottom-end'
630
- });
631
- _defineProperty(Tabs, "contextType", ConfigContext);
521
+ return function () {
522
+ resizeHandler && resizeHandler.remove();
523
+ wheelHandler && wheelHandler.remove();
524
+ };
525
+ }, []);
526
+ return {
527
+ refTabsWrap: refTabsWrap,
528
+ refTabs: refTabs,
529
+ scrollTabsWrapToTab: scrollTabsWrapToTab
530
+ };
531
+ }
532
+ export function Tabs(props) {
533
+ var _props$type, _props$ellipsisDropdo, _context8;
534
+ var type = (_props$type = props.type) !== null && _props$type !== void 0 ? _props$type : 'plain';
535
+ var customClassName = props.className,
536
+ triggerType = props.triggerType,
537
+ panes = props.panes,
538
+ children = props.children,
539
+ _props$shouldDestroyI = props.shouldDestroyInactivePane,
540
+ shouldDestroyInactivePane = _props$shouldDestroyI === void 0 ? false : _props$shouldDestroyI;
541
+ var ellipsisDropdownPlacement = (_props$ellipsisDropdo = props.ellipsisDropdownPlacement) !== null && _props$ellipsisDropdo !== void 0 ? _props$ellipsisDropdo : 'bottom-end';
542
+ var _useState = useState(function () {
543
+ return getInitialActiveId(props);
544
+ }),
545
+ _useState2 = _slicedToArray(_useState, 2),
546
+ innerActiveId = _useState2[0],
547
+ setInnerActiveId = _useState2[1];
548
+ var _useState3 = useState({
549
+ from: 0,
550
+ to: 0
551
+ }),
552
+ _useState4 = _slicedToArray(_useState3, 2),
553
+ visibleRange = _useState4[0],
554
+ setVisibleRange = _useState4[1];
555
+ var _useState5 = useState(false),
556
+ _useState6 = _slicedToArray(_useState5, 2),
557
+ isEllipsisPopperVisible = _useState6[0],
558
+ setIsEllipsisPopperVisible = _useState6[1];
559
+ var _useTheme = useTheme(),
560
+ baseSize = _useTheme.baseSize;
561
+ var size = resolveSize(props, type, baseSize);
562
+
563
+ // 受控 activeId 优先,否则用内部 state(镜像原 getDerivedStateFromProps)
564
+ var activeId = isUndefined(props.activeId) ? innerActiveId : props.activeId;
565
+ var visibleFrom = visibleRange.from,
566
+ visibleTo = visibleRange.to;
567
+ var validChildren = getValidChildren(children);
568
+ var tabsAll = getTabsAll(panes, validChildren);
569
+ var tabsEllipsis = getTabsEllipsis(tabsAll, visibleFrom, visibleTo);
570
+ var canEllipsis = _includesInstanceProperty(_context8 = ['plain', 'card']).call(_context8, type);
571
+ var withEllipsis = !!tabsEllipsis.length;
572
+ var tabsClassName = cx('brick-tabs', "brick-tabs-type-".concat(type), "brick-tabs-size-".concat(size), {
573
+ 'brick-tabs-with-ellipsis': tabsEllipsis.length
574
+ }, customClassName);
575
+ var _useTabsScroll = useTabsScroll({
576
+ tabsAll: tabsAll,
577
+ type: type,
578
+ canEllipsis: canEllipsis,
579
+ withEllipsis: withEllipsis,
580
+ activeId: activeId,
581
+ visibleFrom: visibleFrom,
582
+ visibleTo: visibleTo,
583
+ setVisibleRange: setVisibleRange
584
+ }),
585
+ refTabsWrap = _useTabsScroll.refTabsWrap,
586
+ refTabs = _useTabsScroll.refTabs,
587
+ scrollTabsWrapToTab = _useTabsScroll.scrollTabsWrapToTab;
588
+ var isTabPaneDisabled = function isTabPaneDisabled(id) {
589
+ var _context9;
590
+ return _someInstanceProperty(_context9 = _filterInstanceProperty(tabsAll).call(tabsAll, function (tab) {
591
+ return tab.disabled;
592
+ })).call(_context9, function (tab) {
593
+ return tab.id === id;
594
+ });
595
+ };
596
+ var changeActive = function changeActive(id) {
597
+ if (activeId === id || isTabPaneDisabled(id)) {
598
+ return;
599
+ }
600
+ if (isUndefined(props.activeId)) {
601
+ setInnerActiveId(id);
602
+ }
603
+ safeInvoke(props.onChange, id);
604
+ };
605
+ var deleteTab = function deleteTab(id) {
606
+ safeInvoke(props.onTabDelete, id);
607
+ };
608
+ var handleClickAdd = function handleClickAdd(e) {
609
+ var nextId = safeInvoke(props.onClickAdd, e);
610
+ nextId && changeActive(nextId);
611
+ };
612
+ var handleClickTab = function handleClickTab(e, id) {
613
+ var $removeBtn = e.currentTarget.querySelector('.brick-tabs-tab-delete');
614
+ if ($removeBtn && ifNodeContains($removeBtn, e.target)) {
615
+ deleteTab(id);
616
+ } else {
617
+ changeActive(id);
618
+ }
619
+ if (!isTabPaneDisabled(id)) {
620
+ safeInvoke(props.onClickTab, e, id);
621
+ }
622
+ };
623
+ var handleChangeRadioGroup = function handleChangeRadioGroup(value) {
624
+ value && changeActive(value);
625
+ };
626
+ var handleMenuPopperChange = function handleMenuPopperChange(status) {
627
+ setIsEllipsisPopperVisible(status);
628
+ };
629
+ var ctx = {
630
+ type: type,
631
+ size: size,
632
+ activeId: activeId,
633
+ tabsAll: tabsAll,
634
+ tabsEllipsis: tabsEllipsis,
635
+ isEllipsisPopperVisible: isEllipsisPopperVisible,
636
+ triggerType: triggerType,
637
+ ellipsisDropdownPlacement: ellipsisDropdownPlacement,
638
+ hasOnClickAdd: !isUndefined(props.onClickAdd),
639
+ shouldDestroyInactivePane: shouldDestroyInactivePane,
640
+ panes: panes,
641
+ validChildren: validChildren,
642
+ refTabsWrap: refTabsWrap,
643
+ refTabs: refTabs,
644
+ onClickTab: handleClickTab,
645
+ onChangeActive: changeActive,
646
+ onFocusTab: function onFocusTab(id) {
647
+ return scrollTabsWrapToTab(id);
648
+ },
649
+ onClickAdd: handleClickAdd,
650
+ onChangeRadioGroup: handleChangeRadioGroup,
651
+ onMenuPopperChange: handleMenuPopperChange
652
+ };
653
+ var contextValue = {
654
+ activeId: activeId
655
+ };
656
+ return /*#__PURE__*/_jsx(TabsContext.Provider, {
657
+ value: contextValue,
658
+ children: /*#__PURE__*/_jsxs("div", {
659
+ className: tabsClassName,
660
+ children: [/*#__PURE__*/_jsx(TabsContainerView, {
661
+ ctx: ctx
662
+ }), /*#__PURE__*/_jsx(TabPanesContainerView, {
663
+ ctx: ctx
664
+ })]
665
+ })
666
+ });
667
+ }
668
+ Tabs.displayName = 'Tabs';