Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import React from 'react'
  2. import { render } from 'react-dom'
  3.  
  4. var styles = {};
  5.  
  6. styles.tab = {
  7. display: 'inline-block',
  8. padding: 10,
  9. margin: 10,
  10. borderBottom: '4px solid',
  11. borderBottomColor: '#ccc',
  12. cursor: 'pointer'
  13. };
  14.  
  15. styles.activeTab = {
  16. ...styles.tab,
  17. borderBottomColor: '#000'
  18. };
  19.  
  20. styles.disabledTab = {
  21. ...styles.tab,
  22. opacity: 0.25,
  23. cursor: 'default'
  24. };
  25.  
  26. styles.tabPanels = {
  27. padding: 10
  28. };
  29.  
  30.  
  31. var TabList = React.createClass({
  32. contextTypes: {
  33. activeIndex: React.PropTypes.number,
  34. onActivate: React.PropTypes.func
  35. },
  36.  
  37. render () {
  38. console.log('TabList')
  39. var children = React.Children.map(this.props.children, (child, index) => {
  40. return React.cloneElement(child, {
  41. isActive: index === this.context.activeIndex,
  42. onClick: () => this.context.onActivate(index)
  43. })
  44. });
  45. return <div style={styles.tabs}>{children}</div>
  46. }
  47. });
  48.  
  49. var Tab = React.createClass({
  50. render () {
  51. console.log('Tab')
  52. return (
  53. <div
  54. onClick={this.props.disabled ? null : this.props.onClick}
  55. style={this.props.disabled ? styles.disabledTab : (
  56. this.props.isActive ? styles.activeTab : styles.tab
  57. )}
  58. >
  59. {this.props.children}
  60. </div>
  61. );
  62. }
  63. });
  64.  
  65. var TabPanels = React.createClass({
  66. contextTypes: {
  67. activeIndex: React.PropTypes.number
  68. },
  69.  
  70. render () {
  71. console.log('TabPanels')
  72. return (
  73. <div style={styles.tabPanels}>
  74. {this.props.children[this.context.activeIndex]}
  75. </div>
  76. );
  77. }
  78. });
  79.  
  80. var TabPanel = React.createClass({
  81. render () {
  82. console.log('TabPanel')
  83. return <div>{this.props.children}</div>
  84. }
  85. });
  86.  
  87. var Tabs = React.createClass({
  88.  
  89. getInitialState() {
  90. return {
  91. activeIndex: 0
  92. };
  93. },
  94.  
  95. childContextTypes: {
  96. activeIndex: React.PropTypes.number,
  97. onActivate: React.PropTypes.func
  98. },
  99.  
  100. getChildContext () {
  101. return {
  102. activeIndex: this.state.activeIndex,
  103. onActivate: (activeIndex) => {
  104. this.setState({ activeIndex })
  105. }
  106. };
  107. },
  108.  
  109. render() {
  110. console.log('Tabs');
  111. return <div>{this.props.children}</div>;
  112. }
  113. });
  114.  
  115. var App = React.createClass({
  116. render () {
  117. return (
  118. <div>
  119. <Tabs>
  120. <div>
  121. <TabList>
  122. <Tab>Tacos</Tab>
  123. <Tab disabled>Burritos</Tab>
  124. <Tab>Coconut Korma</Tab>
  125. </TabList>
  126. </div>
  127.  
  128. <div>
  129. <TabPanels>
  130. <TabPanel>
  131. <p>Tacos are delicious</p>
  132. </TabPanel>
  133. <TabPanel>
  134. <p>Sometimes a burrito is what you really need.</p>
  135. </TabPanel>
  136. <TabPanel>
  137. <p>Might be your best option.</p>
  138. </TabPanel>
  139. </TabPanels>
  140. </div>
  141. </Tabs>
  142. </div>
  143. );
  144. }
  145. });
  146.  
  147. render(<App/>, document.getElementById('app'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement