diff --git a/README.md b/README.md index 918e8a4f3..78e729de2 100644 --- a/README.md +++ b/README.md @@ -914,10 +914,11 @@ ReactDOM.render(, document.getElementById('root')); Renders a tabbed menu and view component. -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`. +Define a `TabItem` component, pass it to the `Tab` and remove unnecessary nodes expect for `TabItem` by identifying the function's name in `props.children`. +Use the `React.useState()` hook to initialize the value of the `bindIndex` state variable to `props.defaultIndex`. 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 }) => ( -
- ))} -
-
- ); - } -} function TabItem(props) { return
; } + +function Tabs(props) { + const [bindIndex, setBindIndex] = React.useState(props.defaultIndex); + const changeTab = newIndex => { + if (typeof props.onTabClick === "function") props.onTabClick(newIndex); + setBindIndex(newIndex); + }; + const items = props.children.filter(item => item.type.name === "TabItem"); + + return ( +
+
+ {items.map(({ props: { index, label } }) => ( + + ))} +
+
+ {items.map(({ props }) => ( +
+ ))} +
+
+ ); +} ```
@@ -998,14 +981,14 @@ function TabItem(props) { ```jsx ReactDOM.render( - + Lorem ipsum Dolor sit amet - , + , document.getElementById("root") ); diff --git a/data/snippet_data.json b/data/snippet_data.json index 6c8de8b51..648993aa6 100644 --- a/data/snippet_data.json +++ b/data/snippet_data.json @@ -215,11 +215,11 @@ { "name": "Tabs.md", "title": "Tab", - "text": "Renders a tabbed menu and view component.\n\nDefine `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`.\nUse `Array.prototype.map` on the collected nodes to render the `tab-menu` and `tab-view`. \nDefine `changeTab`, which will be executed when clicking a `\n ))}\n
\n
\n {items.map(({ props }) => (\n \n ))}\n
\n
\n );\n }\n}\nfunction TabItem(props) {\n return
;\n}\n```", - "```jsx\nReactDOM.render(\n \n \n Lorem ipsum\n \n \n Dolor sit amet\n \n ,\n document.getElementById(\"root\")\n);\n\n```" + "```jsx\nfunction TabItem(props) {\n return
;\n}\n\nfunction Tabs(props) {\n const [bindIndex, setBindIndex] = React.useState(props.defaultIndex);\n const changeTab = newIndex => {\n if (typeof props.onTabClick === \"function\") props.onTabClick(newIndex);\n setBindIndex(newIndex);\n };\n const items = props.children.filter(item => item.type.name === \"TabItem\");\n\n return (\n
\n
\n {items.map(({ props: { index, label } }) => (\n changeTab(index)}\n className={bindIndex === index ? \"focus\" : \"\"}\n >\n {label}\n \n ))}\n
\n
\n {items.map(({ props }) => (\n \n ))}\n
\n
\n );\n}\n```", + "```jsx\nReactDOM.render(\n \n \n Lorem ipsum\n \n \n Dolor sit amet\n \n ,\n document.getElementById(\"root\")\n);\n\n```" ], "expertise": 1, "tags": [ diff --git a/snippets/Tabs.md b/snippets/Tabs.md index 8582e9974..e1847e625 100644 --- a/snippets/Tabs.md +++ b/snippets/Tabs.md @@ -1,10 +1,11 @@ ### Tab Renders a tabbed menu and view component. -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`. +Define a `TabItem` component, pass it to the `Tab` and remove unnecessary nodes expect for `TabItem` by identifying the function's name in `props.children`. +Use the `React.useState()` hook to initialize the value of the `bindIndex` state variable to `props.defaultIndex`. 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 }) => ( -
- ))} -
-
- ); - } -} function TabItem(props) { return
; } + +function Tabs(props) { + const [bindIndex, setBindIndex] = React.useState(props.defaultIndex); + const changeTab = newIndex => { + if (typeof props.onTabClick === "function") props.onTabClick(newIndex); + setBindIndex(newIndex); + }; + const items = props.children.filter(item => item.type.name === "TabItem"); + + return ( +
+
+ {items.map(({ props: { index, label } }) => ( + + ))} +
+
+ {items.map(({ props }) => ( +
+ ))} +
+
+ ); +} ``` ```jsx ReactDOM.render( - + Lorem ipsum Dolor sit amet - , + , document.getElementById("root") );