@befe/brick-comp-tabs 0.2.92

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 ADDED
@@ -0,0 +1,634 @@
1
+ import _Reflect$construct from "@babel/runtime-corejs3/core-js-stable/reflect/construct";
2
+ import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
3
+ import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
4
+ import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
5
+ import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
6
+ import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
7
+ import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
8
+ import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
9
+ import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/objectWithoutProperties";
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 _assertThisInitialized from "@babel/runtime-corejs3/helpers/assertThisInitialized";
14
+ import _inherits from "@babel/runtime-corejs3/helpers/inherits";
15
+ import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/possibleConstructorReturn";
16
+ import _getPrototypeOf from "@babel/runtime-corejs3/helpers/getPrototypeOf";
17
+ import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
18
+ var _excluded = ["content"];
19
+ 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; }
20
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context14, _context15; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context14 = ownKeys(Object(t), !0)).call(_context14, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context15 = ownKeys(Object(t))).call(_context15, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
21
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
22
+ import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
23
+ import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
24
+ import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
25
+ import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
26
+ import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some";
27
+ import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
28
+ import _findIndexInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find-index";
29
+ import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
30
+ function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = _Reflect$construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
31
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
32
+ import { Component, createRef, Children, isValidElement } from 'react';
33
+ // import * as PropTypes from 'prop-types'
34
+ import c from 'classnames';
35
+ import { addEventListener, ifNodeContains, pick, pickDerivedStateFromProps, safeInvoke } from '@befe/brick-utils';
36
+ import { isUndefined } from 'lodash-es';
37
+ import { ConfigContext, getDefaultValueUsingContextTheme } from '@befe/brick-core';
38
+ import { Icon } from '@befe/brick-comp-icon';
39
+ import { SvgPlainDown, SvgPlainUp, SvgMarkPlus, SvgSignExclamation } from '@befe/brick-icon';
40
+ import { Menu, MenuItem, MenuPopper } from '@befe/brick-comp-menu';
41
+ import { RadioGroup } from '@befe/brick-comp-radio';
42
+ import { Button } from '@befe/brick-comp-button';
43
+ import { CloseX } from '@befe/brick-comp-close-x';
44
+ import { TabPane } from './tab-pane';
45
+ import { TabsContext } from './tabs-context';
46
+ import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
47
+ var BORDER_WIDTH = 1;
48
+ var SIZED_TYPES = ['plain', 'tag'];
49
+ export var Tabs = /*#__PURE__*/function (_Component) {
50
+ _inherits(Tabs, _Component);
51
+ var _super = _createSuper(Tabs);
52
+ function Tabs() {
53
+ var _context;
54
+ var _this;
55
+ _classCallCheck(this, Tabs);
56
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
57
+ args[_key] = arguments[_key];
58
+ }
59
+ _this = _super.call.apply(_super, _concatInstanceProperty(_context = [this]).call(_context, args));
60
+ _defineProperty(_assertThisInitialized(_this), "context", void 0);
61
+ _defineProperty(_assertThisInitialized(_this), "windowResizeHandler", void 0);
62
+ _defineProperty(_assertThisInitialized(_this), "tabsWheelHandler", null);
63
+ _defineProperty(_assertThisInitialized(_this), "state", {
64
+ activeId: (isUndefined(_this.props.activeId) ? _this.props.defaultActiveId : _this.props.activeId) || null,
65
+ visibleFrom: 0,
66
+ visibleTo: 0,
67
+ isEllipsisPopperVisible: false
68
+ });
69
+ _defineProperty(_assertThisInitialized(_this), "refTabsWrap", /*#__PURE__*/createRef());
70
+ _defineProperty(_assertThisInitialized(_this), "refTabs", /*#__PURE__*/createRef());
71
+ _defineProperty(_assertThisInitialized(_this), "tabsWrapWidth", 0);
72
+ _defineProperty(_assertThisInitialized(_this), "tabsWidth", []);
73
+ _defineProperty(_assertThisInitialized(_this), "tabsWrapMaxScrollLeft", 0);
74
+ _defineProperty(_assertThisInitialized(_this), "tabsWrapScrollLeft", 0);
75
+ _defineProperty(_assertThisInitialized(_this), "handleClickAdd", function (e) {
76
+ var activeId = safeInvoke(_this.props.onClickAdd, e);
77
+ activeId && _this.changeActive(activeId);
78
+ });
79
+ _defineProperty(_assertThisInitialized(_this), "handleClickTab", function (e, id) {
80
+ var $removeBtn = e.currentTarget.querySelector('.brick-tabs-tab-delete');
81
+ if ($removeBtn && ifNodeContains($removeBtn, e.target)) {
82
+ _this.deleteTab(id);
83
+ } else {
84
+ _this.changeActive(id);
85
+ }
86
+ if (!_this.isTabPaneDisabled(id)) {
87
+ safeInvoke(_this.props.onClickTab, e, id);
88
+ }
89
+ });
90
+ _defineProperty(_assertThisInitialized(_this), "handleChangeRadioGroup", function (value) {
91
+ value && _this.changeActive(value);
92
+ });
93
+ _defineProperty(_assertThisInitialized(_this), "handleMenuPopperChange", function (status) {
94
+ _this.setState({
95
+ isEllipsisPopperVisible: status
96
+ });
97
+ });
98
+ _defineProperty(_assertThisInitialized(_this), "handleWheelTabsWrap", function (e) {
99
+ if (!_this.withEllipsis) {
100
+ return;
101
+ }
102
+ var deltaX = e.deltaX;
103
+ var deltaY = e.deltaY;
104
+ var scrollLeft = Math.abs(deltaX) > Math.abs(deltaY) ? deltaX : deltaY;
105
+ _this.scrollTabsWrap(_this.tabsWrapScrollLeft + scrollLeft);
106
+ e.preventDefault();
107
+ });
108
+ _defineProperty(_assertThisInitialized(_this), "renderTab", function (tab) {
109
+ var tabClass = c('brick-tabs-tab', _defineProperty({
110
+ 'brick-tabs-tab-active': _this.state.activeId === tab.id,
111
+ 'brick-tabs-tab-disabled': tab.disabled,
112
+ 'brick-tabs-tab-deletable': tab.deletable
113
+ }, "brick-tabs-tab-status-".concat(tab.status), tab.status));
114
+ var tabProps = {
115
+ className: tabClass,
116
+ onClick: function onClick(e) {
117
+ _this.handleClickTab(e, tab.id);
118
+ },
119
+ tabIndex: tab.disabled ? -1 : 0,
120
+ onFocus: function onFocus() {
121
+ _this.scrollTabsWrapToTab(tab.id);
122
+ },
123
+ onKeyDown: function onKeyDown(e) {
124
+ if (e.key === 'Enter' || e.key === ' ') {
125
+ _this.changeActive(tab.id);
126
+ // 阻止 "空格" 触发的滚动
127
+ e.preventDefault();
128
+ }
129
+ }
130
+ };
131
+ return /*#__PURE__*/_jsxs("li", _objectSpread(_objectSpread({}, tabProps), {}, {
132
+ children: [_this.renderTabStatus(tab), tab.label, _this.renderTabDelete(tab)]
133
+ }), String(tab.id));
134
+ });
135
+ return _this;
136
+ }
137
+ _createClass(Tabs, [{
138
+ key: "className",
139
+ get: function get() {
140
+ var _ref = this.props,
141
+ className = _ref.className,
142
+ type = _ref.type;
143
+ var size = this.size;
144
+ return c('brick-tabs', "brick-tabs-type-".concat(type), "brick-tabs-size-".concat(size), {
145
+ 'brick-tabs-with-ellipsis': this.tabsEllipsis.length
146
+ }, className);
147
+ }
148
+ }, {
149
+ key: "size",
150
+ get: function get() {
151
+ if (!_includesInstanceProperty(SIZED_TYPES).call(SIZED_TYPES, this.props.type)) {
152
+ return 'md';
153
+ }
154
+ return getDefaultValueUsingContextTheme(this, 'size', 'baseSize');
155
+ }
156
+ }, {
157
+ key: "contextValue",
158
+ get: function get() {
159
+ return {
160
+ activeId: this.state.activeId
161
+ };
162
+ }
163
+ }, {
164
+ key: "validChildren",
165
+ get: function get() {
166
+ var _context2;
167
+ var children = this.props.children;
168
+ if (!children) {
169
+ return null;
170
+ }
171
+ var validChildren = _filterInstanceProperty(_context2 = Children.toArray(children)).call(_context2, function (child) {
172
+ return /*#__PURE__*/isValidElement(child) && child.type === TabPane;
173
+ });
174
+ return validChildren.length ? validChildren : null;
175
+ }
176
+
177
+ // 全部的 tabs, 主要提供给
178
+ // - tabs-container-shadow
179
+ // - isTabPaneDisabled 判断用
180
+ }, {
181
+ key: "tabsAll",
182
+ get: function get() {
183
+ var panes = this.props.panes;
184
+ var validChildren = this.validChildren;
185
+ if (validChildren) {
186
+ return _mapInstanceProperty(validChildren).call(validChildren, function (child) {
187
+ return pick(child.props, ['id', 'label', 'disabled', 'deletable', 'status']);
188
+ });
189
+ }
190
+ return panes || [];
191
+ }
192
+
193
+ // 收到下拉里的 tabs
194
+ }, {
195
+ key: "tabsEllipsis",
196
+ get: function get() {
197
+ var _context3;
198
+ var tabs = this.tabsAll;
199
+ var from = this.state.visibleFrom || 0;
200
+ var to = this.state.visibleTo || tabs.length - 1;
201
+ var prevTabs = _sliceInstanceProperty(tabs).call(tabs, 0, from);
202
+ var afterTabs = _sliceInstanceProperty(tabs).call(tabs, to + 1, tabs.length);
203
+ return _concatInstanceProperty(_context3 = []).call(_context3, _toConsumableArray(prevTabs), _toConsumableArray(afterTabs));
204
+ }
205
+ }, {
206
+ key: "canEllipsis",
207
+ get: function get() {
208
+ var _context4;
209
+ var _ref2 = this.props,
210
+ type = _ref2.type;
211
+ return _includesInstanceProperty(_context4 = ['plain', 'card']).call(_context4, type);
212
+ }
213
+ }, {
214
+ key: "withEllipsis",
215
+ get: function get() {
216
+ return !!this.tabsEllipsis.length;
217
+ }
218
+ }, {
219
+ key: "isTabPaneDisabled",
220
+ value: function isTabPaneDisabled(id) {
221
+ var _context5, _context6;
222
+ return _someInstanceProperty(_context5 = _filterInstanceProperty(_context6 = this.tabsAll).call(_context6, function (tab) {
223
+ return tab.disabled;
224
+ })).call(_context5, function (tab) {
225
+ return tab.id === id;
226
+ });
227
+ }
228
+ }, {
229
+ key: "updateWidth",
230
+ value: function updateWidth() {
231
+ // 考虑初始 display none 没有宽度、tab 宽度变化的情况
232
+ // 暂不提前优化计算时机
233
+ var $tabsWrap = this.refTabsWrap.current;
234
+ var $tabs = this.refTabs.current;
235
+ if ($tabsWrap && $tabs) {
236
+ var _context7;
237
+ this.tabsWrapWidth = $tabsWrap.getBoundingClientRect().width;
238
+ // .brick-tabs-wrap 因为 overflow: hidden
239
+ // 估四周给了一个边缘扩大(padding: x; margin: -x),以使 focus-ring 可以露出来,
240
+ // 详见 style.css 的 .brick-tabs-wrap 的修饰
241
+ // 最大滚动补正 4 * 2
242
+ var overlapMargin = 4 * 2 - BORDER_WIDTH;
243
+ this.tabsWrapMaxScrollLeft = $tabs.getBoundingClientRect().width - this.tabsWrapWidth + overlapMargin;
244
+ this.tabsWidth = _mapInstanceProperty(_context7 = _Array$from($tabs.querySelectorAll('li'))).call(_context7, function (tab) {
245
+ return tab.getBoundingClientRect().width;
246
+ });
247
+ }
248
+ }
249
+ }, {
250
+ key: "updateEllipsis",
251
+ value: function updateEllipsis() {
252
+ var _this$state = this.state,
253
+ visibleFrom = _this$state.visibleFrom,
254
+ visibleTo = _this$state.visibleTo;
255
+ var tabsCount = this.tabsAll.length;
256
+ if (!this.canEllipsis || !tabsCount) {
257
+ return;
258
+ }
259
+ this.updateWidth();
260
+ var visibleLeft = this.tabsWrapScrollLeft;
261
+ var visibleRight = visibleLeft + this.tabsWrapWidth;
262
+ var borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0;
263
+ var from = -1;
264
+ var to = 0;
265
+ var i = -1;
266
+ var currentLeft = 0;
267
+ var currentRight = 0;
268
+ while (++i < tabsCount) {
269
+ if (currentLeft > visibleRight) {
270
+ break;
271
+ }
272
+ currentRight += this.tabsWidth[i] - borderWidth;
273
+ if (currentRight < visibleLeft) {
274
+ continue;
275
+ }
276
+ var visible = currentLeft >= visibleLeft && currentRight <= visibleRight;
277
+ if (visible) {
278
+ from = from < 0 ? i : from;
279
+ to = i;
280
+ }
281
+ currentLeft = currentRight;
282
+ }
283
+ from = from < 0 ? 0 : from;
284
+ if (visibleFrom !== from || visibleTo !== to) {
285
+ this.setState({
286
+ visibleFrom: from,
287
+ visibleTo: to
288
+ });
289
+ }
290
+ }
291
+ }, {
292
+ key: "changeActive",
293
+ value: function changeActive(id) {
294
+ if (this.state.activeId === id || this.isTabPaneDisabled(id)) {
295
+ return;
296
+ }
297
+ if (isUndefined(this.props.activeId)) {
298
+ this.setState({
299
+ activeId: id
300
+ });
301
+ }
302
+ safeInvoke(this.props.onChange, id);
303
+ }
304
+ }, {
305
+ key: "deleteTab",
306
+ value: function deleteTab(id) {
307
+ safeInvoke(this.props.onTabDelete, id);
308
+ }
309
+ }, {
310
+ key: "scrollTabsWrap",
311
+ value: function scrollTabsWrap() {
312
+ var scrollLeftTo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.tabsWrapScrollLeft;
313
+ var scrollLeft = Math.min(this.tabsWrapMaxScrollLeft, scrollLeftTo);
314
+ scrollLeft = Math.max(0, scrollLeft);
315
+ if (scrollLeft !== this.tabsWrapScrollLeft) {
316
+ this.refTabs.current.style.transform = "translate(-".concat(scrollLeft, "px, 0px)");
317
+ this.tabsWrapScrollLeft = scrollLeft;
318
+ this.updateEllipsis();
319
+ }
320
+ }
321
+ }, {
322
+ key: "scrollTabsWrapToTab",
323
+ value: function scrollTabsWrapToTab(id) {
324
+ var _context8, _context9, _context10;
325
+ var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
326
+ if (!this.refTabs.current || !this.canEllipsis) {
327
+ return;
328
+ }
329
+ var _this$state2 = this.state,
330
+ visibleFrom = _this$state2.visibleFrom,
331
+ visibleTo = _this$state2.visibleTo;
332
+ var activeIndex = id ? _findIndexInstanceProperty(_context8 = this.tabsAll).call(_context8, function (tab) {
333
+ return tab.id === id;
334
+ }) : 0;
335
+
336
+ // 在可视区域内,不滚动,除非强制(比如 window resize 时)
337
+ if (!force && activeIndex >= visibleFrom && activeIndex <= visibleTo) {
338
+ return;
339
+ }
340
+ var borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0;
341
+ var right = this.tabsAll.length - 1;
342
+ var left = 0;
343
+ var totalWidth = this.tabsWidth[activeIndex];
344
+ var from = activeIndex;
345
+ var to = activeIndex;
346
+
347
+ // from ... active ... to
348
+ // 从 active 向两侧延展,以确定 from 进行,scroll
349
+ // scroll 时虽然不会用到 to ,但 to 会影响 from 的确定,因为当切换时,希望以 active 为中心,尽量显示左右两侧
350
+ while (totalWidth < this.tabsWrapWidth && (from > left || to < right)) {
351
+ // 先扩展右边
352
+ if (to < right) {
353
+ var tempWidth = totalWidth + this.tabsWidth[++to] - borderWidth;
354
+ if (tempWidth > this.tabsWrapWidth) {
355
+ to--;
356
+ break;
357
+ }
358
+ totalWidth = tempWidth;
359
+ }
360
+ if (from > left) {
361
+ var _tempWidth = totalWidth + this.tabsWidth[--from] - borderWidth;
362
+ if (_tempWidth > this.tabsWrapWidth) {
363
+ from++;
364
+ break;
365
+ }
366
+ totalWidth = _tempWidth;
367
+ }
368
+ }
369
+ var scrollLeftTo = _reduceInstanceProperty(_context9 = _sliceInstanceProperty(_context10 = this.tabsWidth).call(_context10, 0, from)).call(_context9, function (accumulator, currentValue) {
370
+ return accumulator + currentValue;
371
+ }, 0);
372
+ this.scrollTabsWrap(scrollLeftTo);
373
+ }
374
+
375
+ // 滚动到可视区域
376
+ }, {
377
+ key: "scrollTabsWrapToActive",
378
+ value: function scrollTabsWrapToActive() {
379
+ var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
380
+ var activeId = this.state.activeId;
381
+ activeId && this.scrollTabsWrapToTab(activeId, force);
382
+ }
383
+ }, {
384
+ key: "renderTabDelete",
385
+ value: function renderTabDelete(tab) {
386
+ // 仅 type card 只支持添加、删除
387
+ if (this.props.type !== 'card' || !tab.deletable) {
388
+ return null;
389
+ }
390
+
391
+ // 点击由 handleClickTab 代理
392
+ return /*#__PURE__*/_jsx(CloseX, {
393
+ className: 'brick-tabs-tab-delete',
394
+ size: 'sm'
395
+ });
396
+ }
397
+ }, {
398
+ key: "renderTabStatus",
399
+ value: function renderTabStatus(tab) {
400
+ // 仅 type card 支持
401
+ if (this.props.type !== 'card' || tab.disabled) {
402
+ return null;
403
+ }
404
+
405
+ // 暂时只有 error 状态
406
+ return tab.status === 'error' && /*#__PURE__*/_jsx(Icon, {
407
+ className: 'brick-tabs-tab-status-icon',
408
+ svg: SvgSignExclamation
409
+ });
410
+ }
411
+ }, {
412
+ key: "renderTabs",
413
+ value: function renderTabs() {
414
+ var _context11;
415
+ return /*#__PURE__*/_jsx("div", {
416
+ className: 'brick-tabs-wrap',
417
+ ref: this.refTabsWrap,
418
+ children: /*#__PURE__*/_jsx("ul", {
419
+ ref: this.refTabs,
420
+ children: _mapInstanceProperty(_context11 = this.tabsAll).call(_context11, this.renderTab)
421
+ })
422
+ });
423
+ }
424
+ }, {
425
+ key: "renderTabsEllipsisMenu",
426
+ value: function renderTabsEllipsisMenu() {
427
+ var _context12,
428
+ _this2 = this;
429
+ // 只有 type card 支持状态
430
+ var isTypeCard = this.props.type === 'card';
431
+ return /*#__PURE__*/_jsx(Menu, {
432
+ type: 'common',
433
+ children: _mapInstanceProperty(_context12 = this.tabsEllipsis).call(_context12, function (tab) {
434
+ var className = c('brick-tabs-ellipsis-item', isTypeCard && tab.status && "brick-tabs-ellipsis-item-status-".concat(tab.status));
435
+ return /*#__PURE__*/_jsx(MenuItem, {
436
+ className: className,
437
+ onClick: function onClick(e) {
438
+ _this2.handleClickTab(e, tab.id);
439
+ },
440
+ suffix: _this2.renderTabDelete(tab),
441
+ disabled: tab.disabled,
442
+ children: tab.label
443
+ }, String(tab.id));
444
+ })
445
+ });
446
+ }
447
+ }, {
448
+ key: "renderTabsEllipsis",
449
+ value: function renderTabsEllipsis() {
450
+ if (!this.tabsEllipsis.length) {
451
+ return null;
452
+ }
453
+ var targetNode = /*#__PURE__*/_jsx("div", {
454
+ className: 'brick-tabs-ellipsis',
455
+ children: /*#__PURE__*/_jsx(Icon, {
456
+ svg: this.state.isEllipsisPopperVisible ? SvgPlainUp : SvgPlainDown
457
+ })
458
+ });
459
+ return /*#__PURE__*/_jsx(MenuPopper, {
460
+ className: 'brick-tabs-ellipsis-dropdown-popper',
461
+ size: this.size,
462
+ targetNode: targetNode,
463
+ placement: this.props.ellipsisDropdownPlacement,
464
+ type: this.props.triggerType,
465
+ onChange: this.handleMenuPopperChange,
466
+ children: this.renderTabsEllipsisMenu()
467
+ });
468
+ }
469
+ }, {
470
+ key: "renderAddButton",
471
+ value: function renderAddButton() {
472
+ var _this$props = this.props,
473
+ type = _this$props.type,
474
+ onClickAdd = _this$props.onClickAdd;
475
+
476
+ // 仅 type card 支持添加、删除
477
+ if (type !== 'card' || !onClickAdd) {
478
+ return null;
479
+ }
480
+ return /*#__PURE__*/_jsx("div", {
481
+ className: 'brick-tabs-add',
482
+ children: /*#__PURE__*/_jsx(Button, {
483
+ className: 'brick-tabs-add-button',
484
+ onClick: this.handleClickAdd,
485
+ type: 'plain',
486
+ color: 'primary',
487
+ size: 'md',
488
+ icon: SvgMarkPlus
489
+ })
490
+ });
491
+ }
492
+ }, {
493
+ key: "renderButtonGroupTabs",
494
+ value: function renderButtonGroupTabs() {
495
+ var _context13;
496
+ var type = this.props.type;
497
+ var value = this.state.activeId;
498
+ var options = _mapInstanceProperty(_context13 = this.tabsAll).call(_context13, function (child) {
499
+ var id = child.id,
500
+ label = child.label,
501
+ disabled = child.disabled;
502
+ return {
503
+ value: id,
504
+ label: label,
505
+ disabled: disabled
506
+ };
507
+ });
508
+ return /*#__PURE__*/_jsx(RadioGroup, {
509
+ className: 'brick-tabs-group',
510
+ type: type === 'button-group-important' ? 'important' : 'intensive',
511
+ size: this.size,
512
+ value: value,
513
+ options: options,
514
+ onChange: this.handleChangeRadioGroup
515
+ });
516
+ }
517
+ }, {
518
+ key: "renderTabsContainer",
519
+ value: function renderTabsContainer() {
520
+ var _this$props$type;
521
+ if ((_this$props$type = this.props.type) !== null && _this$props$type !== void 0 && _includesInstanceProperty(_this$props$type).call(_this$props$type, 'button-group')) {
522
+ return /*#__PURE__*/_jsx("div", {
523
+ className: 'brick-tabs-container',
524
+ children: this.renderButtonGroupTabs()
525
+ });
526
+ }
527
+ return /*#__PURE__*/_jsxs("div", {
528
+ className: 'brick-tabs-container',
529
+ children: [this.renderTabs(), this.renderTabsEllipsis(), this.renderAddButton()]
530
+ });
531
+ }
532
+ }, {
533
+ key: "renderTabPanes",
534
+ value: function renderTabPanes() {
535
+ var _this3 = this;
536
+ var _this$props2 = this.props,
537
+ panes = _this$props2.panes,
538
+ shouldDestroyInactivePane = _this$props2.shouldDestroyInactivePane;
539
+ var validChildren = this.validChildren;
540
+ if (validChildren) {
541
+ if (shouldDestroyInactivePane) {
542
+ return _filterInstanceProperty(validChildren).call(validChildren, function (tab) {
543
+ return _this3.state.activeId === tab.props.id;
544
+ });
545
+ }
546
+ return validChildren;
547
+ }
548
+ if (!panes || !panes.length) {
549
+ return null;
550
+ }
551
+ var panesResult = panes;
552
+ if (shouldDestroyInactivePane) {
553
+ panesResult = _filterInstanceProperty(panes).call(panes, function (tab) {
554
+ return _this3.state.activeId === tab.id;
555
+ });
556
+ }
557
+ return _mapInstanceProperty(panesResult).call(panesResult, function (tab) {
558
+ var content = tab.content,
559
+ tabProps = _objectWithoutProperties(tab, _excluded);
560
+ return /*#__PURE__*/_jsx(TabPane, _objectSpread(_objectSpread({}, tabProps), {}, {
561
+ children: content
562
+ }), String(tab.id));
563
+ });
564
+ }
565
+ }, {
566
+ key: "renderTabPanesContainer",
567
+ value: function renderTabPanesContainer() {
568
+ var panes = this.renderTabPanes();
569
+ return panes ? /*#__PURE__*/_jsx("div", {
570
+ className: 'brick-tabs-panes-container',
571
+ children: panes
572
+ }) : null;
573
+ }
574
+ }, {
575
+ key: "componentDidMount",
576
+ value: function componentDidMount() {
577
+ var _this4 = this;
578
+ this.updateEllipsis();
579
+ this.windowResizeHandler = addEventListener(window, 'resize', function () {
580
+ _this4.updateEllipsis();
581
+ // 需要强制滚动否则
582
+ // - 除了 active 会消失在可视区,
583
+ // - 更不能接受的是,如最后一个 tab active,wrap 宽度增大将使 tabs 和 ellipsis 之间出现空白
584
+ _this4.scrollTabsWrapToActive(true);
585
+ });
586
+ if (this.refTabsWrap.current) {
587
+ // react onWheel 事件无法设置 passive: false,而不能 `e.preventDefault()`
588
+ this.tabsWheelHandler = addEventListener(this.refTabsWrap.current, 'wheel', this.handleWheelTabsWrap, {
589
+ passive: false
590
+ });
591
+ }
592
+ }
593
+ }, {
594
+ key: "componentWillUnmount",
595
+ value: function componentWillUnmount() {
596
+ this.windowResizeHandler && this.windowResizeHandler.remove();
597
+ }
598
+ }, {
599
+ key: "componentDidUpdate",
600
+ value: function componentDidUpdate(prevProps, prevState) {
601
+ this.updateEllipsis();
602
+ if (prevState.activeId !== this.state.activeId) {
603
+ this.scrollTabsWrapToActive();
604
+ }
605
+ }
606
+ }, {
607
+ key: "render",
608
+ value: function render() {
609
+ return /*#__PURE__*/_jsx(TabsContext.Provider, {
610
+ value: this.contextValue,
611
+ children: /*#__PURE__*/_jsxs("div", {
612
+ className: this.className,
613
+ children: [this.renderTabsContainer(), this.renderTabPanesContainer()]
614
+ })
615
+ });
616
+ }
617
+ }], [{
618
+ key: "getDerivedStateFromProps",
619
+ value: function getDerivedStateFromProps(nextProps) {
620
+ return pickDerivedStateFromProps(nextProps, ['activeId']);
621
+ }
622
+ }]);
623
+ return Tabs;
624
+ }(Component);
625
+ _defineProperty(Tabs, "displayName", 'Tabs');
626
+ // static propTypes = {}
627
+ _defineProperty(Tabs, "defaultProps", {
628
+ className: '',
629
+ type: 'plain',
630
+ defaultActiveId: null,
631
+ shouldDestroyInactivePane: false,
632
+ ellipsisDropdownPlacement: 'bottom-end'
633
+ });
634
+ _defineProperty(Tabs, "contextType", ConfigContext);
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@befe/brick-comp-tabs",
3
+ "version": "0.2.92",
4
+ "description": "@befe/brick component Tabs",
5
+ "main": "es",
6
+ "module": "es",
7
+ "types": "types",
8
+ "sideEffects": [
9
+ "*.css",
10
+ "*.scss"
11
+ ],
12
+ "scripts": {
13
+ "test": "node ../../scripts/test",
14
+ "clean": "rm -rf es lib mobx types",
15
+ "build": "npm run clean && npm run build:js && npm run build:dts && npm run build:style",
16
+ "build:dts": "node ../../scripts/build-dts",
17
+ "build:js": "node ../../scripts/build-js",
18
+ "build:style": "node ../../scripts/build-style-component"
19
+ },
20
+ "author": "wujun07",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "@befe/brick-comp-button": "^0.2.80",
24
+ "@befe/brick-comp-close-x": "^0.2.80",
25
+ "@befe/brick-comp-icon": "^0.2.77",
26
+ "@befe/brick-comp-menu": "^0.2.92",
27
+ "@befe/brick-comp-radio": "^0.2.78",
28
+ "@befe/brick-core": "^0.2.42",
29
+ "@befe/brick-icon": "^0.2.22",
30
+ "@befe/brick-style": "^0.2.54",
31
+ "@befe/brick-utils": "^0.2.17"
32
+ },
33
+ "peerDependencies": {
34
+ "classnames": "^2.0.0",
35
+ "lodash-es": "^4.0.0",
36
+ "react": "^16.9.0 || ^17.0.0",
37
+ "react-dom": "^16.9.0 || ^17.0.0"
38
+ },
39
+ "gitHead": "ed610b70dd5ccb910c0edaf32e1521f31d1aa397"
40
+ }
package/src/index.scss ADDED
@@ -0,0 +1,6 @@
1
+ @forward "./style";
2
+ // 依赖组件的样式写在下方,因为需要被 ./style 中的 base 覆盖 theme variable
3
+ @forward "@befe/brick-comp-button/src/index";
4
+ @forward "@befe/brick-comp-icon/src/index";
5
+ @forward "@befe/brick-comp-menu/src/index";
6
+ @forward "@befe/brick-comp-radio/src/index";
package/src/index.tsx ADDED
@@ -0,0 +1,2 @@
1
+ export * from './tabs'
2
+ export * from './tab-pane'