Update Tabs.md

This commit is contained in:
Angelos Chalaris
2019-01-28 19:40:05 +02:00
committed by GitHub
parent 95b573a39b
commit 1bbd643e16

View File

@ -1,67 +1,97 @@
### Tab ### Tab
Render a menu and a view, change the content in the view by clicking the button in the menu and the callback will execute before render Renders a tabbed menu and view component.
Define the `TabItem` as a middleware, pass it to the `Tab` and remove the useless nodes expect `TabItem` by identify the function's name in `props.children`, after that we will use the collected nodes to render the `tab-menu` and `tab-view` with map. Buttons in the `tab-menu` will execute `changeTab` Define `TabItem` as a middleware, pass it to the `Tab` and remove unnecessary nodes expect for `TabItem` by identifying the function's name in `props.children`.
when been clicked, `changeTab` will execute the callback funciton(`onTabClick`) and change `bindIndex` to emit `render`,every node in `tab-view` use `bindIndex` and the `index` which ws passing from `TabItem` to judege whether it show of not(`itemStyle`) Use `Array.prototype.map` on the collected nodes to render the `tab-menu` and `tab-view`.
Define `changeTab`, which will be executed when clicking a `<button>` from the `tab-menu`.
`changeTab` executes the passed callback, `onTabClick` and updates `state.bindIndex`, which in turn causes a re-render, evaluating the `style` and `className` of the `tab-view` items and `tab-menu` buttons according to their `index`.
```css
.tab-menu > button {
cursor: pointer;
padding: 8px 16px;
border: 0;
border-bottom: 2px solid transparent;
background: none;
}
.tab-menu > button.focus {
border-bottom: 2px solid #007BEF;
}
.tab-menu > button:hover {
border-bottom: 2px solid #007BEF;
}
```
```jsx ```jsx
class Tab extends React.Component{ class Tab extends React.Component {
constructor(props){ constructor(props) {
super(props) super(props);
this.changeTab(props.defaultIndex)
this.state = { this.state = {
bindIndex:'' bindIndex: props.defaultIndex
} };
} }
changeTab(newIndex) {
changeTab(newIndex){ if (typeof this.props.onTabClick === "function")
if(typeof this.props.onTabClick === 'function'){ this.props.onTabClick(newIndex);
this.props.onTabClick(newIndex)
}
this.setState({ this.setState({
bindIndex: newIndex bindIndex: newIndex
}) });
} }
buttonClass(index) {
itemStyle(index){ return this.state.bindIndex === index ? "focus" : "";
}
itemStyle(index) {
return { return {
display:this.state.bindIndex === index?'block':'none' display: this.state.bindIndex === index ? "block" : "none"
} };
} }
render() {
render(){ const items = this.props.children.filter(
const items = this.props.children item => item.type.name === "TabItem"
.filter(item=>item.type.name==='TabItem') );
return ( return (
<div className='wrapper'> <div className="wrapper">
<div className='tab-menu'> <div className="tab-menu">
{items.map(({props:{index, label}})=><button onClick={()=>this.changeTab(index)}>{label}</button>)} {items.map(({ props: { index, label } }) => (
<button
onClick={() => this.changeTab(index)}
className={this.buttonClass(index)}
>
{label}
</button>
))}
</div> </div>
<div className='tab-view'> <div className="tab-view">
{items.map(({props})=>( {items.map(({ props }) => (
<div {...props} <div
className='tab-view_item' {...props}
key={props.index} className="tab-view_item"
style={this.itemStyle(props.index)}/>))} key={props.index}
style={this.itemStyle(props.index)}
/>
))}
</div> </div>
</div> </div>
) );
} }
} }
function TabItem(props) {
function TabItem(props){ return <div {...props} />;
return <div {...props}></div>
} }
``` ```
```jsx ```jsx
ReactDOM.render( ReactDOM.render(
<Tab defaultIndex='1' onTabClick={console.log}> <Tab defaultIndex="1" onTabClick={console.log}>
<TabItem label='item1' index='1'>item1</TabItem> <TabItem label="A" index="1">
<TabItem label='item2' index='2'>item2</TabItem> Lorem ipsum
</TabItem>
<TabItem label="B" index="2">
Dolor sit amet
</TabItem>
</Tab>, </Tab>,
document.getElementById('root') document.getElementById("root")
); );
``` ```
<!-- tags: visual,children,class --> <!-- tags: visual,children,class -->