From 95b573a39ba08cf8c1fdefa29ecc21692f15b214 Mon Sep 17 00:00:00 2001 From: chenmeng Date: Sun, 27 Jan 2019 17:59:52 +0800 Subject: [PATCH 1/2] + add snippet Tabs --- snippets/Tabs.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 snippets/Tabs.md diff --git a/snippets/Tabs.md b/snippets/Tabs.md new file mode 100644 index 000000000..e981fc823 --- /dev/null +++ b/snippets/Tabs.md @@ -0,0 +1,69 @@ +### 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 + +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` +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`) + + +```jsx +class Tab extends React.Component{ + constructor(props){ + super(props) + this.changeTab(props.defaultIndex) + this.state = { + bindIndex:'' + } + } + + changeTab(newIndex){ + if(typeof this.props.onTabClick === 'function'){ + this.props.onTabClick(newIndex) + } + this.setState({ + bindIndex: newIndex + }) + } + + itemStyle(index){ + return { + display:this.state.bindIndex === index?'block':'none' + } + } + + render(){ + const items = this.props.children + .filter(item=>item.type.name==='TabItem') + return ( +
+
+ {items.map(({props:{index, label}})=>)} +
+
+ {items.map(({props})=>( +
))} +
+
+ ) + } +} + +function TabItem(props){ + return
+} +``` +```jsx +ReactDOM.render( + + item1 + item2 + , + document.getElementById('root') +); +``` + + + + \ No newline at end of file From 1bbd643e160b39d53431ee1523dd12e5336048cd Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 28 Jan 2019 19:40:05 +0200 Subject: [PATCH 2/2] Update Tabs.md --- snippets/Tabs.md | 112 ++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/snippets/Tabs.md b/snippets/Tabs.md index e981fc823..8582e9974 100644 --- a/snippets/Tabs.md +++ b/snippets/Tabs.md @@ -1,69 +1,99 @@ ### 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` -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`) +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`. +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 `)} +
+
+ {items.map(({ props: { index, label } }) => ( + + ))}
-
- {items.map(({props})=>( -
))} +
+ {items.map(({ props }) => ( +
+ ))}
- ) + ); } } - -function TabItem(props){ - return
+function TabItem(props) { + return
; } ``` ```jsx ReactDOM.render( - - item1 - item2 + + + Lorem ipsum + + + Dolor sit amet + , - document.getElementById('root') + document.getElementById("root") ); + ``` - \ No newline at end of file +