@targetprocess/tabs 12.20.1-alpha.4 → 12.21.0

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/Tabs.jsx CHANGED
@@ -1,499 +1,499 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
- import classNames from 'classnames'
4
- import ResizeDetector from 'react-resize-detector'
5
- import ShowMore from './ShowMore'
6
- import {TabPanel} from './TabPanel'
7
- import TabInner from './TabInner'
8
- import styles from './style/index.css'
9
- import {debounce} from '../../base/utils/debounce'
10
- import {TabsPrint} from './TabsPrint'
11
- import {showMorePopoverProps} from './shared-proptype'
12
- import {generateId} from '../../base/utils/ariaUtils'
13
-
14
- const tabPrefix = 'tab-'
15
- const panelPrefix = 'panel-'
16
- const tablistPrefix = 'tablist-'
17
-
18
- // By itself, Tab is just a dummy component that is used for a cleaner Tabs API
19
- // and as marker for actual tabs to prevent storing them as huge array
20
- export const Tab = () => null
21
-
22
- Tab.propTypes = {
23
- header: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
24
- id: PropTypes.string.isRequired,
25
- children: PropTypes.node,
26
- disabled: PropTypes.bool
27
- }
28
-
29
- /**
30
- * Tabs component is an aggregate root for several content panes
31
- * (via Tabs.Tab component), each with its own children, and a header
32
- * to switch between them.
33
- *
34
- * Each `<Tab />` must have a `header` (that can be arbitary react element but
35
- * is most commonly just a string), `id` and regular children.
36
- *
37
- * Top-level `<Tabs />` can work in either controlled or uncontrolled mode.
38
- * In controlled mode, it uses `initialActiveTabId` to determine which
39
- * tab is open by default, and then manually manageschanging tabs
40
- * on appropriate header clicks.
41
- * In uncontrolled mode, it uses `activeTabId` prop to determine which tab
42
- * is open and exposes `onActiveTabChanged` callback that is invoked each time
43
- * a user clicks on a tab.
44
- *
45
- * By default, `<Tabs />` will shrink its tab headers to fit the parent element. Tab
46
- * headers which don't fit into the parent are rendered inside Show More popover.
47
- * In order to disable this behavior and allow `<Tabs/>` to occupy as much space as it needs
48
- * to render all tab headers, set `shrinkToParent` prop to `false`
49
- **/
50
-
51
- export class Tabs extends React.Component {
52
- static propTypes = {
53
- /** Name of some important part of the component */
54
- name: PropTypes.string.isRequired,
55
-
56
- activeTabId: PropTypes.string,
57
- initialActiveTabId: PropTypes.string,
58
- onActiveTabChanged: PropTypes.func,
59
- resizeThrottle: PropTypes.number,
60
- showMoreWidthBuffer: PropTypes.number,
61
- showMorePopoverClassName: PropTypes.string,
62
- showMorePopoverProps: showMorePopoverProps,
63
- tabSizeTimeout: PropTypes.number,
64
- className: PropTypes.string,
65
- shrinkToParent: PropTypes.bool,
66
- printMode: PropTypes.bool,
67
- cacheOpenedTabs: PropTypes.bool
68
- }
69
-
70
- static defaultProps = {
71
- resizeThrottle: 100,
72
- tabSizeTimeout: 200,
73
- showMoreWidthBuffer: 70,
74
- shrinkToParent: true,
75
- printMode: false,
76
- cacheOpenedTabs: false
77
- }
78
-
79
- tablistId = generateId(tablistPrefix)
80
-
81
- tabRefs = new Map()
82
-
83
- activeTabElement = null
84
-
85
- titleWrapper = null
86
-
87
- state = {
88
- activeTabId: this.props.initialActiveTabId,
89
- cachedTabs: new Set(),
90
- sliderLeft: 0,
91
- sliderWidth: 0,
92
- tabDimensions: new Map(),
93
- blockWidth: 0,
94
- tabsTotalWidth: 0,
95
- showMoreWidth: 40,
96
- focusedTabKey: null
97
- }
98
-
99
- static getDerivedStateFromProps(props, state) {
100
- if (!props.cacheOpenedTabs) {
101
- return {
102
- ...state,
103
- cachedTabs: new Set()
104
- }
105
- } else if (typeof props.activeTabId !== 'undefined') {
106
- const cachedTabs = new Set(state.cachedTabs)
107
- cachedTabs.add(state.activeTabId)
108
-
109
- return {
110
- ...state,
111
- activeTabId: props.activeTabId,
112
- cachedTabs
113
- }
114
- }
115
-
116
- return null
117
- }
118
-
119
- isControlledMode = () => typeof this.props.activeTabId !== 'undefined'
120
-
121
- changeTab = tabId => {
122
- const currentActiveTabId = this.getActiveTabId(this.getAllTabs())
123
-
124
- if (this.props.cacheOpenedTabs) {
125
- this.setState(state => {
126
- const cachedTabs = new Set(state.cachedTabs)
127
- cachedTabs.add(currentActiveTabId)
128
-
129
- return {
130
- ...state,
131
- activeTabId: tabId,
132
- cachedTabs
133
- }
134
- })
135
- } else {
136
- this.setState({activeTabId: tabId})
137
- }
138
- if (typeof this.props.onActiveTabChanged === 'function') {
139
- this.props.onActiveTabChanged(tabId)
140
- }
141
- }
142
-
143
- componentDidMount() {
144
- setTimeout(() => {
145
- this.setTabsDimensions()
146
- this.adjustSlider()
147
- }, this.props.tabSizeTimeout)
148
- }
149
-
150
- componentDidUpdate(previousProps, previousState) {
151
- const activeTabChanged = this.isControlledMode()
152
- ? this.props.activeTabId !== previousProps.activeTabId
153
- : this.state.activeTabId !== previousState.activeTabId
154
- const {blockWidth} = this.state
155
-
156
- if (activeTabChanged) {
157
- this.adjustSlider()
158
- }
159
-
160
- if (!blockWidth) {
161
- this.setTabsDimensions()
162
- }
163
- }
164
-
165
- shouldComponentUpdate(nextProps, nextState) {
166
- const {
167
- activeTabId,
168
- blockWidth,
169
- showMoreWidth,
170
- sliderLeft,
171
- sliderWidth,
172
- tabsTotalWidth,
173
- focusedTabKey
174
- } = this.state
175
-
176
- const {
177
- initialActiveTabId,
178
- onActiveTabChanged,
179
- resizeThrottle,
180
- tabSizeTimeout,
181
- children
182
- } = this.props
183
-
184
- return (
185
- nextState.blockWidth !== blockWidth ||
186
- nextState.showMoreWidth !== showMoreWidth ||
187
- nextState.sliderLeft !== sliderLeft ||
188
- nextState.sliderWidth !== sliderWidth ||
189
- nextState.tabsTotalWidth !== tabsTotalWidth ||
190
- nextState.focusedTabKey !== focusedTabKey ||
191
- nextState.activeTabId !== activeTabId ||
192
- nextProps.initialActiveTabId !== initialActiveTabId ||
193
- nextProps.onActiveTabChanged !== onActiveTabChanged ||
194
- nextProps.resizeThrottle !== resizeThrottle ||
195
- nextProps.tabSizeTimeout !== tabSizeTimeout ||
196
- nextProps.children !== children
197
- )
198
- }
199
-
200
- setTabsDimensions = () => {
201
- window.requestAnimationFrame(() => {
202
- if (!this.tabsWrapper) {
203
- // it shouldn't happens evern. Just paranoic check
204
- return
205
- }
206
-
207
- // initial wrapper width calculation
208
- const blockWidth =
209
- this.tabsWrapper.offsetWidth - this.props.showMoreWidthBuffer - this.getTitleWidth()
210
-
211
- // calculate width and offset for each tab
212
- let tabsTotalWidth = 0
213
- const tabDimensions = new Map()
214
-
215
- Array.from(this.tabRefs.entries()).forEach(([key, value]) => {
216
- if (value) {
217
- const width = value.tab.offsetWidth
218
- tabDimensions.set(key.replace(tabPrefix, ''), {width, offset: tabsTotalWidth})
219
- tabsTotalWidth += width
220
- }
221
- })
222
-
223
- this.setState({tabDimensions, tabsTotalWidth, blockWidth})
224
- })
225
- }
226
-
227
- adjustSlider = () => {
228
- window.requestAnimationFrame(() => {
229
- if (this.activeTabElement && this.activeTabElement.tab) {
230
- this.setState({
231
- sliderLeft: this.activeTabElement.tab.firstChild.offsetLeft,
232
- sliderWidth: this.activeTabElement.tab.firstChild.clientWidth
233
- })
234
- } else {
235
- this.setState({sliderLeft: 0, sliderWidth: 0})
236
- }
237
- })
238
- }
239
-
240
- getTitleWidth = () => (this.titleWrapper ? this.titleWrapper.offsetWidth : 0)
241
-
242
- onResizeThrottled = width => {
243
- this.setState({blockWidth: width - this.props.showMoreWidthBuffer - this.getTitleWidth()})
244
- this.adjustSlider()
245
- }
246
-
247
- handleHeaderResize = debounce(() => {
248
- this.setTabsDimensions()
249
- this.adjustSlider()
250
- }, 50)
251
-
252
- renderTabHeader = headerProp => {
253
- const isRenderProp = typeof headerProp === 'function'
254
- if (!isRenderProp) {
255
- return headerProp
256
- }
257
-
258
- return headerProp({
259
- onResize: this.handleHeaderResize
260
- })
261
- }
262
-
263
- getTabs = (allTabs, shrinkToParent) => {
264
- const {blockWidth, tabsTotalWidth, tabDimensions, showMoreWidth} = this.state
265
- const activeTabId = this.getActiveTabId(allTabs)
266
- const availableWidth = blockWidth - (tabsTotalWidth > blockWidth ? showMoreWidth : 0)
267
-
268
- return allTabs.reduce(
269
- (result, item) => {
270
- const {id, header, children, disabled} = item.props
271
- const selected = activeTabId === id
272
- const payload = {tabIndex: result.tabIndex, selected, disabled, id}
273
- const tabPayload = {
274
- ...payload,
275
- header
276
- }
277
-
278
- const panelPayload = {
279
- ...payload,
280
- children
281
- }
282
-
283
- const tabWidth = tabDimensions.has(id) ? tabDimensions.get(id).width : 0
284
-
285
- /* eslint-disable no-param-reassign */
286
- if (
287
- !shrinkToParent ||
288
- // initial call
289
- !blockWidth ||
290
- // all tabs are fit into the block
291
- blockWidth > tabsTotalWidth ||
292
- // current tab fit into the block
293
- result.availableWidth - tabWidth - showMoreWidth > 0
294
- ) {
295
- result.tabsVisible.push(tabPayload)
296
- } else {
297
- result.tabsHidden.push(tabPayload)
298
- if (selected) {
299
- result.isSelectedTabHidden = true
300
- }
301
- }
302
- /* eslint-enable no-param-reassign */
303
-
304
- result.panels.set(id, panelPayload)
305
- result.availableWidth -= tabWidth
306
- result.tabIndex += 1
307
-
308
- return result
309
- },
310
- {
311
- tabsVisible: [],
312
- tabsHidden: [],
313
- panels: new Map(),
314
- isSelectedTabHidden: false,
315
- tabIndex: 0,
316
- availableWidth
317
- }
318
- )
319
- }
320
-
321
- getActiveTabId = tabs => {
322
- const [firstTab] = tabs
323
-
324
- if (this.isControlledMode()) {
325
- const tabId = this.props.activeTabId
326
-
327
- if (tabId && tabs.some(tab => tab.props.id === tabId)) {
328
- return tabId
329
- }
330
-
331
- return firstTab.props.id
332
- }
333
-
334
- const {activeTabId} = this.state
335
-
336
- if (typeof activeTabId === 'undefined') {
337
- if (!(firstTab && firstTab.props)) {
338
- return null
339
- }
340
-
341
- return firstTab.props.id || null
342
- }
343
-
344
- return activeTabId
345
- }
346
-
347
- onFocusTab = focusedTabKey => () => this.setState({focusedTabKey})
348
-
349
- onBlurTab = () => this.setState({focusedTabKey: null})
350
-
351
- onKeyDown = event => {
352
- if (event.key === 'Enter' && this.state.focusedTabKey !== null) {
353
- this.changeTab(this.state.focusedTabKey)
354
- }
355
- }
356
-
357
- onClick = tab => !tab.disabled && this.changeTab(tab.id)
358
-
359
- getTabProps = ({header, id, selected, tabIndex, disabled}) => ({
360
- selected,
361
- tabIndex,
362
- children: this.renderTabHeader(header),
363
- key: tabPrefix + id,
364
- id: tabPrefix + id,
365
- ref: tab => {
366
- if (selected) {
367
- this.activeTabElement = tab
368
- }
369
-
370
- this.tabRefs.set(tabPrefix + id, tab)
371
- },
372
- originalKey: id,
373
- onClick: () => this.onClick({disabled, id}),
374
- getOnFocusCallback: this.onFocusTab,
375
- onBlur: this.onBlurTab,
376
- panelId: panelPrefix + id,
377
- classNames: classNames(
378
- styles['tabs__header__tab-item'],
379
- selected && styles['tabs__header__tab-item--active'],
380
- disabled && styles['tabs__header__tab-item--disabled']
381
- )
382
- })
383
-
384
- getPanelProps = ({id, children}) => ({
385
- children,
386
- key: panelPrefix + id,
387
- id: panelPrefix + id,
388
- tabId: tabPrefix + id
389
- })
390
-
391
- showMoreChanged = element => {
392
- if (!element) {
393
- return
394
- }
395
-
396
- const showMoreWidth = element.offsetWidth
397
- if (this.state.showMoreWidth === showMoreWidth) {
398
- return
399
- }
400
-
401
- this.setState({
402
- showMoreWidth
403
- })
404
- }
405
-
406
- getShowMoreProps = isSelectedTabHidden => ({
407
- onShowMoreChanged: this.showMoreChanged,
408
- hasChildSelected: isSelectedTabHidden
409
- })
410
-
411
- getSliderStyle = isSelectedTabHidden => ({
412
- left: isSelectedTabHidden ? '100%' : this.state.sliderLeft,
413
- width: isSelectedTabHidden ? '0' : this.state.sliderWidth
414
- })
415
-
416
- getAllTabs = () => React.Children.toArray(this.props.children).filter(child => child.type === Tab)
417
-
418
- render() {
419
- if (this.props.printMode) {
420
- return <TabsPrint className={this.props.className} children={this.props.children} />
421
- }
422
-
423
- const allTabs = this.getAllTabs()
424
-
425
- const activeTabId = this.getActiveTabId(allTabs)
426
- const {tabsVisible, tabsHidden, panels, isSelectedTabHidden} = this.getTabs(
427
- allTabs,
428
- this.props.shrinkToParent
429
- )
430
- const activeTab = panels.get(activeTabId) || allTabs[0]
431
-
432
- return (
433
- <div className={classNames(styles['tabs'], this.props.className)}>
434
- <div className={styles.tabs__header}>
435
- <div
436
- ref={tabs => (this.tabsWrapper = tabs)}
437
- onKeyDown={this.onKeyDown}
438
- className={styles.tabs__header__container}
439
- role="tablist"
440
- aria-labelledby={this.tablistId}
441
- >
442
- {this.props.title ? (
443
- <div
444
- className={styles.tabs__header__title}
445
- ref={titleWrapper => (this.titleWrapper = titleWrapper)}
446
- id={this.tablistId}
447
- >
448
- {this.props.title}
449
- </div>
450
- ) : null}
451
- {tabsVisible.map(tab => (
452
- <TabInner {...this.getTabProps(tab)} />
453
- ))}
454
- <ShowMore
455
- {...this.getShowMoreProps(isSelectedTabHidden)}
456
- showMorePopoverClassName={this.props.showMorePopoverClassName}
457
- showMorePopoverProps={this.props.showMorePopoverProps}
458
- >
459
- {tabsHidden.map((tab, index) => (
460
- <TabInner firstElement={index === 0} {...this.getTabProps(tab)} />
461
- ))}
462
- </ShowMore>
463
- </div>
464
- <div className={styles.tabs__header__line}>
465
- <div
466
- className={styles.tabs__header__line__slider}
467
- style={this.getSliderStyle(isSelectedTabHidden)}
468
- />
469
- </div>
470
- </div>
471
- <div className={styles.tabs__body}>
472
- {this.props.cacheOpenedTabs
473
- ? allTabs.map(({props: {id}}) => {
474
- const shouldBeRendered =
475
- panels.has(id) && (id === activeTabId || this.state.cachedTabs.has(id))
476
- const isHidden = id !== activeTabId
477
-
478
- if (shouldBeRendered) {
479
- return <TabPanel {...this.getPanelProps(panels.get(id))} hidden={isHidden} />
480
- }
481
-
482
- return null
483
- })
484
- : activeTab && <TabPanel {...this.getPanelProps(activeTab)} />}
485
- </div>
486
-
487
- <ResizeDetector
488
- handleWidth
489
- handleHeight
490
- refreshMode="throttle"
491
- refreshRate={this.props.resizeThrottle}
492
- onResize={this.onResizeThrottled}
493
- />
494
- </div>
495
- )
496
- }
497
- }
498
-
499
- Tabs.Tab = Tab
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import classNames from 'classnames'
4
+ import ResizeDetector from 'react-resize-detector'
5
+ import ShowMore from './ShowMore'
6
+ import {TabPanel} from './TabPanel'
7
+ import TabInner from './TabInner'
8
+ import styles from './style/index.css'
9
+ import {debounce} from '../../base/utils/debounce'
10
+ import {TabsPrint} from './TabsPrint'
11
+ import {showMorePopoverProps} from './shared-proptype'
12
+ import {generateId} from '../../base/utils/ariaUtils'
13
+
14
+ const tabPrefix = 'tab-'
15
+ const panelPrefix = 'panel-'
16
+ const tablistPrefix = 'tablist-'
17
+
18
+ // By itself, Tab is just a dummy component that is used for a cleaner Tabs API
19
+ // and as marker for actual tabs to prevent storing them as huge array
20
+ export const Tab = () => null
21
+
22
+ Tab.propTypes = {
23
+ header: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
24
+ id: PropTypes.string.isRequired,
25
+ children: PropTypes.node,
26
+ disabled: PropTypes.bool
27
+ }
28
+
29
+ /**
30
+ * Tabs component is an aggregate root for several content panes
31
+ * (via Tabs.Tab component), each with its own children, and a header
32
+ * to switch between them.
33
+ *
34
+ * Each `<Tab />` must have a `header` (that can be arbitary react element but
35
+ * is most commonly just a string), `id` and regular children.
36
+ *
37
+ * Top-level `<Tabs />` can work in either controlled or uncontrolled mode.
38
+ * In controlled mode, it uses `initialActiveTabId` to determine which
39
+ * tab is open by default, and then manually manageschanging tabs
40
+ * on appropriate header clicks.
41
+ * In uncontrolled mode, it uses `activeTabId` prop to determine which tab
42
+ * is open and exposes `onActiveTabChanged` callback that is invoked each time
43
+ * a user clicks on a tab.
44
+ *
45
+ * By default, `<Tabs />` will shrink its tab headers to fit the parent element. Tab
46
+ * headers which don't fit into the parent are rendered inside Show More popover.
47
+ * In order to disable this behavior and allow `<Tabs/>` to occupy as much space as it needs
48
+ * to render all tab headers, set `shrinkToParent` prop to `false`
49
+ **/
50
+
51
+ export class Tabs extends React.Component {
52
+ static propTypes = {
53
+ /** Name of some important part of the component */
54
+ name: PropTypes.string.isRequired,
55
+
56
+ activeTabId: PropTypes.string,
57
+ initialActiveTabId: PropTypes.string,
58
+ onActiveTabChanged: PropTypes.func,
59
+ resizeThrottle: PropTypes.number,
60
+ showMoreWidthBuffer: PropTypes.number,
61
+ showMorePopoverClassName: PropTypes.string,
62
+ showMorePopoverProps: showMorePopoverProps,
63
+ tabSizeTimeout: PropTypes.number,
64
+ className: PropTypes.string,
65
+ shrinkToParent: PropTypes.bool,
66
+ printMode: PropTypes.bool,
67
+ cacheOpenedTabs: PropTypes.bool
68
+ }
69
+
70
+ static defaultProps = {
71
+ resizeThrottle: 100,
72
+ tabSizeTimeout: 200,
73
+ showMoreWidthBuffer: 70,
74
+ shrinkToParent: true,
75
+ printMode: false,
76
+ cacheOpenedTabs: false
77
+ }
78
+
79
+ tablistId = generateId(tablistPrefix)
80
+
81
+ tabRefs = new Map()
82
+
83
+ activeTabElement = null
84
+
85
+ titleWrapper = null
86
+
87
+ state = {
88
+ activeTabId: this.props.initialActiveTabId,
89
+ cachedTabs: new Set(),
90
+ sliderLeft: 0,
91
+ sliderWidth: 0,
92
+ tabDimensions: new Map(),
93
+ blockWidth: 0,
94
+ tabsTotalWidth: 0,
95
+ showMoreWidth: 40,
96
+ focusedTabKey: null
97
+ }
98
+
99
+ static getDerivedStateFromProps(props, state) {
100
+ if (!props.cacheOpenedTabs) {
101
+ return {
102
+ ...state,
103
+ cachedTabs: new Set()
104
+ }
105
+ } else if (typeof props.activeTabId !== 'undefined') {
106
+ const cachedTabs = new Set(state.cachedTabs)
107
+ cachedTabs.add(state.activeTabId)
108
+
109
+ return {
110
+ ...state,
111
+ activeTabId: props.activeTabId,
112
+ cachedTabs
113
+ }
114
+ }
115
+
116
+ return null
117
+ }
118
+
119
+ isControlledMode = () => typeof this.props.activeTabId !== 'undefined'
120
+
121
+ changeTab = tabId => {
122
+ const currentActiveTabId = this.getActiveTabId(this.getAllTabs())
123
+
124
+ if (this.props.cacheOpenedTabs) {
125
+ this.setState(state => {
126
+ const cachedTabs = new Set(state.cachedTabs)
127
+ cachedTabs.add(currentActiveTabId)
128
+
129
+ return {
130
+ ...state,
131
+ activeTabId: tabId,
132
+ cachedTabs
133
+ }
134
+ })
135
+ } else {
136
+ this.setState({activeTabId: tabId})
137
+ }
138
+ if (typeof this.props.onActiveTabChanged === 'function') {
139
+ this.props.onActiveTabChanged(tabId)
140
+ }
141
+ }
142
+
143
+ componentDidMount() {
144
+ setTimeout(() => {
145
+ this.setTabsDimensions()
146
+ this.adjustSlider()
147
+ }, this.props.tabSizeTimeout)
148
+ }
149
+
150
+ componentDidUpdate(previousProps, previousState) {
151
+ const activeTabChanged = this.isControlledMode()
152
+ ? this.props.activeTabId !== previousProps.activeTabId
153
+ : this.state.activeTabId !== previousState.activeTabId
154
+ const {blockWidth} = this.state
155
+
156
+ if (activeTabChanged) {
157
+ this.adjustSlider()
158
+ }
159
+
160
+ if (!blockWidth) {
161
+ this.setTabsDimensions()
162
+ }
163
+ }
164
+
165
+ shouldComponentUpdate(nextProps, nextState) {
166
+ const {
167
+ activeTabId,
168
+ blockWidth,
169
+ showMoreWidth,
170
+ sliderLeft,
171
+ sliderWidth,
172
+ tabsTotalWidth,
173
+ focusedTabKey
174
+ } = this.state
175
+
176
+ const {
177
+ initialActiveTabId,
178
+ onActiveTabChanged,
179
+ resizeThrottle,
180
+ tabSizeTimeout,
181
+ children
182
+ } = this.props
183
+
184
+ return (
185
+ nextState.blockWidth !== blockWidth ||
186
+ nextState.showMoreWidth !== showMoreWidth ||
187
+ nextState.sliderLeft !== sliderLeft ||
188
+ nextState.sliderWidth !== sliderWidth ||
189
+ nextState.tabsTotalWidth !== tabsTotalWidth ||
190
+ nextState.focusedTabKey !== focusedTabKey ||
191
+ nextState.activeTabId !== activeTabId ||
192
+ nextProps.initialActiveTabId !== initialActiveTabId ||
193
+ nextProps.onActiveTabChanged !== onActiveTabChanged ||
194
+ nextProps.resizeThrottle !== resizeThrottle ||
195
+ nextProps.tabSizeTimeout !== tabSizeTimeout ||
196
+ nextProps.children !== children
197
+ )
198
+ }
199
+
200
+ setTabsDimensions = () => {
201
+ window.requestAnimationFrame(() => {
202
+ if (!this.tabsWrapper) {
203
+ // it shouldn't happens evern. Just paranoic check
204
+ return
205
+ }
206
+
207
+ // initial wrapper width calculation
208
+ const blockWidth =
209
+ this.tabsWrapper.offsetWidth - this.props.showMoreWidthBuffer - this.getTitleWidth()
210
+
211
+ // calculate width and offset for each tab
212
+ let tabsTotalWidth = 0
213
+ const tabDimensions = new Map()
214
+
215
+ Array.from(this.tabRefs.entries()).forEach(([key, value]) => {
216
+ if (value) {
217
+ const width = value.tab.offsetWidth
218
+ tabDimensions.set(key.replace(tabPrefix, ''), {width, offset: tabsTotalWidth})
219
+ tabsTotalWidth += width
220
+ }
221
+ })
222
+
223
+ this.setState({tabDimensions, tabsTotalWidth, blockWidth})
224
+ })
225
+ }
226
+
227
+ adjustSlider = () => {
228
+ window.requestAnimationFrame(() => {
229
+ if (this.activeTabElement && this.activeTabElement.tab) {
230
+ this.setState({
231
+ sliderLeft: this.activeTabElement.tab.firstChild.offsetLeft,
232
+ sliderWidth: this.activeTabElement.tab.firstChild.clientWidth
233
+ })
234
+ } else {
235
+ this.setState({sliderLeft: 0, sliderWidth: 0})
236
+ }
237
+ })
238
+ }
239
+
240
+ getTitleWidth = () => (this.titleWrapper ? this.titleWrapper.offsetWidth : 0)
241
+
242
+ onResizeThrottled = width => {
243
+ this.setState({blockWidth: width - this.props.showMoreWidthBuffer - this.getTitleWidth()})
244
+ this.adjustSlider()
245
+ }
246
+
247
+ handleHeaderResize = debounce(() => {
248
+ this.setTabsDimensions()
249
+ this.adjustSlider()
250
+ }, 50)
251
+
252
+ renderTabHeader = headerProp => {
253
+ const isRenderProp = typeof headerProp === 'function'
254
+ if (!isRenderProp) {
255
+ return headerProp
256
+ }
257
+
258
+ return headerProp({
259
+ onResize: this.handleHeaderResize
260
+ })
261
+ }
262
+
263
+ getTabs = (allTabs, shrinkToParent) => {
264
+ const {blockWidth, tabsTotalWidth, tabDimensions, showMoreWidth} = this.state
265
+ const activeTabId = this.getActiveTabId(allTabs)
266
+ const availableWidth = blockWidth - (tabsTotalWidth > blockWidth ? showMoreWidth : 0)
267
+
268
+ return allTabs.reduce(
269
+ (result, item) => {
270
+ const {id, header, children, disabled} = item.props
271
+ const selected = activeTabId === id
272
+ const payload = {tabIndex: result.tabIndex, selected, disabled, id}
273
+ const tabPayload = {
274
+ ...payload,
275
+ header
276
+ }
277
+
278
+ const panelPayload = {
279
+ ...payload,
280
+ children
281
+ }
282
+
283
+ const tabWidth = tabDimensions.has(id) ? tabDimensions.get(id).width : 0
284
+
285
+ /* eslint-disable no-param-reassign */
286
+ if (
287
+ !shrinkToParent ||
288
+ // initial call
289
+ !blockWidth ||
290
+ // all tabs are fit into the block
291
+ blockWidth > tabsTotalWidth ||
292
+ // current tab fit into the block
293
+ result.availableWidth - tabWidth - showMoreWidth > 0
294
+ ) {
295
+ result.tabsVisible.push(tabPayload)
296
+ } else {
297
+ result.tabsHidden.push(tabPayload)
298
+ if (selected) {
299
+ result.isSelectedTabHidden = true
300
+ }
301
+ }
302
+ /* eslint-enable no-param-reassign */
303
+
304
+ result.panels.set(id, panelPayload)
305
+ result.availableWidth -= tabWidth
306
+ result.tabIndex += 1
307
+
308
+ return result
309
+ },
310
+ {
311
+ tabsVisible: [],
312
+ tabsHidden: [],
313
+ panels: new Map(),
314
+ isSelectedTabHidden: false,
315
+ tabIndex: 0,
316
+ availableWidth
317
+ }
318
+ )
319
+ }
320
+
321
+ getActiveTabId = tabs => {
322
+ const [firstTab] = tabs
323
+
324
+ if (this.isControlledMode()) {
325
+ const tabId = this.props.activeTabId
326
+
327
+ if (tabId && tabs.some(tab => tab.props.id === tabId)) {
328
+ return tabId
329
+ }
330
+
331
+ return firstTab.props.id
332
+ }
333
+
334
+ const {activeTabId} = this.state
335
+
336
+ if (typeof activeTabId === 'undefined') {
337
+ if (!(firstTab && firstTab.props)) {
338
+ return null
339
+ }
340
+
341
+ return firstTab.props.id || null
342
+ }
343
+
344
+ return activeTabId
345
+ }
346
+
347
+ onFocusTab = focusedTabKey => () => this.setState({focusedTabKey})
348
+
349
+ onBlurTab = () => this.setState({focusedTabKey: null})
350
+
351
+ onKeyDown = event => {
352
+ if (event.key === 'Enter' && this.state.focusedTabKey !== null) {
353
+ this.changeTab(this.state.focusedTabKey)
354
+ }
355
+ }
356
+
357
+ onClick = tab => !tab.disabled && this.changeTab(tab.id)
358
+
359
+ getTabProps = ({header, id, selected, tabIndex, disabled}) => ({
360
+ selected,
361
+ tabIndex,
362
+ children: this.renderTabHeader(header),
363
+ key: tabPrefix + id,
364
+ id: tabPrefix + id,
365
+ ref: tab => {
366
+ if (selected) {
367
+ this.activeTabElement = tab
368
+ }
369
+
370
+ this.tabRefs.set(tabPrefix + id, tab)
371
+ },
372
+ originalKey: id,
373
+ onClick: () => this.onClick({disabled, id}),
374
+ getOnFocusCallback: this.onFocusTab,
375
+ onBlur: this.onBlurTab,
376
+ panelId: panelPrefix + id,
377
+ classNames: classNames(
378
+ styles['tabs__header__tab-item'],
379
+ selected && styles['tabs__header__tab-item--active'],
380
+ disabled && styles['tabs__header__tab-item--disabled']
381
+ )
382
+ })
383
+
384
+ getPanelProps = ({id, children}) => ({
385
+ children,
386
+ key: panelPrefix + id,
387
+ id: panelPrefix + id,
388
+ tabId: tabPrefix + id
389
+ })
390
+
391
+ showMoreChanged = element => {
392
+ if (!element) {
393
+ return
394
+ }
395
+
396
+ const showMoreWidth = element.offsetWidth
397
+ if (this.state.showMoreWidth === showMoreWidth) {
398
+ return
399
+ }
400
+
401
+ this.setState({
402
+ showMoreWidth
403
+ })
404
+ }
405
+
406
+ getShowMoreProps = isSelectedTabHidden => ({
407
+ onShowMoreChanged: this.showMoreChanged,
408
+ hasChildSelected: isSelectedTabHidden
409
+ })
410
+
411
+ getSliderStyle = isSelectedTabHidden => ({
412
+ left: isSelectedTabHidden ? '100%' : this.state.sliderLeft,
413
+ width: isSelectedTabHidden ? '0' : this.state.sliderWidth
414
+ })
415
+
416
+ getAllTabs = () => React.Children.toArray(this.props.children).filter(child => child.type === Tab)
417
+
418
+ render() {
419
+ if (this.props.printMode) {
420
+ return <TabsPrint className={this.props.className} children={this.props.children} />
421
+ }
422
+
423
+ const allTabs = this.getAllTabs()
424
+
425
+ const activeTabId = this.getActiveTabId(allTabs)
426
+ const {tabsVisible, tabsHidden, panels, isSelectedTabHidden} = this.getTabs(
427
+ allTabs,
428
+ this.props.shrinkToParent
429
+ )
430
+ const activeTab = panels.get(activeTabId) || allTabs[0]
431
+
432
+ return (
433
+ <div className={classNames(styles['tabs'], this.props.className)}>
434
+ <div className={styles.tabs__header}>
435
+ <div
436
+ ref={tabs => (this.tabsWrapper = tabs)}
437
+ onKeyDown={this.onKeyDown}
438
+ className={styles.tabs__header__container}
439
+ role="tablist"
440
+ aria-labelledby={this.tablistId}
441
+ >
442
+ {this.props.title ? (
443
+ <div
444
+ className={styles.tabs__header__title}
445
+ ref={titleWrapper => (this.titleWrapper = titleWrapper)}
446
+ id={this.tablistId}
447
+ >
448
+ {this.props.title}
449
+ </div>
450
+ ) : null}
451
+ {tabsVisible.map(tab => (
452
+ <TabInner {...this.getTabProps(tab)} />
453
+ ))}
454
+ <ShowMore
455
+ {...this.getShowMoreProps(isSelectedTabHidden)}
456
+ showMorePopoverClassName={this.props.showMorePopoverClassName}
457
+ showMorePopoverProps={this.props.showMorePopoverProps}
458
+ >
459
+ {tabsHidden.map((tab, index) => (
460
+ <TabInner firstElement={index === 0} {...this.getTabProps(tab)} />
461
+ ))}
462
+ </ShowMore>
463
+ </div>
464
+ <div className={styles.tabs__header__line}>
465
+ <div
466
+ className={styles.tabs__header__line__slider}
467
+ style={this.getSliderStyle(isSelectedTabHidden)}
468
+ />
469
+ </div>
470
+ </div>
471
+ <div className={styles.tabs__body}>
472
+ {this.props.cacheOpenedTabs
473
+ ? allTabs.map(({props: {id}}) => {
474
+ const shouldBeRendered =
475
+ panels.has(id) && (id === activeTabId || this.state.cachedTabs.has(id))
476
+ const isHidden = id !== activeTabId
477
+
478
+ if (shouldBeRendered) {
479
+ return <TabPanel {...this.getPanelProps(panels.get(id))} hidden={isHidden} />
480
+ }
481
+
482
+ return null
483
+ })
484
+ : activeTab && <TabPanel {...this.getPanelProps(activeTab)} />}
485
+ </div>
486
+
487
+ <ResizeDetector
488
+ handleWidth
489
+ handleHeight
490
+ refreshMode="throttle"
491
+ refreshRate={this.props.resizeThrottle}
492
+ onResize={this.onResizeThrottled}
493
+ />
494
+ </div>
495
+ )
496
+ }
497
+ }
498
+
499
+ Tabs.Tab = Tab