/
Tab.js
165 lines (138 loc) · 4.41 KB
/
Tab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import {
ModernAutoControlledComponent as Component,
customPropTypes,
getElementType,
getUnhandledProps,
} from '../../lib'
import Grid from '../../collections/Grid/Grid'
import GridColumn from '../../collections/Grid/GridColumn'
import Menu from '../../collections/Menu/Menu'
import TabPane from './TabPane'
/**
* A Tab is a hidden section of content activated by a Menu.
* @see Menu
* @see Segment
*/
class Tab extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** The initial activeIndex. */
defaultActiveIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Index of the currently active tab. */
activeIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* Shorthand props for the Menu.
* tabular, if true, will derive final value from `menuPosition`, otherwise set 'left' or 'right' explicitly.
*/
menu: PropTypes.object,
/** Align vertical menu */
menuPosition: PropTypes.oneOf(['left', 'right']),
/** Shorthand props for the Grid. */
grid: PropTypes.object,
/**
* Called on tab change.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props and proposed new activeIndex.
* @param {object} data.activeIndex - The new proposed activeIndex.
*/
onTabChange: PropTypes.func,
/**
* Array of objects describing each Menu.Item and Tab.Pane:
* { menuItem: 'Home', render: () => <Tab.Pane /> }
* or
* { menuItem: 'Home', pane: 'Welcome' }
*/
panes: PropTypes.arrayOf(
PropTypes.shape({
menuItem: customPropTypes.itemShorthand,
pane: customPropTypes.itemShorthand,
render: PropTypes.func,
}),
),
/** A Tab can render only active pane. */
renderActiveOnly: PropTypes.bool,
}
static autoControlledProps = ['activeIndex']
static defaultProps = {
grid: { paneWidth: 12, tabWidth: 4 },
menu: { attached: true, tabular: true },
renderActiveOnly: true,
}
static Pane = TabPane
getInitialAutoControlledState() {
return { activeIndex: 0 }
}
handleItemClick = (e, { index }) => {
_.invoke(this.props, 'onTabChange', e, { ...this.props, activeIndex: index })
this.setState({ activeIndex: index })
}
renderItems() {
const { panes, renderActiveOnly } = this.props
const { activeIndex } = this.state
if (renderActiveOnly) return _.invoke(_.get(panes, `[${activeIndex}]`), 'render', this.props)
return _.map(panes, ({ pane }, index) =>
TabPane.create(pane, {
overrideProps: {
active: index === activeIndex,
},
}),
)
}
renderMenu() {
const { menu, panes, menuPosition } = this.props
const { activeIndex } = this.state
if (menu.tabular === true && menuPosition === 'right') {
menu.tabular = 'right'
}
return Menu.create(menu, {
autoGenerateKey: false,
overrideProps: {
items: _.map(panes, 'menuItem'),
onItemClick: this.handleItemClick,
activeIndex,
},
})
}
renderVertical(menu) {
const { grid, menuPosition } = this.props
const { paneWidth, tabWidth, ...gridProps } = grid
const position = menuPosition || (menu.props.tabular === 'right' && 'right') || 'left'
return (
<Grid {...gridProps}>
{position === 'left' &&
GridColumn.create({ width: tabWidth, children: menu }, { autoGenerateKey: false })}
{GridColumn.create(
{
width: paneWidth,
children: this.renderItems(),
stretched: true,
},
{ autoGenerateKey: false },
)}
{position === 'right' &&
GridColumn.create({ width: tabWidth, children: menu }, { autoGenerateKey: false })}
</Grid>
)
}
render() {
const menu = this.renderMenu()
const rest = getUnhandledProps(Tab, this.props)
const ElementType = getElementType(Tab, this.props)
if (menu.props.vertical) {
return <ElementType {...rest}>{this.renderVertical(menu)}</ElementType>
}
return (
<ElementType {...rest}>
{menu.props.attached !== 'bottom' && menu}
{this.renderItems()}
{menu.props.attached === 'bottom' && menu}
</ElementType>
)
}
}
export default Tab