Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 6x 3x 3x 3x 3x 3x 3x 3x 3x 6x 3x 3x 3x 3x 6x 8x 8x 8x 8x 14x 8x 8x 8x | /* @flow */ import * as React from 'react' import { matchPath, type RouterHistory } from 'react-router' import invariant from 'invariant' import HistoryUtils from './HistoryUtils' import StackUtils from './StackUtils' import RouteUtils from './RouteUtils' import StateUtils from './StateUtils' import type { NavigationState, TabsRendererProps, Tab, Route, HistoryRootIndex, HistoryNodes, } from './TypeDefinitions' type Props = { history: RouterHistory, children: Array<React$Node>, render: (props: TabsRendererProps<>) => React.Element<any>, lazy?: boolean, enableHistoryNodes?: boolean, onReset?: () => void, } type State = {| tabs: Array<Tab>, navigationState: NavigationState<>, historyRootIndex: HistoryRootIndex, historyNodes: HistoryNodes, |} class TabStack extends React.Component<Props, State> { unlistenHistory: ?Function = null static defaultProps = { enableHistoryNodes: false, } constructor(props: Props) { super(props) const { children, history } = props const { entries, index, location } = history invariant( children || React.Children.count(children) > 0, 'A <TabStack /> must have child elements', ) const tabs = children && StackUtils.create(children, props) const navigationState = StateUtils.initialize( tabs, location, entries, 'stack', ) invariant( navigationState.index !== -1, 'There is no route defined for path « %s »', location.pathname, ) const initialRoute = navigationState.routes[navigationState.index] const historyRootIndex = index const historyNodes = { [initialRoute.routeName]: entries.slice(index) } this.state = { tabs, navigationState, historyRootIndex, historyNodes } } componentDidMount() { const { history } = this.props this.unlistenHistory = HistoryUtils.listen(history, this.onHistoryChange) } componentWillUnmount() { if (this.unlistenHistory) this.unlistenHistory() } componentWillReceiveProps(nextProps: Props) { const { tabs } = this.state const { children: nextChildren, history } = nextProps const { location, entries } = history const nextTabs = StackUtils.create(nextChildren, nextProps) const nextTab = nextTabs.find(tab => matchPath(location.pathname, tab)) const nextRoute = nextTab && RouteUtils.create(nextTab, location) Eif (nextRoute && !StackUtils.shallowEqual(tabs, nextTabs)) { const nextNavigationState = StateUtils.initialize( nextTabs, location, entries, 'stack', ) invariant( nextNavigationState.index !== -1, 'There is no route defined for path « %s »', location.pathname, ) this.setState({ tabs: nextTabs, navigationState: nextNavigationState }) } } onHistoryChange = (history: RouterHistory, nextHistory: RouterHistory) => { const { location } = nextHistory const { navigationState, tabs, historyNodes } = this.state const currentRoute = navigationState.routes[navigationState.index] const nextTab = tabs.find(tab => matchPath(location.pathname, tab)) const nextRoute = nextTab ? RouteUtils.create(nextTab, location) : null const newHistoryNodes = HistoryUtils.saveNodes(historyNodes) Iif (nextRoute && !RouteUtils.equal(currentRoute, nextRoute)) { this.setState(prevState => ({ historyNodes: newHistoryNodes, navigationState: StateUtils.changeIndex( prevState.navigationState, nextRoute, ), })) } else { this.setState({ historyNodes: newHistoryNodes, }) } } onIndexChange = (index: number) => { const { enableHistoryNodes, history } = this.props const { tabs, navigationState, historyNodes, historyRootIndex } = this.state const nextRoute = navigationState.routes[index] const nextTab = tabs.find(tab => tab.path === nextRoute.routeName) if (index !== navigationState.index) { this.setState( { navigationState: StateUtils.changeIndex(navigationState, index), }, () => { if (enableHistoryNodes) { HistoryUtils.persistNodes( this.props.history, historyNodes[nextRoute.routeName], historyRootIndex, ) } if (nextTab && nextTab.onRequestChangeTab) { nextTab.onRequestChangeTab() } else if (!historyNodes[nextRoute.routeName]) { this.props.history.replace(nextRoute.routeName) } else { const entry = historyNodes[nextRoute.routeName].slice(-1)[0] this.props.history.replace(entry.pathname, entry.state) } }, ) } else { const n = historyRootIndex - history.index if (n < 0) { this.props.history.go(n) } else { if (this.props.onReset) this.props.onReset() if (nextTab && nextTab.onReset) nextTab.onReset() } } } shouldComponentUpdate(nextProps: Props, nextState: State) { return ( this.state.tabs !== nextState.tabs || this.state.navigationState !== nextState.navigationState ) } renderTab = (route: Route) => { const { lazy } = this.props const { historyNodes } = this.state Iif (lazy && !historyNodes[route.routeName]) return null const children = React.Children.toArray(this.props.children) const child = children.find(({ props }) => props.path === route.routeName) Iif (!child) return null return React.cloneElement(child, route) } render() { return this.props.render({ navigationState: this.state.navigationState, tabs: this.state.tabs, onIndexChange: this.onIndexChange, renderTab: this.renderTab, }) } } export default TabStack |