@befe/brick-comp-tabs 0.2.127 → 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/CHANGELOG.md +11 -0
- package/es/index.css +42 -1
- package/es/style.css +5 -0
- package/es/tab-pane.js +23 -62
- package/es/tabs.js +610 -573
- package/package.json +11 -13
- package/src/style.scss +2 -2
- package/src/tab-pane.tsx +25 -41
- package/src/tabs.tsx +516 -440
- package/types/tab-pane.d.ts +11 -13
- package/types/tabs.d.ts +6 -70
package/src/tabs.tsx
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Component,
|
|
3
|
-
ContextType,
|
|
4
|
-
ReactNode,
|
|
5
|
-
createRef,
|
|
6
2
|
MouseEvent,
|
|
3
|
+
ReactNode,
|
|
4
|
+
RefObject,
|
|
5
|
+
PropsWithChildren,
|
|
7
6
|
Children,
|
|
8
7
|
isValidElement,
|
|
9
8
|
ReactComponentElement,
|
|
10
|
-
|
|
9
|
+
useState,
|
|
10
|
+
useRef,
|
|
11
|
+
useEffect,
|
|
11
12
|
} from 'react'
|
|
12
|
-
// import * as PropTypes from 'prop-types'
|
|
13
|
-
import c from 'classnames'
|
|
14
13
|
import {
|
|
14
|
+
cx,
|
|
15
15
|
addEventListener,
|
|
16
16
|
EventListenerHandler,
|
|
17
17
|
ifNodeContains,
|
|
18
18
|
pick,
|
|
19
|
-
pickDerivedStateFromProps,
|
|
20
19
|
safeInvoke,
|
|
20
|
+
isUndefined,
|
|
21
21
|
} from '@befe/brick-utils'
|
|
22
|
-
import {
|
|
23
|
-
import {ConfigContext, getDefaultValueUsingContextTheme} from '@befe/brick-core'
|
|
22
|
+
import {useTheme} from '@befe/brick-core'
|
|
24
23
|
import {Icon} from '@befe/brick-comp-icon'
|
|
25
24
|
import {SvgPlainDown, SvgPlainUp, SvgMarkPlus, SvgSignExclamation} from '@befe/brick-icon'
|
|
26
25
|
import {Menu, MenuItem, MenuPopper, MenuPopperProps} from '@befe/brick-comp-menu'
|
|
27
26
|
import {RadioGroup, RadioGroupProps} from '@befe/brick-comp-radio'
|
|
28
27
|
import {Button} from '@befe/brick-comp-button'
|
|
29
28
|
import {CloseX} from '@befe/brick-comp-close-x'
|
|
30
|
-
import {TabPane, TabPaneProps} from './tab-pane'
|
|
29
|
+
import {TabPane, TabPaneProps, TAB_PANE_DEFAULT_STATUS} from './tab-pane'
|
|
31
30
|
import {TabPaneId, TabsContext, TabsContextValue} from './tabs-context'
|
|
32
31
|
|
|
33
32
|
type ButtonGroupProps = Pick<RadioGroupProps, 'value' | 'options'>
|
|
@@ -112,176 +111,383 @@ export interface TabsProps {
|
|
|
112
111
|
ellipsisDropdownPlacement?: MenuPopperProps['placement']
|
|
113
112
|
}
|
|
114
113
|
|
|
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
114
|
const BORDER_WIDTH = 1
|
|
125
115
|
|
|
126
116
|
const SIZED_TYPES = ['plain', 'tag']
|
|
127
117
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
118
|
+
type TabsType = NonNullable<TabsProps['type']>
|
|
119
|
+
|
|
120
|
+
// 初始(非受控)activeId:受控值优先,其余回落 defaultActiveId,最终兜底 null
|
|
121
|
+
function getInitialActiveId(props: TabsProps): TabPaneId | null {
|
|
122
|
+
return (isUndefined(props.activeId) ? props.defaultActiveId : props.activeId) || null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 仅 plain / tag 受主题 baseSize 影响,其余固定 md
|
|
126
|
+
function resolveSize(props: TabsProps, type: TabsType, baseSize: 'sm' | 'md'): 'sm' | 'md' {
|
|
127
|
+
if (!SIZED_TYPES.includes(type)) {
|
|
128
|
+
return 'md'
|
|
137
129
|
}
|
|
130
|
+
return props.size ?? baseSize
|
|
131
|
+
}
|
|
138
132
|
|
|
139
|
-
|
|
133
|
+
function getValidChildren(children: ReactNode) {
|
|
134
|
+
if (!children) {
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
140
137
|
|
|
141
|
-
|
|
138
|
+
const validChildren = Children.toArray(children).filter(child => {
|
|
139
|
+
return isValidElement(child) && child.type === TabPane
|
|
140
|
+
}) as Array<ReactComponentElement<typeof TabPane>>
|
|
142
141
|
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
return validChildren.length
|
|
143
|
+
? validChildren
|
|
144
|
+
: null
|
|
145
|
+
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
return pickDerivedStateFromProps(nextProps, ['activeId'])
|
|
148
|
-
}
|
|
147
|
+
type ValidChildren = Array<ReactComponentElement<typeof TabPane>> | null
|
|
149
148
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
149
|
+
// resize / wheel 长订阅回调容器(latest-ref)
|
|
150
|
+
interface TabsLongSubscriptions {
|
|
151
|
+
onResize: () => void
|
|
152
|
+
onWheel: (e: WheelEvent) => void
|
|
153
|
+
}
|
|
156
154
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
`brick-tabs-size-${size}`,
|
|
172
|
-
{
|
|
173
|
-
'brick-tabs-with-ellipsis': this.tabsEllipsis.length,
|
|
174
|
-
},
|
|
175
|
-
className
|
|
176
|
-
)
|
|
155
|
+
// 全部的 tabs(提供给 tabs-container-shadow / isTabPaneDisabled 判断用)
|
|
156
|
+
function getTabsAll(panes: TabItem[] | undefined, validChildren: ValidChildren): TabItem[] {
|
|
157
|
+
if (validChildren) {
|
|
158
|
+
return validChildren.map(child => ({
|
|
159
|
+
...pick(child.props, [
|
|
160
|
+
'id',
|
|
161
|
+
'label',
|
|
162
|
+
'disabled',
|
|
163
|
+
'deletable',
|
|
164
|
+
]),
|
|
165
|
+
// TabPane 不再挂 defaultProps(React18 废弃函数组件 defaultProps):
|
|
166
|
+
// 默认参数不进 element.props,故父组件读取时用 TabPane 导出的默认常量兜底
|
|
167
|
+
status: child.props.status ?? TAB_PANE_DEFAULT_STATUS,
|
|
168
|
+
}))
|
|
177
169
|
}
|
|
178
170
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return 'md'
|
|
182
|
-
}
|
|
183
|
-
return getDefaultValueUsingContextTheme(this, 'size', 'baseSize')
|
|
184
|
-
}
|
|
171
|
+
return panes || []
|
|
172
|
+
}
|
|
185
173
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
174
|
+
// 收到下拉里的 tabs
|
|
175
|
+
function getTabsEllipsis(tabsAll: TabItem[], visibleFrom: number, visibleTo: number): TabItem[] {
|
|
176
|
+
const from = visibleFrom || 0
|
|
177
|
+
const to = visibleTo || tabsAll.length - 1
|
|
191
178
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (!children) {
|
|
195
|
-
return null
|
|
196
|
-
}
|
|
179
|
+
const prevTabs = tabsAll.slice(0, from)
|
|
180
|
+
const afterTabs = tabsAll.slice(to + 1, tabsAll.length)
|
|
197
181
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}) as Array<ReactComponentElement<typeof TabPane>>
|
|
182
|
+
return [...prevTabs, ...afterTabs]
|
|
183
|
+
}
|
|
201
184
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
185
|
+
// 删除按钮:结果可能作为 MenuItem 的 suffix 透传,null 与 element 对下游 DOM 不同,
|
|
186
|
+
// 故保持「纯函数返回 ReactNode(null / element)」而非子组件
|
|
187
|
+
function renderTabDelete(type: TabsType, deletable?: boolean): ReactNode {
|
|
188
|
+
// 仅 type card 支持添加、删除
|
|
189
|
+
if (type !== 'card' || !deletable) {
|
|
190
|
+
return null
|
|
205
191
|
}
|
|
206
192
|
|
|
207
|
-
//
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
193
|
+
// 点击由 handleClickTab 代理
|
|
194
|
+
return <CloseX className={'brick-tabs-tab-delete'} size={'sm'} />
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// 渲染上下文:把派生值 + 事件回调收拢,透传给外提的渲染子组件(子组件不产生额外 host DOM,快照不变)
|
|
198
|
+
interface TabsViewCtx {
|
|
199
|
+
type: TabsType
|
|
200
|
+
size: 'sm' | 'md'
|
|
201
|
+
activeId?: TabPaneId | null
|
|
202
|
+
tabsAll: TabItem[]
|
|
203
|
+
tabsEllipsis: TabItem[]
|
|
204
|
+
isEllipsisPopperVisible: boolean
|
|
205
|
+
triggerType: TabsProps['triggerType']
|
|
206
|
+
ellipsisDropdownPlacement: TabsProps['ellipsisDropdownPlacement']
|
|
207
|
+
hasOnClickAdd: boolean
|
|
208
|
+
shouldDestroyInactivePane: boolean
|
|
209
|
+
panes?: TabItem[]
|
|
210
|
+
validChildren: ValidChildren
|
|
211
|
+
refTabsWrap: RefObject<HTMLDivElement>
|
|
212
|
+
refTabs: RefObject<HTMLUListElement>
|
|
213
|
+
onClickTab: (e: MouseEvent, id: TabPaneId) => void
|
|
214
|
+
onChangeActive: (id: TabPaneId) => void
|
|
215
|
+
onFocusTab: (id: TabPaneId) => void
|
|
216
|
+
onClickAdd: (e: MouseEvent) => void
|
|
217
|
+
onChangeRadioGroup: (value: ButtonGroupProps['value']) => void
|
|
218
|
+
onMenuPopperChange: (status: boolean) => void
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function TabItemView({tab, ctx}: {tab: TabItem; ctx: TabsViewCtx}) {
|
|
222
|
+
const {type, activeId, onClickTab, onChangeActive, onFocusTab} = ctx
|
|
223
|
+
|
|
224
|
+
const tabClass = cx(
|
|
225
|
+
'brick-tabs-tab',
|
|
226
|
+
{
|
|
227
|
+
'brick-tabs-tab-active': activeId === tab.id,
|
|
228
|
+
'brick-tabs-tab-disabled': tab.disabled,
|
|
229
|
+
'brick-tabs-tab-deletable': tab.deletable,
|
|
230
|
+
[`brick-tabs-tab-status-${tab.status!}`]: tab.status,
|
|
221
231
|
}
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
<li
|
|
236
|
+
className={tabClass}
|
|
237
|
+
onClick={(e: MouseEvent) => onClickTab(e, tab.id)}
|
|
238
|
+
tabIndex={tab.disabled ? -1 : 0}
|
|
239
|
+
onFocus={() => onFocusTab(tab.id)}
|
|
240
|
+
onKeyDown={e => {
|
|
241
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
242
|
+
onChangeActive(tab.id)
|
|
243
|
+
// 阻止 "空格" 触发的滚动
|
|
244
|
+
e.preventDefault()
|
|
245
|
+
}
|
|
246
|
+
}}
|
|
247
|
+
>
|
|
248
|
+
{type === 'card' && !tab.disabled && tab.status === 'error' && (
|
|
249
|
+
<Icon className={'brick-tabs-tab-status-icon'} svg={SvgSignExclamation} />
|
|
250
|
+
)}
|
|
251
|
+
{tab.label}
|
|
252
|
+
{renderTabDelete(type, tab.deletable)}
|
|
253
|
+
</li>
|
|
254
|
+
)
|
|
255
|
+
}
|
|
222
256
|
|
|
223
|
-
|
|
224
|
-
|
|
257
|
+
function TabList({ctx}: {ctx: TabsViewCtx}) {
|
|
258
|
+
return (
|
|
259
|
+
<div className={'brick-tabs-wrap'} ref={ctx.refTabsWrap}>
|
|
260
|
+
<ul ref={ctx.refTabs}>
|
|
261
|
+
{ctx.tabsAll.map(tab => (
|
|
262
|
+
<TabItemView key={String(tab.id)} tab={tab} ctx={ctx} />
|
|
263
|
+
))}
|
|
264
|
+
</ul>
|
|
265
|
+
</div>
|
|
266
|
+
)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function EllipsisMenu({ctx}: {ctx: TabsViewCtx}) {
|
|
270
|
+
const {type, tabsEllipsis, onClickTab} = ctx
|
|
271
|
+
// 只有 type card 支持状态
|
|
272
|
+
const isTypeCard = type === 'card'
|
|
273
|
+
return (
|
|
274
|
+
<Menu type={'common'}>
|
|
275
|
+
{tabsEllipsis.map((tab) => {
|
|
276
|
+
const className = cx(
|
|
277
|
+
'brick-tabs-ellipsis-item',
|
|
278
|
+
isTypeCard && tab.status && `brick-tabs-ellipsis-item-status-${tab.status}`
|
|
279
|
+
)
|
|
280
|
+
return (
|
|
281
|
+
<MenuItem
|
|
282
|
+
className={className}
|
|
283
|
+
key={String(tab.id)}
|
|
284
|
+
onClick={(e) => onClickTab(e, tab.id)}
|
|
285
|
+
suffix={renderTabDelete(type, tab.deletable)}
|
|
286
|
+
disabled={tab.disabled}
|
|
287
|
+
>
|
|
288
|
+
{tab.label}
|
|
289
|
+
</MenuItem>
|
|
290
|
+
)
|
|
291
|
+
})}
|
|
292
|
+
</Menu>
|
|
293
|
+
)
|
|
294
|
+
}
|
|
225
295
|
|
|
226
|
-
|
|
227
|
-
|
|
296
|
+
function EllipsisDropdown({ctx}: {ctx: TabsViewCtx}) {
|
|
297
|
+
const {
|
|
298
|
+
tabsEllipsis,
|
|
299
|
+
size,
|
|
300
|
+
isEllipsisPopperVisible,
|
|
301
|
+
ellipsisDropdownPlacement,
|
|
302
|
+
triggerType,
|
|
303
|
+
onMenuPopperChange,
|
|
304
|
+
} = ctx
|
|
305
|
+
|
|
306
|
+
if (!tabsEllipsis.length) {
|
|
307
|
+
return null
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const targetNode = (
|
|
311
|
+
<div className={'brick-tabs-ellipsis'}>
|
|
312
|
+
<Icon svg={isEllipsisPopperVisible ? SvgPlainUp : SvgPlainDown} />
|
|
313
|
+
</div>
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
return (
|
|
317
|
+
<MenuPopper
|
|
318
|
+
className={'brick-tabs-ellipsis-dropdown-popper'}
|
|
319
|
+
size={size}
|
|
320
|
+
targetNode={targetNode}
|
|
321
|
+
placement={ellipsisDropdownPlacement}
|
|
322
|
+
type={triggerType}
|
|
323
|
+
onChange={onMenuPopperChange}
|
|
324
|
+
>
|
|
325
|
+
<EllipsisMenu ctx={ctx} />
|
|
326
|
+
</MenuPopper>
|
|
327
|
+
)
|
|
328
|
+
}
|
|
228
329
|
|
|
229
|
-
|
|
330
|
+
function AddButton({ctx}: {ctx: TabsViewCtx}) {
|
|
331
|
+
const {type, hasOnClickAdd, onClickAdd} = ctx
|
|
230
332
|
|
|
231
|
-
|
|
232
|
-
|
|
333
|
+
// 仅 type card 支持添加、删除
|
|
334
|
+
if (type !== 'card' || !hasOnClickAdd) {
|
|
335
|
+
return null
|
|
336
|
+
}
|
|
233
337
|
|
|
234
|
-
|
|
235
|
-
|
|
338
|
+
return (
|
|
339
|
+
<div className={'brick-tabs-add'}>
|
|
340
|
+
<Button
|
|
341
|
+
className={'brick-tabs-add-button'}
|
|
342
|
+
onClick={onClickAdd}
|
|
343
|
+
type={'plain'}
|
|
344
|
+
color={'brand'}
|
|
345
|
+
size={'md'}
|
|
346
|
+
icon={SvgMarkPlus}
|
|
347
|
+
/>
|
|
348
|
+
</div>
|
|
349
|
+
)
|
|
350
|
+
}
|
|
236
351
|
|
|
237
|
-
|
|
352
|
+
function ButtonGroupTabs({ctx}: {ctx: TabsViewCtx}) {
|
|
353
|
+
const {type, size, activeId, tabsAll, onChangeRadioGroup} = ctx
|
|
354
|
+
const value = activeId as ButtonGroupProps['value']
|
|
355
|
+
const options = tabsAll.map(child => {
|
|
356
|
+
const {id, label, disabled} = child
|
|
357
|
+
return {
|
|
358
|
+
value: id,
|
|
359
|
+
label,
|
|
360
|
+
disabled,
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
return (
|
|
365
|
+
<RadioGroup
|
|
366
|
+
className={'brick-tabs-group'}
|
|
367
|
+
type={type === 'button-group-important' ? 'important' : 'intensive'}
|
|
368
|
+
size={size}
|
|
369
|
+
value={value}
|
|
370
|
+
options={options}
|
|
371
|
+
onChange={onChangeRadioGroup}
|
|
372
|
+
/>
|
|
373
|
+
)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function TabsContainerView({ctx}: {ctx: TabsViewCtx}) {
|
|
377
|
+
if (ctx.type.includes('button-group')) {
|
|
378
|
+
return (
|
|
379
|
+
<div className={'brick-tabs-container'}>
|
|
380
|
+
<ButtonGroupTabs ctx={ctx} />
|
|
381
|
+
</div>
|
|
382
|
+
)
|
|
238
383
|
}
|
|
384
|
+
return (
|
|
385
|
+
<div className={'brick-tabs-container'}>
|
|
386
|
+
<TabList ctx={ctx} />
|
|
387
|
+
<EllipsisDropdown ctx={ctx} />
|
|
388
|
+
<AddButton ctx={ctx} />
|
|
389
|
+
</div>
|
|
390
|
+
)
|
|
391
|
+
}
|
|
239
392
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
393
|
+
function getRenderedPanes(ctx: TabsViewCtx): ReactNode {
|
|
394
|
+
const {panes, validChildren, shouldDestroyInactivePane, activeId} = ctx
|
|
395
|
+
if (validChildren) {
|
|
396
|
+
if (shouldDestroyInactivePane) {
|
|
397
|
+
return validChildren.filter(tab => activeId === tab.props.id)
|
|
398
|
+
}
|
|
399
|
+
return validChildren
|
|
243
400
|
}
|
|
244
401
|
|
|
245
|
-
|
|
246
|
-
return
|
|
402
|
+
if (!panes || !panes.length) {
|
|
403
|
+
return null
|
|
247
404
|
}
|
|
248
405
|
|
|
249
|
-
|
|
250
|
-
|
|
406
|
+
let panesResult = panes
|
|
407
|
+
if (shouldDestroyInactivePane) {
|
|
408
|
+
panesResult = panes.filter(tab => activeId === tab.id)
|
|
251
409
|
}
|
|
252
410
|
|
|
253
|
-
|
|
411
|
+
return panesResult.map((tab) => {
|
|
412
|
+
const {content, ...tabProps} = tab
|
|
413
|
+
return <TabPane key={String(tab.id)} {...tabProps}>{content}</TabPane>
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function TabPanesContainerView({ctx}: {ctx: TabsViewCtx}) {
|
|
418
|
+
const panes = getRenderedPanes(ctx)
|
|
419
|
+
|
|
420
|
+
return panes ? (
|
|
421
|
+
<div className={'brick-tabs-panes-container'}>{panes}</div>
|
|
422
|
+
) : null
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
interface UseTabsScrollParams {
|
|
426
|
+
tabsAll: TabItem[]
|
|
427
|
+
type: TabsType
|
|
428
|
+
canEllipsis: boolean
|
|
429
|
+
withEllipsis: boolean
|
|
430
|
+
activeId?: TabPaneId | null
|
|
431
|
+
visibleFrom: number
|
|
432
|
+
visibleTo: number
|
|
433
|
+
setVisibleRange: (range: {from: number; to: number}) => void
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// tabs 省略号测量 + 横向滚动子系统:持有 DOM ref 与测量缓存、管理 resize / wheel 长订阅,
|
|
437
|
+
// 把这组命令式逻辑从组件体里抽出,保持 Tabs 渲染部分精简
|
|
438
|
+
function useTabsScroll(params: UseTabsScrollParams) {
|
|
439
|
+
const {tabsAll, type, canEllipsis, withEllipsis, activeId, visibleFrom, visibleTo, setVisibleRange} = params
|
|
440
|
+
|
|
441
|
+
const refTabsWrap = useRef<HTMLDivElement>(null)
|
|
442
|
+
const refTabs = useRef<HTMLUListElement>(null)
|
|
443
|
+
const tabsWrapWidthRef = useRef(0)
|
|
444
|
+
const tabsWidthRef = useRef<number[]>([])
|
|
445
|
+
const tabsWrapMaxScrollLeftRef = useRef(0)
|
|
446
|
+
const tabsWrapScrollLeftRef = useRef(0)
|
|
447
|
+
|
|
448
|
+
const mountedRef = useRef(false)
|
|
449
|
+
const prevActiveIdRef = useRef(activeId)
|
|
450
|
+
/* istanbul ignore next: 初始 noop 回调在挂载后被 latestRef effect 立即覆盖,二者均不可达 */
|
|
451
|
+
const latestRef = useRef<TabsLongSubscriptions>({
|
|
452
|
+
onResize: () => {/* noop until mounted */},
|
|
453
|
+
onWheel: () => {/* noop until mounted */},
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
// __HOOK_METHODS__
|
|
457
|
+
|
|
458
|
+
const updateWidth = () => {
|
|
254
459
|
// 考虑初始 display none 没有宽度、tab 宽度变化的情况
|
|
255
460
|
// 暂不提前优化计算时机
|
|
256
|
-
const $tabsWrap =
|
|
257
|
-
const $tabs =
|
|
461
|
+
const $tabsWrap = refTabsWrap.current
|
|
462
|
+
const $tabs = refTabs.current
|
|
463
|
+
/* istanbul ignore else: refs 仅在挂载提交后才被测量调用,此处为防御性判空,else 分支 jsdom 不可达 */
|
|
258
464
|
if ($tabsWrap && $tabs) {
|
|
259
|
-
|
|
465
|
+
tabsWrapWidthRef.current = $tabsWrap.getBoundingClientRect().width
|
|
260
466
|
// .brick-tabs-wrap 因为 overflow: hidden
|
|
261
467
|
// 估四周给了一个边缘扩大(padding: x; margin: -x),以使 focus-ring 可以露出来,
|
|
262
468
|
// 详见 style.css 的 .brick-tabs-wrap 的修饰
|
|
263
469
|
// 最大滚动补正 4 * 2
|
|
264
470
|
const overlapMargin = 4 * 2 - BORDER_WIDTH
|
|
265
|
-
|
|
471
|
+
tabsWrapMaxScrollLeftRef.current
|
|
472
|
+
= $tabs.getBoundingClientRect().width - tabsWrapWidthRef.current + overlapMargin
|
|
266
473
|
|
|
267
|
-
|
|
474
|
+
tabsWidthRef.current = Array.from($tabs.querySelectorAll('li')).map((tab: HTMLLIElement) => {
|
|
268
475
|
return tab.getBoundingClientRect().width
|
|
269
476
|
})
|
|
270
477
|
}
|
|
271
478
|
}
|
|
272
479
|
|
|
273
|
-
updateEllipsis() {
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
if (!this.canEllipsis || !tabsCount) {
|
|
480
|
+
const updateEllipsis = () => {
|
|
481
|
+
const tabsCount = tabsAll.length
|
|
482
|
+
if (!canEllipsis || !tabsCount) {
|
|
277
483
|
return
|
|
278
484
|
}
|
|
279
485
|
|
|
280
|
-
|
|
486
|
+
updateWidth()
|
|
281
487
|
|
|
282
|
-
const visibleLeft =
|
|
283
|
-
const visibleRight = visibleLeft +
|
|
284
|
-
const borderWidth =
|
|
488
|
+
const visibleLeft = tabsWrapScrollLeftRef.current
|
|
489
|
+
const visibleRight = visibleLeft + tabsWrapWidthRef.current
|
|
490
|
+
const borderWidth = type === 'card' ? BORDER_WIDTH : 0
|
|
285
491
|
|
|
286
492
|
let from = -1
|
|
287
493
|
let to = 0
|
|
@@ -293,7 +499,7 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
293
499
|
break
|
|
294
500
|
}
|
|
295
501
|
|
|
296
|
-
currentRight +=
|
|
502
|
+
currentRight += tabsWidthRef.current[i] - borderWidth
|
|
297
503
|
if (currentRight < visibleLeft) {
|
|
298
504
|
continue
|
|
299
505
|
}
|
|
@@ -310,48 +516,30 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
310
516
|
from = from < 0 ? 0 : from
|
|
311
517
|
|
|
312
518
|
if (visibleFrom !== from || visibleTo !== to) {
|
|
313
|
-
|
|
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
|
|
519
|
+
setVisibleRange({from, to})
|
|
323
520
|
}
|
|
324
|
-
if (isUndefined(this.props.activeId)) {
|
|
325
|
-
this.setState({
|
|
326
|
-
activeId: id,
|
|
327
|
-
})
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
safeInvoke(this.props.onChange, id)
|
|
331
521
|
}
|
|
332
522
|
|
|
333
|
-
|
|
334
|
-
safeInvoke(this.props.onTabDelete, id)
|
|
335
|
-
}
|
|
523
|
+
// __HOOK_METHODS_2__
|
|
336
524
|
|
|
337
|
-
|
|
338
|
-
|
|
525
|
+
/* istanbul ignore next: 所有调用方都显式传入 scrollLeftTo,默认参数分支不可达 */
|
|
526
|
+
const scrollTabsWrap = (scrollLeftTo: number = tabsWrapScrollLeftRef.current) => {
|
|
527
|
+
let scrollLeft = Math.min(tabsWrapMaxScrollLeftRef.current, scrollLeftTo)
|
|
339
528
|
scrollLeft = Math.max(0, scrollLeft)
|
|
340
529
|
|
|
341
|
-
if (scrollLeft !==
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
530
|
+
if (scrollLeft !== tabsWrapScrollLeftRef.current) {
|
|
531
|
+
refTabs.current!.style.transform = `translate(-${scrollLeft}px, 0px)`
|
|
532
|
+
tabsWrapScrollLeftRef.current = scrollLeft
|
|
533
|
+
updateEllipsis()
|
|
345
534
|
}
|
|
346
535
|
}
|
|
347
536
|
|
|
348
|
-
scrollTabsWrapToTab(id: TabPaneId, force = false) {
|
|
349
|
-
if (!
|
|
537
|
+
const scrollTabsWrapToTab = (id: TabPaneId, force = false) => {
|
|
538
|
+
if (!refTabs.current || !canEllipsis) {
|
|
350
539
|
return
|
|
351
540
|
}
|
|
352
|
-
const {visibleFrom, visibleTo} = this.state
|
|
353
541
|
const activeIndex = id
|
|
354
|
-
?
|
|
542
|
+
? tabsAll.findIndex(tab => tab.id === id)
|
|
355
543
|
: 0
|
|
356
544
|
|
|
357
545
|
// 在可视区域内,不滚动,除非强制(比如 window resize 时)
|
|
@@ -359,21 +547,21 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
359
547
|
return
|
|
360
548
|
}
|
|
361
549
|
|
|
362
|
-
const borderWidth =
|
|
363
|
-
const right =
|
|
550
|
+
const borderWidth = type === 'card' ? BORDER_WIDTH : 0
|
|
551
|
+
const right = tabsAll.length - 1
|
|
364
552
|
const left = 0
|
|
365
|
-
let totalWidth =
|
|
553
|
+
let totalWidth = tabsWidthRef.current[activeIndex]
|
|
366
554
|
let from = activeIndex
|
|
367
555
|
let to = activeIndex
|
|
368
556
|
|
|
369
557
|
// from ... active ... to
|
|
370
558
|
// 从 active 向两侧延展,以确定 from 进行,scroll
|
|
371
559
|
// scroll 时虽然不会用到 to ,但 to 会影响 from 的确定,因为当切换时,希望以 active 为中心,尽量显示左右两侧
|
|
372
|
-
while (totalWidth <
|
|
560
|
+
while (totalWidth < tabsWrapWidthRef.current && (from > left || to < right)) {
|
|
373
561
|
// 先扩展右边
|
|
374
562
|
if (to < right) {
|
|
375
|
-
const tempWidth = totalWidth +
|
|
376
|
-
if (tempWidth >
|
|
563
|
+
const tempWidth = totalWidth + tabsWidthRef.current[++to] - borderWidth
|
|
564
|
+
if (tempWidth > tabsWrapWidthRef.current) {
|
|
377
565
|
to--
|
|
378
566
|
break
|
|
379
567
|
}
|
|
@@ -381,8 +569,8 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
381
569
|
}
|
|
382
570
|
|
|
383
571
|
if (from > left) {
|
|
384
|
-
const tempWidth = totalWidth +
|
|
385
|
-
if (tempWidth >
|
|
572
|
+
const tempWidth = totalWidth + tabsWidthRef.current[--from] - borderWidth
|
|
573
|
+
if (tempWidth > tabsWrapWidthRef.current) {
|
|
386
574
|
from++
|
|
387
575
|
break
|
|
388
576
|
}
|
|
@@ -390,50 +578,20 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
390
578
|
}
|
|
391
579
|
}
|
|
392
580
|
|
|
393
|
-
const scrollLeftTo =
|
|
581
|
+
const scrollLeftTo = tabsWidthRef.current
|
|
394
582
|
.slice(0, from)
|
|
395
583
|
.reduce((accumulator, currentValue) => accumulator + currentValue, 0)
|
|
396
584
|
|
|
397
|
-
|
|
585
|
+
scrollTabsWrap(scrollLeftTo)
|
|
398
586
|
}
|
|
399
587
|
|
|
400
588
|
// 滚动到可视区域
|
|
401
|
-
scrollTabsWrapToActive(force = false) {
|
|
402
|
-
|
|
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)
|
|
589
|
+
const scrollTabsWrapToActive = (force = false) => {
|
|
590
|
+
activeId && scrollTabsWrapToTab(activeId, force)
|
|
427
591
|
}
|
|
428
592
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
isEllipsisPopperVisible: status,
|
|
432
|
-
})
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
handleWheelTabsWrap = (e: WheelEvent) => {
|
|
436
|
-
if (!this.withEllipsis) {
|
|
593
|
+
const handleWheelTabsWrap = (e: WheelEvent) => {
|
|
594
|
+
if (!withEllipsis) {
|
|
437
595
|
return
|
|
438
596
|
}
|
|
439
597
|
|
|
@@ -444,273 +602,191 @@ export class Tabs extends Component<TabsProps, TabsState> {
|
|
|
444
602
|
? deltaX
|
|
445
603
|
: deltaY
|
|
446
604
|
|
|
447
|
-
|
|
605
|
+
scrollTabsWrap(tabsWrapScrollLeftRef.current + scrollLeft)
|
|
448
606
|
|
|
449
607
|
e.preventDefault()
|
|
450
608
|
}
|
|
451
609
|
|
|
452
|
-
|
|
453
|
-
// 仅 type card 只支持添加、删除
|
|
454
|
-
if (this.props.type !== 'card' || !tab.deletable) {
|
|
455
|
-
return null
|
|
456
|
-
}
|
|
610
|
+
// __HOOK_EFFECTS__
|
|
457
611
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (this.props.type !== 'card' || tab.disabled) {
|
|
465
|
-
return null
|
|
612
|
+
// 镜像 componentDidMount + componentDidUpdate 的 updateEllipsis;
|
|
613
|
+
// activeId 变化(且非首次挂载)时追加 scrollTabsWrapToActive(首挂只测量不滚动)
|
|
614
|
+
useEffect(() => {
|
|
615
|
+
updateEllipsis()
|
|
616
|
+
if (mountedRef.current && prevActiveIdRef.current !== activeId) {
|
|
617
|
+
scrollTabsWrapToActive()
|
|
466
618
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
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
|
-
},
|
|
619
|
+
mountedRef.current = true
|
|
620
|
+
prevActiveIdRef.current = activeId
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
// 让长订阅(resize / wheel)回调始终指向最新一次渲染的闭包,规避 [] 订阅捕获过期 state/props
|
|
624
|
+
useEffect(() => {
|
|
625
|
+
latestRef.current.onResize = () => {
|
|
626
|
+
updateEllipsis()
|
|
627
|
+
// 需要强制滚动否则
|
|
628
|
+
// - 除了 active 会消失在可视区,
|
|
629
|
+
// - 更不能接受的是,如最后一个 tab active,wrap 宽度增大将使 tabs 和 ellipsis 之间出现空白
|
|
630
|
+
scrollTabsWrapToActive(true)
|
|
499
631
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
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
|
|
632
|
+
latestRef.current.onWheel = handleWheelTabsWrap
|
|
633
|
+
})
|
|
634
|
+
|
|
635
|
+
useEffect(() => {
|
|
636
|
+
const resizeHandler = addEventListener(window, 'resize', () => latestRef.current.onResize())
|
|
637
|
+
|
|
638
|
+
// react onWheel 事件无法设置 passive: false,而不能 `e.preventDefault()`
|
|
639
|
+
const $tabsWrap = refTabsWrap.current
|
|
640
|
+
let wheelHandler: EventListenerHandler | null = null
|
|
641
|
+
if ($tabsWrap) {
|
|
642
|
+
wheelHandler = addEventListener(
|
|
643
|
+
$tabsWrap,
|
|
644
|
+
'wheel',
|
|
645
|
+
(e) => latestRef.current.onWheel(e as WheelEvent),
|
|
646
|
+
{passive: false}
|
|
647
|
+
)
|
|
551
648
|
}
|
|
552
649
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
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
|
-
}
|
|
650
|
+
return () => {
|
|
651
|
+
resizeHandler && resizeHandler.remove()
|
|
652
|
+
wheelHandler && wheelHandler.remove()
|
|
653
|
+
}
|
|
654
|
+
}, [])
|
|
574
655
|
|
|
575
|
-
|
|
576
|
-
|
|
656
|
+
return {refTabsWrap, refTabs, scrollTabsWrapToTab}
|
|
657
|
+
}
|
|
577
658
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
659
|
+
export function Tabs(props: PropsWithChildren<TabsProps>) {
|
|
660
|
+
const type: TabsType = props.type ?? 'plain'
|
|
661
|
+
const {
|
|
662
|
+
className: customClassName,
|
|
663
|
+
triggerType,
|
|
664
|
+
panes,
|
|
665
|
+
children,
|
|
666
|
+
shouldDestroyInactivePane = false,
|
|
667
|
+
} = props
|
|
668
|
+
const ellipsisDropdownPlacement = props.ellipsisDropdownPlacement ?? 'bottom-end'
|
|
669
|
+
|
|
670
|
+
const [innerActiveId, setInnerActiveId] = useState<TabPaneId | null>(() => getInitialActiveId(props))
|
|
671
|
+
const [visibleRange, setVisibleRange] = useState({from: 0, to: 0})
|
|
672
|
+
const [isEllipsisPopperVisible, setIsEllipsisPopperVisible] = useState(false)
|
|
673
|
+
|
|
674
|
+
const {baseSize} = useTheme()
|
|
675
|
+
const size = resolveSize(props, type, baseSize)
|
|
676
|
+
|
|
677
|
+
// 受控 activeId 优先,否则用内部 state(镜像原 getDerivedStateFromProps)
|
|
678
|
+
const activeId = isUndefined(props.activeId) ? innerActiveId : props.activeId
|
|
679
|
+
|
|
680
|
+
const {from: visibleFrom, to: visibleTo} = visibleRange
|
|
681
|
+
|
|
682
|
+
const validChildren = getValidChildren(children)
|
|
683
|
+
const tabsAll = getTabsAll(panes, validChildren)
|
|
684
|
+
const tabsEllipsis = getTabsEllipsis(tabsAll, visibleFrom, visibleTo)
|
|
685
|
+
const canEllipsis = ['plain', 'card'].includes(type)
|
|
686
|
+
const withEllipsis = !!tabsEllipsis.length
|
|
687
|
+
|
|
688
|
+
const tabsClassName = cx(
|
|
689
|
+
'brick-tabs',
|
|
690
|
+
`brick-tabs-type-${type}`,
|
|
691
|
+
`brick-tabs-size-${size}`,
|
|
692
|
+
{
|
|
693
|
+
'brick-tabs-with-ellipsis': tabsEllipsis.length,
|
|
694
|
+
},
|
|
695
|
+
customClassName
|
|
696
|
+
)
|
|
697
|
+
|
|
698
|
+
const {refTabsWrap, refTabs, scrollTabsWrapToTab} = useTabsScroll({
|
|
699
|
+
tabsAll,
|
|
700
|
+
type,
|
|
701
|
+
canEllipsis,
|
|
702
|
+
withEllipsis,
|
|
703
|
+
activeId,
|
|
704
|
+
visibleFrom,
|
|
705
|
+
visibleTo,
|
|
706
|
+
setVisibleRange,
|
|
707
|
+
})
|
|
708
|
+
|
|
709
|
+
const isTabPaneDisabled = (id: TabPaneId) => {
|
|
710
|
+
return tabsAll.filter(tab => tab.disabled).some(tab => tab.id === id)
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
const changeActive = (id: TabPaneId) => {
|
|
714
|
+
if (activeId === id || isTabPaneDisabled(id)) {
|
|
715
|
+
return
|
|
716
|
+
}
|
|
717
|
+
if (isUndefined(props.activeId)) {
|
|
718
|
+
setInnerActiveId(id)
|
|
581
719
|
}
|
|
582
720
|
|
|
583
|
-
|
|
584
|
-
<div className={'brick-tabs-add'}>
|
|
585
|
-
<Button
|
|
586
|
-
className={'brick-tabs-add-button'}
|
|
587
|
-
onClick={this.handleClickAdd}
|
|
588
|
-
type={'plain'}
|
|
589
|
-
color={'brand'}
|
|
590
|
-
size={'md'}
|
|
591
|
-
icon={SvgMarkPlus}
|
|
592
|
-
/>
|
|
593
|
-
</div>
|
|
594
|
-
)
|
|
721
|
+
safeInvoke(props.onChange, id)
|
|
595
722
|
}
|
|
596
723
|
|
|
597
|
-
|
|
598
|
-
|
|
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
|
-
)
|
|
724
|
+
const deleteTab = (id: TabPaneId) => {
|
|
725
|
+
safeInvoke(props.onTabDelete, id)
|
|
619
726
|
}
|
|
620
727
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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
|
-
)
|
|
728
|
+
const handleClickAdd = (e: MouseEvent) => {
|
|
729
|
+
const nextId = safeInvoke(props.onClickAdd, e)
|
|
730
|
+
nextId && changeActive(nextId)
|
|
636
731
|
}
|
|
637
732
|
|
|
638
|
-
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
if (shouldDestroyInactivePane) {
|
|
643
|
-
return validChildren.filter(tab => {
|
|
644
|
-
return this.state.activeId === tab.props.id
|
|
645
|
-
})
|
|
646
|
-
}
|
|
647
|
-
return validChildren
|
|
733
|
+
const handleClickTab = (e: MouseEvent, id: TabPaneId) => {
|
|
734
|
+
const $removeBtn = (e.currentTarget as HTMLElement).querySelector('.brick-tabs-tab-delete')
|
|
735
|
+
if ($removeBtn && ifNodeContains($removeBtn, e.target as HTMLElement)) {
|
|
736
|
+
deleteTab(id)
|
|
648
737
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
return null
|
|
738
|
+
else {
|
|
739
|
+
changeActive(id)
|
|
652
740
|
}
|
|
653
741
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
panesResult = panes.filter(tab => {
|
|
657
|
-
return this.state.activeId === tab.id
|
|
658
|
-
})
|
|
742
|
+
if (!isTabPaneDisabled(id)) {
|
|
743
|
+
safeInvoke(props.onClickTab, e, id)
|
|
659
744
|
}
|
|
660
|
-
|
|
661
|
-
return panesResult.map((tab) => {
|
|
662
|
-
const {content, ...tabProps} = tab
|
|
663
|
-
return <TabPane key={String(tab.id)} {...tabProps}>{content}</TabPane>
|
|
664
|
-
})
|
|
665
745
|
}
|
|
666
746
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
return panes ? (
|
|
671
|
-
<div className={'brick-tabs-panes-container'}>{panes}</div>
|
|
672
|
-
) : null
|
|
747
|
+
const handleChangeRadioGroup = (value: ButtonGroupProps['value']) => {
|
|
748
|
+
value && changeActive(value)
|
|
673
749
|
}
|
|
674
750
|
|
|
675
|
-
|
|
676
|
-
|
|
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
|
-
}
|
|
751
|
+
const handleMenuPopperChange = (status: boolean) => {
|
|
752
|
+
setIsEllipsisPopperVisible(status)
|
|
693
753
|
}
|
|
694
754
|
|
|
695
|
-
|
|
696
|
-
|
|
755
|
+
const ctx: TabsViewCtx = {
|
|
756
|
+
type,
|
|
757
|
+
size,
|
|
758
|
+
activeId,
|
|
759
|
+
tabsAll,
|
|
760
|
+
tabsEllipsis,
|
|
761
|
+
isEllipsisPopperVisible,
|
|
762
|
+
triggerType,
|
|
763
|
+
ellipsisDropdownPlacement,
|
|
764
|
+
hasOnClickAdd: !isUndefined(props.onClickAdd),
|
|
765
|
+
shouldDestroyInactivePane,
|
|
766
|
+
panes,
|
|
767
|
+
validChildren,
|
|
768
|
+
refTabsWrap,
|
|
769
|
+
refTabs,
|
|
770
|
+
onClickTab: handleClickTab,
|
|
771
|
+
onChangeActive: changeActive,
|
|
772
|
+
onFocusTab: (id) => scrollTabsWrapToTab(id),
|
|
773
|
+
onClickAdd: handleClickAdd,
|
|
774
|
+
onChangeRadioGroup: handleChangeRadioGroup,
|
|
775
|
+
onMenuPopperChange: handleMenuPopperChange,
|
|
697
776
|
}
|
|
698
777
|
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
if (prevState.activeId !== this.state.activeId) {
|
|
702
|
-
this.scrollTabsWrapToActive()
|
|
703
|
-
}
|
|
778
|
+
const contextValue: TabsContextValue = {
|
|
779
|
+
activeId,
|
|
704
780
|
}
|
|
705
781
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
<
|
|
709
|
-
<
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
)
|
|
715
|
-
}
|
|
782
|
+
return (
|
|
783
|
+
<TabsContext.Provider value={contextValue}>
|
|
784
|
+
<div className={tabsClassName}>
|
|
785
|
+
<TabsContainerView ctx={ctx} />
|
|
786
|
+
<TabPanesContainerView ctx={ctx} />
|
|
787
|
+
</div>
|
|
788
|
+
</TabsContext.Provider>
|
|
789
|
+
)
|
|
716
790
|
}
|
|
791
|
+
|
|
792
|
+
Tabs.displayName = 'Tabs'
|