@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/CHANGELOG.md +1155 -0
- package/README.md +3 -0
- package/es/index.css +3391 -0
- package/es/index.js +2 -0
- package/es/style.css +314 -0
- package/es/tab-pane.js +67 -0
- package/es/tabs-context.js +2 -0
- package/es/tabs.js +634 -0
- package/package.json +40 -0
- package/src/index.scss +6 -0
- package/src/index.tsx +2 -0
- package/src/style.scss +360 -0
- package/src/tab-pane.tsx +82 -0
- package/src/tabs-context.tsx +9 -0
- package/src/tabs.tsx +716 -0
- package/types/index.d.ts +2 -0
- package/types/tab-pane.d.ts +42 -0
- package/types/tabs-context.d.ts +6 -0
- package/types/tabs.d.ts +139 -0
package/src/tabs.tsx
ADDED
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
ContextType,
|
|
4
|
+
ReactNode,
|
|
5
|
+
createRef,
|
|
6
|
+
MouseEvent,
|
|
7
|
+
Children,
|
|
8
|
+
isValidElement,
|
|
9
|
+
ReactComponentElement,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
} from 'react'
|
|
12
|
+
// import * as PropTypes from 'prop-types'
|
|
13
|
+
import c from 'classnames'
|
|
14
|
+
import {
|
|
15
|
+
addEventListener,
|
|
16
|
+
EventListenerHandler,
|
|
17
|
+
ifNodeContains,
|
|
18
|
+
pick,
|
|
19
|
+
pickDerivedStateFromProps,
|
|
20
|
+
safeInvoke,
|
|
21
|
+
} from '@befe/brick-utils'
|
|
22
|
+
import {isUndefined} from 'lodash-es'
|
|
23
|
+
import {ConfigContext, getDefaultValueUsingContextTheme} from '@befe/brick-core'
|
|
24
|
+
import {Icon} from '@befe/brick-comp-icon'
|
|
25
|
+
import {SvgPlainDown, SvgPlainUp, SvgMarkPlus, SvgSignExclamation} from '@befe/brick-icon'
|
|
26
|
+
import {Menu, MenuItem, MenuPopper, MenuPopperProps} from '@befe/brick-comp-menu'
|
|
27
|
+
import {RadioGroup, RadioGroupProps} from '@befe/brick-comp-radio'
|
|
28
|
+
import {Button} from '@befe/brick-comp-button'
|
|
29
|
+
import {CloseX} from '@befe/brick-comp-close-x'
|
|
30
|
+
import {TabPane, TabPaneProps} from './tab-pane'
|
|
31
|
+
import {TabPaneId, TabsContext, TabsContextValue} from './tabs-context'
|
|
32
|
+
|
|
33
|
+
type ButtonGroupProps = Pick<RadioGroupProps, 'value' | 'options'>
|
|
34
|
+
|
|
35
|
+
interface TabItem extends TabPaneProps {
|
|
36
|
+
/**
|
|
37
|
+
* tab 页内容
|
|
38
|
+
*/
|
|
39
|
+
content?: ReactNode
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TabsProps {
|
|
43
|
+
/**
|
|
44
|
+
* 自定义 class
|
|
45
|
+
*/
|
|
46
|
+
className?: string
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 尺寸
|
|
50
|
+
* use config context `baseSize` as default
|
|
51
|
+
*/
|
|
52
|
+
size?: 'sm' | 'md'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 标签卡样式类型
|
|
56
|
+
*/
|
|
57
|
+
type?: 'plain' | 'card' | 'tag' | 'button-group-intensive' | 'button-group-important'
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 标签页
|
|
61
|
+
*/
|
|
62
|
+
panes?: TabItem[]
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 激活 tab id 的 控制值
|
|
66
|
+
*/
|
|
67
|
+
activeId?: TabPaneId
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 激活 tab id 的 默认值
|
|
71
|
+
*/
|
|
72
|
+
defaultActiveId?: TabPaneId | null
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* activeId 变化时的回调
|
|
76
|
+
*/
|
|
77
|
+
onChange?: (activeId: TabPaneId) => void
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 点击 tab 的回调
|
|
81
|
+
*
|
|
82
|
+
* type `button-group-intensive` 和 `button-group-important` 无效,因 RadioGroup 没有 onClick 事件
|
|
83
|
+
*/
|
|
84
|
+
onClickTab?: (e: MouseEvent, id: TabPaneId) => void
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 下拉列表触发方式
|
|
88
|
+
*/
|
|
89
|
+
triggerType?: MenuPopperProps['type']
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 是否干掉非当前激活的 pane dom
|
|
93
|
+
*/
|
|
94
|
+
shouldDestroyInactivePane?: boolean
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 添加按钮点击回调
|
|
98
|
+
*
|
|
99
|
+
* 提供回调则显示添加按钮,仅 type 'card' 支持
|
|
100
|
+
*/
|
|
101
|
+
onClickAdd?: (e: MouseEvent) => void | TabPaneId
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* delete 删除tab回调
|
|
105
|
+
*/
|
|
106
|
+
onTabDelete?: (activeId: TabPaneId) => void
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @private 暂不公布,以便于收集需要使用 "非 bottom-end" 场景的普遍率
|
|
110
|
+
* 收起到等多下拉 popper 位置
|
|
111
|
+
*/
|
|
112
|
+
ellipsisDropdownPlacement?: MenuPopperProps['placement']
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface TabsState {
|
|
116
|
+
activeId?: TabPaneId | null
|
|
117
|
+
visibleFrom: number
|
|
118
|
+
visibleTo: number
|
|
119
|
+
isEllipsisPopperVisible: boolean
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type TabsPropsWithDefault = TabsProps & Required<TabsProps> & Pick<TabsProps, keyof typeof Tabs.defaultProps>
|
|
123
|
+
|
|
124
|
+
const BORDER_WIDTH = 1
|
|
125
|
+
|
|
126
|
+
const SIZED_TYPES = ['plain', 'tag']
|
|
127
|
+
|
|
128
|
+
export class Tabs extends Component<TabsProps, TabsState> {
|
|
129
|
+
static displayName = 'Tabs'
|
|
130
|
+
// static propTypes = {}
|
|
131
|
+
static defaultProps = {
|
|
132
|
+
className: '',
|
|
133
|
+
type: 'plain',
|
|
134
|
+
defaultActiveId: null,
|
|
135
|
+
shouldDestroyInactivePane: false,
|
|
136
|
+
ellipsisDropdownPlacement: 'bottom-end',
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static contextType = ConfigContext
|
|
140
|
+
|
|
141
|
+
context!: ContextType<typeof ConfigContext>
|
|
142
|
+
|
|
143
|
+
windowResizeHandler!: EventListenerHandler | null
|
|
144
|
+
tabsWheelHandler: EventListenerHandler | null = null
|
|
145
|
+
|
|
146
|
+
static getDerivedStateFromProps(nextProps: TabsProps) {
|
|
147
|
+
return pickDerivedStateFromProps(nextProps, ['activeId'])
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
state: TabsState = {
|
|
151
|
+
activeId: (isUndefined(this.props.activeId) ? this.props.defaultActiveId : this.props.activeId) || null,
|
|
152
|
+
visibleFrom: 0,
|
|
153
|
+
visibleTo: 0,
|
|
154
|
+
isEllipsisPopperVisible: false,
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
refTabsWrap = createRef<HTMLDivElement>()
|
|
158
|
+
refTabs = createRef<HTMLUListElement>()
|
|
159
|
+
|
|
160
|
+
tabsWrapWidth = 0
|
|
161
|
+
tabsWidth: number[] = []
|
|
162
|
+
tabsWrapMaxScrollLeft = 0
|
|
163
|
+
tabsWrapScrollLeft = 0
|
|
164
|
+
|
|
165
|
+
get className() {
|
|
166
|
+
const {className, type} = this.props as TabsPropsWithDefault
|
|
167
|
+
const size = this.size
|
|
168
|
+
return c(
|
|
169
|
+
'brick-tabs',
|
|
170
|
+
`brick-tabs-type-${type}`,
|
|
171
|
+
`brick-tabs-size-${size}`,
|
|
172
|
+
{
|
|
173
|
+
'brick-tabs-with-ellipsis': this.tabsEllipsis.length,
|
|
174
|
+
},
|
|
175
|
+
className
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get size() {
|
|
180
|
+
if (!SIZED_TYPES.includes((this.props as TabsPropsWithDefault).type)) {
|
|
181
|
+
return 'md'
|
|
182
|
+
}
|
|
183
|
+
return getDefaultValueUsingContextTheme(this, 'size', 'baseSize')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
get contextValue(): TabsContextValue {
|
|
187
|
+
return {
|
|
188
|
+
activeId: this.state.activeId,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
get validChildren() {
|
|
193
|
+
const {children} = this.props
|
|
194
|
+
if (!children) {
|
|
195
|
+
return null
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const validChildren = Children.toArray(children).filter(child => {
|
|
199
|
+
return isValidElement(child) && child.type === TabPane
|
|
200
|
+
}) as Array<ReactComponentElement<typeof TabPane>>
|
|
201
|
+
|
|
202
|
+
return validChildren.length
|
|
203
|
+
? validChildren
|
|
204
|
+
: null
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 全部的 tabs, 主要提供给
|
|
208
|
+
// - tabs-container-shadow
|
|
209
|
+
// - isTabPaneDisabled 判断用
|
|
210
|
+
get tabsAll(): TabItem[] {
|
|
211
|
+
const {panes} = this.props
|
|
212
|
+
const validChildren = this.validChildren
|
|
213
|
+
if (validChildren) {
|
|
214
|
+
return validChildren.map(child => pick(child.props, [
|
|
215
|
+
'id',
|
|
216
|
+
'label',
|
|
217
|
+
'disabled',
|
|
218
|
+
'deletable',
|
|
219
|
+
'status',
|
|
220
|
+
]))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return panes || []
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// 收到下拉里的 tabs
|
|
227
|
+
get tabsEllipsis(): TabItem[] {
|
|
228
|
+
|
|
229
|
+
const tabs = this.tabsAll
|
|
230
|
+
|
|
231
|
+
const from = this.state.visibleFrom || 0
|
|
232
|
+
const to = this.state.visibleTo || tabs.length - 1
|
|
233
|
+
|
|
234
|
+
const prevTabs = tabs.slice(0, from)
|
|
235
|
+
const afterTabs = tabs.slice(to + 1, tabs.length)
|
|
236
|
+
|
|
237
|
+
return [...prevTabs, ...afterTabs]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
get canEllipsis() {
|
|
241
|
+
const {type} = this.props as TabsPropsWithDefault
|
|
242
|
+
return ['plain', 'card'].includes(type);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
get withEllipsis() {
|
|
246
|
+
return !!this.tabsEllipsis.length
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
isTabPaneDisabled(id: TabPaneId) {
|
|
250
|
+
return this.tabsAll.filter(tab => tab.disabled).some(tab => tab.id === id)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
updateWidth() {
|
|
254
|
+
// 考虑初始 display none 没有宽度、tab 宽度变化的情况
|
|
255
|
+
// 暂不提前优化计算时机
|
|
256
|
+
const $tabsWrap = this.refTabsWrap.current
|
|
257
|
+
const $tabs = this.refTabs.current
|
|
258
|
+
if ($tabsWrap && $tabs) {
|
|
259
|
+
this.tabsWrapWidth = $tabsWrap.getBoundingClientRect().width
|
|
260
|
+
// .brick-tabs-wrap 因为 overflow: hidden
|
|
261
|
+
// 估四周给了一个边缘扩大(padding: x; margin: -x),以使 focus-ring 可以露出来,
|
|
262
|
+
// 详见 style.css 的 .brick-tabs-wrap 的修饰
|
|
263
|
+
// 最大滚动补正 4 * 2
|
|
264
|
+
const overlapMargin = 4 * 2 - BORDER_WIDTH
|
|
265
|
+
this.tabsWrapMaxScrollLeft = $tabs.getBoundingClientRect().width - this.tabsWrapWidth + overlapMargin
|
|
266
|
+
|
|
267
|
+
this.tabsWidth = Array.from($tabs.querySelectorAll('li')).map((tab: HTMLLIElement) => {
|
|
268
|
+
return tab.getBoundingClientRect().width
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
updateEllipsis() {
|
|
274
|
+
const {visibleFrom, visibleTo} = this.state
|
|
275
|
+
const tabsCount = this.tabsAll.length
|
|
276
|
+
if (!this.canEllipsis || !tabsCount) {
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
this.updateWidth()
|
|
281
|
+
|
|
282
|
+
const visibleLeft = this.tabsWrapScrollLeft
|
|
283
|
+
const visibleRight = visibleLeft + this.tabsWrapWidth
|
|
284
|
+
const borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0
|
|
285
|
+
|
|
286
|
+
let from = -1
|
|
287
|
+
let to = 0
|
|
288
|
+
let i = -1
|
|
289
|
+
let currentLeft = 0
|
|
290
|
+
let currentRight = 0
|
|
291
|
+
while (++i < tabsCount) {
|
|
292
|
+
if (currentLeft > visibleRight) {
|
|
293
|
+
break
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
currentRight += this.tabsWidth[i] - borderWidth
|
|
297
|
+
if (currentRight < visibleLeft) {
|
|
298
|
+
continue
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const visible = currentLeft >= visibleLeft && currentRight <= visibleRight
|
|
302
|
+
if (visible) {
|
|
303
|
+
from = from < 0 ? i : from
|
|
304
|
+
to = i
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
currentLeft = currentRight
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
from = from < 0 ? 0 : from
|
|
311
|
+
|
|
312
|
+
if (visibleFrom !== from || visibleTo !== to) {
|
|
313
|
+
this.setState({
|
|
314
|
+
visibleFrom: from,
|
|
315
|
+
visibleTo: to,
|
|
316
|
+
})
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
changeActive(id: TabPaneId) {
|
|
321
|
+
if (this.state.activeId === id || this.isTabPaneDisabled(id)) {
|
|
322
|
+
return
|
|
323
|
+
}
|
|
324
|
+
if (isUndefined(this.props.activeId)) {
|
|
325
|
+
this.setState({
|
|
326
|
+
activeId: id,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
safeInvoke(this.props.onChange, id)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
deleteTab(id: TabPaneId) {
|
|
334
|
+
safeInvoke(this.props.onTabDelete, id)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
scrollTabsWrap(scrollLeftTo: number = this.tabsWrapScrollLeft) {
|
|
338
|
+
let scrollLeft = Math.min(this.tabsWrapMaxScrollLeft, scrollLeftTo)
|
|
339
|
+
scrollLeft = Math.max(0, scrollLeft)
|
|
340
|
+
|
|
341
|
+
if (scrollLeft !== this.tabsWrapScrollLeft) {
|
|
342
|
+
this.refTabs.current!.style.transform = `translate(-${scrollLeft}px, 0px)`
|
|
343
|
+
this.tabsWrapScrollLeft = scrollLeft
|
|
344
|
+
this.updateEllipsis()
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
scrollTabsWrapToTab(id: TabPaneId, force = false) {
|
|
349
|
+
if (!this.refTabs.current || !this.canEllipsis) {
|
|
350
|
+
return
|
|
351
|
+
}
|
|
352
|
+
const {visibleFrom, visibleTo} = this.state
|
|
353
|
+
const activeIndex = id
|
|
354
|
+
? this.tabsAll.findIndex(tab => tab.id === id)
|
|
355
|
+
: 0
|
|
356
|
+
|
|
357
|
+
// 在可视区域内,不滚动,除非强制(比如 window resize 时)
|
|
358
|
+
if (!force && activeIndex >= visibleFrom && activeIndex <= visibleTo) {
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const borderWidth = this.props.type === 'card' ? BORDER_WIDTH : 0
|
|
363
|
+
const right = this.tabsAll.length - 1
|
|
364
|
+
const left = 0
|
|
365
|
+
let totalWidth = this.tabsWidth[activeIndex]
|
|
366
|
+
let from = activeIndex
|
|
367
|
+
let to = activeIndex
|
|
368
|
+
|
|
369
|
+
// from ... active ... to
|
|
370
|
+
// 从 active 向两侧延展,以确定 from 进行,scroll
|
|
371
|
+
// scroll 时虽然不会用到 to ,但 to 会影响 from 的确定,因为当切换时,希望以 active 为中心,尽量显示左右两侧
|
|
372
|
+
while (totalWidth < this.tabsWrapWidth && (from > left || to < right)) {
|
|
373
|
+
// 先扩展右边
|
|
374
|
+
if (to < right) {
|
|
375
|
+
const tempWidth = totalWidth + this.tabsWidth[++to] - borderWidth
|
|
376
|
+
if (tempWidth > this.tabsWrapWidth) {
|
|
377
|
+
to--
|
|
378
|
+
break
|
|
379
|
+
}
|
|
380
|
+
totalWidth = tempWidth
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (from > left) {
|
|
384
|
+
const tempWidth = totalWidth + this.tabsWidth[--from] - borderWidth
|
|
385
|
+
if (tempWidth > this.tabsWrapWidth) {
|
|
386
|
+
from++
|
|
387
|
+
break
|
|
388
|
+
}
|
|
389
|
+
totalWidth = tempWidth
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const scrollLeftTo = this.tabsWidth
|
|
394
|
+
.slice(0, from)
|
|
395
|
+
.reduce((accumulator, currentValue) => accumulator + currentValue, 0)
|
|
396
|
+
|
|
397
|
+
this.scrollTabsWrap(scrollLeftTo)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// 滚动到可视区域
|
|
401
|
+
scrollTabsWrapToActive(force = false) {
|
|
402
|
+
const {activeId} = this.state
|
|
403
|
+
activeId && this.scrollTabsWrapToTab(activeId, force)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
handleClickAdd = (e: MouseEvent) => {
|
|
407
|
+
const activeId = safeInvoke(this.props.onClickAdd, e)
|
|
408
|
+
activeId && this.changeActive(activeId)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
handleClickTab = (e: MouseEvent, id: TabPaneId) => {
|
|
412
|
+
const $removeBtn = (e.currentTarget as HTMLElement).querySelector('.brick-tabs-tab-delete')
|
|
413
|
+
if ($removeBtn && ifNodeContains($removeBtn, e.target as HTMLElement)) {
|
|
414
|
+
this.deleteTab(id)
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
this.changeActive(id)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (!this.isTabPaneDisabled(id)) {
|
|
421
|
+
safeInvoke(this.props.onClickTab, e, id);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
handleChangeRadioGroup = (value: ButtonGroupProps['value']) => {
|
|
426
|
+
value && this.changeActive(value)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
handleMenuPopperChange = (status: boolean) => {
|
|
430
|
+
this.setState({
|
|
431
|
+
isEllipsisPopperVisible: status,
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
handleWheelTabsWrap = (e: WheelEvent) => {
|
|
436
|
+
if (!this.withEllipsis) {
|
|
437
|
+
return
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const deltaX = e.deltaX
|
|
441
|
+
const deltaY = e.deltaY
|
|
442
|
+
|
|
443
|
+
const scrollLeft = Math.abs(deltaX) > Math.abs(deltaY)
|
|
444
|
+
? deltaX
|
|
445
|
+
: deltaY
|
|
446
|
+
|
|
447
|
+
this.scrollTabsWrap(this.tabsWrapScrollLeft + scrollLeft)
|
|
448
|
+
|
|
449
|
+
e.preventDefault()
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
renderTabDelete(tab: TabItem) {
|
|
453
|
+
// 仅 type card 只支持添加、删除
|
|
454
|
+
if (this.props.type !== 'card' || !tab.deletable) {
|
|
455
|
+
return null
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// 点击由 handleClickTab 代理
|
|
459
|
+
return <CloseX className={'brick-tabs-tab-delete'} size={'sm'} />
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
renderTabStatus(tab: TabItem) {
|
|
463
|
+
// 仅 type card 支持
|
|
464
|
+
if (this.props.type !== 'card' || tab.disabled) {
|
|
465
|
+
return null
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// 暂时只有 error 状态
|
|
469
|
+
return tab.status === 'error' && <Icon className={'brick-tabs-tab-status-icon'} svg={SvgSignExclamation} />
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
renderTab = (tab: TabItem) => {
|
|
473
|
+
const tabClass = c(
|
|
474
|
+
'brick-tabs-tab',
|
|
475
|
+
{
|
|
476
|
+
'brick-tabs-tab-active': this.state.activeId === tab.id,
|
|
477
|
+
'brick-tabs-tab-disabled': tab.disabled,
|
|
478
|
+
'brick-tabs-tab-deletable': tab.deletable,
|
|
479
|
+
[`brick-tabs-tab-status-${tab.status!}`]: tab.status,
|
|
480
|
+
}
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
const tabProps: HTMLAttributes<HTMLLIElement> = {
|
|
484
|
+
className: tabClass,
|
|
485
|
+
onClick: (e: MouseEvent) => {
|
|
486
|
+
this.handleClickTab(e, tab.id)
|
|
487
|
+
},
|
|
488
|
+
tabIndex: tab.disabled ? -1 : 0,
|
|
489
|
+
onFocus: () => {
|
|
490
|
+
this.scrollTabsWrapToTab(tab.id)
|
|
491
|
+
},
|
|
492
|
+
onKeyDown: e => {
|
|
493
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
494
|
+
this.changeActive(tab.id)
|
|
495
|
+
// 阻止 "空格" 触发的滚动
|
|
496
|
+
e.preventDefault()
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return (
|
|
502
|
+
<li key={String(tab.id)} {...tabProps}>
|
|
503
|
+
{this.renderTabStatus(tab)}
|
|
504
|
+
{tab.label}
|
|
505
|
+
{this.renderTabDelete(tab)}
|
|
506
|
+
</li>
|
|
507
|
+
)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
renderTabs() {
|
|
511
|
+
return (
|
|
512
|
+
<div className={'brick-tabs-wrap'} ref={this.refTabsWrap}>
|
|
513
|
+
<ul ref={this.refTabs}>
|
|
514
|
+
{this.tabsAll.map(this.renderTab)}
|
|
515
|
+
</ul>
|
|
516
|
+
</div>
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
renderTabsEllipsisMenu() {
|
|
521
|
+
// 只有 type card 支持状态
|
|
522
|
+
const isTypeCard = this.props.type === 'card'
|
|
523
|
+
return (
|
|
524
|
+
<Menu type={'common'}>
|
|
525
|
+
{this.tabsEllipsis.map((tab) => {
|
|
526
|
+
const className = c(
|
|
527
|
+
'brick-tabs-ellipsis-item',
|
|
528
|
+
isTypeCard && tab.status && `brick-tabs-ellipsis-item-status-${tab.status}`
|
|
529
|
+
)
|
|
530
|
+
return (
|
|
531
|
+
<MenuItem
|
|
532
|
+
className={className}
|
|
533
|
+
key={String(tab.id)}
|
|
534
|
+
onClick={(e) => {
|
|
535
|
+
this.handleClickTab(e, tab.id)
|
|
536
|
+
}}
|
|
537
|
+
suffix={this.renderTabDelete(tab)}
|
|
538
|
+
disabled={tab.disabled}
|
|
539
|
+
>
|
|
540
|
+
{tab.label}
|
|
541
|
+
</MenuItem>
|
|
542
|
+
)
|
|
543
|
+
})}
|
|
544
|
+
</Menu>
|
|
545
|
+
)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
renderTabsEllipsis() {
|
|
549
|
+
if (!this.tabsEllipsis.length) {
|
|
550
|
+
return null
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const targetNode = (
|
|
554
|
+
<div
|
|
555
|
+
className={'brick-tabs-ellipsis'}
|
|
556
|
+
>
|
|
557
|
+
<Icon svg={this.state.isEllipsisPopperVisible ? SvgPlainUp : SvgPlainDown} />
|
|
558
|
+
</div>
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
return (
|
|
562
|
+
<MenuPopper
|
|
563
|
+
className={'brick-tabs-ellipsis-dropdown-popper'}
|
|
564
|
+
size={this.size}
|
|
565
|
+
targetNode={targetNode}
|
|
566
|
+
placement={this.props.ellipsisDropdownPlacement}
|
|
567
|
+
type={this.props.triggerType}
|
|
568
|
+
onChange={this.handleMenuPopperChange}
|
|
569
|
+
>
|
|
570
|
+
{this.renderTabsEllipsisMenu()}
|
|
571
|
+
</MenuPopper>
|
|
572
|
+
)
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
renderAddButton() {
|
|
576
|
+
const {type, onClickAdd} = this.props
|
|
577
|
+
|
|
578
|
+
// 仅 type card 支持添加、删除
|
|
579
|
+
if (type !== 'card' || !onClickAdd) {
|
|
580
|
+
return null
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
return (
|
|
584
|
+
<div className={'brick-tabs-add'}>
|
|
585
|
+
<Button
|
|
586
|
+
className={'brick-tabs-add-button'}
|
|
587
|
+
onClick={this.handleClickAdd}
|
|
588
|
+
type={'plain'}
|
|
589
|
+
color={'primary'}
|
|
590
|
+
size={'md'}
|
|
591
|
+
icon={SvgMarkPlus}
|
|
592
|
+
/>
|
|
593
|
+
</div>
|
|
594
|
+
)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
renderButtonGroupTabs() {
|
|
598
|
+
const {type} = this.props
|
|
599
|
+
const value = this.state.activeId as ButtonGroupProps['value']
|
|
600
|
+
const options = this.tabsAll.map(child => {
|
|
601
|
+
const {id, label, disabled} = child
|
|
602
|
+
return {
|
|
603
|
+
value: id,
|
|
604
|
+
label,
|
|
605
|
+
disabled,
|
|
606
|
+
}
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
return (
|
|
610
|
+
<RadioGroup
|
|
611
|
+
className={'brick-tabs-group'}
|
|
612
|
+
type={type === 'button-group-important' ? 'important' : 'intensive'}
|
|
613
|
+
size={this.size}
|
|
614
|
+
value={value}
|
|
615
|
+
options={options}
|
|
616
|
+
onChange={this.handleChangeRadioGroup}
|
|
617
|
+
/>
|
|
618
|
+
)
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
renderTabsContainer() {
|
|
622
|
+
if (this.props.type?.includes('button-group')) {
|
|
623
|
+
return (
|
|
624
|
+
<div className={'brick-tabs-container'}>
|
|
625
|
+
{this.renderButtonGroupTabs()}
|
|
626
|
+
</div>
|
|
627
|
+
)
|
|
628
|
+
}
|
|
629
|
+
return (
|
|
630
|
+
<div className={'brick-tabs-container'}>
|
|
631
|
+
{this.renderTabs()}
|
|
632
|
+
{this.renderTabsEllipsis()}
|
|
633
|
+
{this.renderAddButton()}
|
|
634
|
+
</div>
|
|
635
|
+
)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
renderTabPanes() {
|
|
639
|
+
const {panes, shouldDestroyInactivePane} = this.props
|
|
640
|
+
const validChildren = this.validChildren
|
|
641
|
+
if (validChildren) {
|
|
642
|
+
if (shouldDestroyInactivePane) {
|
|
643
|
+
return validChildren.filter(tab => {
|
|
644
|
+
return this.state.activeId === tab.props.id
|
|
645
|
+
})
|
|
646
|
+
}
|
|
647
|
+
return validChildren
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
if (!panes || !panes.length) {
|
|
651
|
+
return null
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
let panesResult = panes
|
|
655
|
+
if (shouldDestroyInactivePane) {
|
|
656
|
+
panesResult = panes.filter(tab => {
|
|
657
|
+
return this.state.activeId === tab.id
|
|
658
|
+
})
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
return panesResult.map((tab) => {
|
|
662
|
+
const {content, ...tabProps} = tab
|
|
663
|
+
return <TabPane key={String(tab.id)} {...tabProps}>{content}</TabPane>
|
|
664
|
+
})
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
renderTabPanesContainer() {
|
|
668
|
+
const panes = this.renderTabPanes()
|
|
669
|
+
|
|
670
|
+
return panes ? (
|
|
671
|
+
<div className={'brick-tabs-panes-container'}>{panes}</div>
|
|
672
|
+
) : null
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
componentDidMount(): void {
|
|
676
|
+
this.updateEllipsis()
|
|
677
|
+
this.windowResizeHandler = addEventListener(window, 'resize', () => {
|
|
678
|
+
this.updateEllipsis()
|
|
679
|
+
// 需要强制滚动否则
|
|
680
|
+
// - 除了 active 会消失在可视区,
|
|
681
|
+
// - 更不能接受的是,如最后一个 tab active,wrap 宽度增大将使 tabs 和 ellipsis 之间出现空白
|
|
682
|
+
this.scrollTabsWrapToActive(true)
|
|
683
|
+
})
|
|
684
|
+
if (this.refTabsWrap.current) {
|
|
685
|
+
// react onWheel 事件无法设置 passive: false,而不能 `e.preventDefault()`
|
|
686
|
+
this.tabsWheelHandler = addEventListener(
|
|
687
|
+
this.refTabsWrap.current,
|
|
688
|
+
'wheel',
|
|
689
|
+
this.handleWheelTabsWrap,
|
|
690
|
+
{passive: false}
|
|
691
|
+
)
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
componentWillUnmount(): void {
|
|
696
|
+
this.windowResizeHandler && this.windowResizeHandler.remove()
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
componentDidUpdate(prevProps: Readonly<TabsProps>, prevState: Readonly<TabsState>) {
|
|
700
|
+
this.updateEllipsis()
|
|
701
|
+
if (prevState.activeId !== this.state.activeId) {
|
|
702
|
+
this.scrollTabsWrapToActive()
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
render() {
|
|
707
|
+
return (
|
|
708
|
+
<TabsContext.Provider value={this.contextValue}>
|
|
709
|
+
<div className={this.className}>
|
|
710
|
+
{this.renderTabsContainer()}
|
|
711
|
+
{this.renderTabPanesContainer()}
|
|
712
|
+
</div>
|
|
713
|
+
</TabsContext.Provider>
|
|
714
|
+
)
|
|
715
|
+
}
|
|
716
|
+
}
|
package/types/index.d.ts
ADDED