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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 14x 7x 9x 7x 7x 7x 17x 17x 30x 13x 17x 7x 4x 3x 3x 3x 1x 16x 16x 30x 14x 16x 14x 2x 16x 89x 89x 89x 89x | import React from 'react'; import PropTypes from 'prop-types'; /** * Tabs is container component for rendering tabs and tab-panels * @category layout * @component tabs * @variations collab-ui-react * */ class Tabs extends React.Component { state = { activeIndex: 0, focus: this.props.focus }; getChildContext = () => { return { activeIndex: this.state.activeIndex, onActivate: index => this.setSelected(index), onFocus: index => this.setState({ focus: index }), focus: this.state.focus, }; }; componentWillMount() { const tabsCount = this.getChildrenElements('TabList'); const panelsCount = this.getChildrenElements('TabContent'); Iif (tabsCount !== panelsCount) { throw new Error(`There should be an equal number of Tabs and TabPanels. Received ${tabsCount} Tabs and ${panelsCount} TabPanels.`); } } getMobileListItems = () => { return React.Children.map(this.props.children, child => { if (child.type.displayName === 'TopbarNav') { return child.props.children; } }); }; getChildrenElements = name => { let elementCount = 0; React.Children.forEach(this.props.children, child => { if (child.type.displayName === name) { return child.props.children.length ? (elementCount += child.props.children.length) : elementCount++; } }); return elementCount; }; setSelected = index => { // Don't do anything if index is the same or outside of the bounds if ( index === this.state.activeIndex || index < 0 || index >= this.getChildrenElements('TabList') ) return; // Keep reference to last index for event handler const last = this.state.activeIndex; // Update state with selected index this.setState({ activeIndex: index }); // Call change event handler if (typeof this.props.onSelect === 'function') { this.props.onSelect(index, last); } }; render() { const { children, className, tabType, justified } = this.props; const cloneChildren = React.Children.map(children, child => { if (child.type.displayName === 'TabContent') { return React.cloneElement(child, { activeIndex: this.state.activeIndex, }); } else if (child.type.displayName === 'TabList') { return React.cloneElement(child, { role: 'tab' }); } else { return child; } }); return ( <div className={ 'cui-tab' + `${(tabType && ` cui-tab--${tabType}`) || ''}` + `${(justified && ` cui-tab--justified`) || ''}` + (className && ` ${className}`) || ''} type={tabType} > {cloneChildren} </div> ); } } Tabs.propTypes = { /** @prop Determines if Tab is active | false */ active: PropTypes.bool, /** @prop Children nodes of Tab and TabContent are required */ children: PropTypes.node.isRequired, /** @prop Optional CSS class name | '' */ className: PropTypes.string, /** @prop Set the index of the focused Tab | 0 */ focus: PropTypes.number, /** @prop Determines if the Tabs are in a justified layout | false */ justified: PropTypes.bool, /** @prop Callback function invoked when user selects a tab | null */ onSelect: PropTypes.func, /** @prop Type of Tabs | 'pills' */ tabType: PropTypes.oneOf(['pills']), }; Tabs.defaultProps = { active: false, className: '', focus: 0, justified: false, onSelect: null, tabType: 'pills', }; Tabs.childContextTypes = { focus: PropTypes.number, activeIndex: PropTypes.number, onActivate: PropTypes.func, onFocus: PropTypes.func, }; Tabs.displayName = 'Tabs'; export default Tabs; /** * @component tabs * @section default * @react import { Tab, TabContent, TabList, TabPane, Tabs, } from '@collab-ui/react'; export default function TabsDefault() { return ( <Tabs> <TabList> <Tab heading='First Tab' /> <Tab heading='Second Tab' /> <Tab heading='Third Tab' /> </TabList> <TabContent> <TabPane> Testing 1 </TabPane> <TabPane> Testing 2 </TabPane> <TabPane> Testing 3 </TabPane> </TabContent> </Tabs> ); } **/ /** * @component tabs * @section justified * @react import { Tab, TabContent, TabList, TabPane, Tabs, } from '@collab-ui/react'; export default function TabsJustified() { return ( <Tabs justified> <TabList> <Tab heading='First Tab' /> <Tab heading='Second Tab' /> <Tab heading='Third Tab' /> </TabList> <TabContent> <TabPane> Testing 1 </TabPane> <TabPane> Testing 2 </TabPane> <TabPane> Testing 3 </TabPane> </TabContent> </Tabs> ); } **/ |