diff --git a/README.md b/README.md index 7cb7f1735..2a2faaf92 100644 --- a/README.md +++ b/README.md @@ -91,13 +91,14 @@ import ReactDOM from 'react-dom';
View contents +* [Accordion](#accordion) * [Carousel](#carousel) * [Collapse](#collapse) * [CountDown](#countdown) * [FileDrop](#filedrop) * [Mailto](#mailto) * [StarRating](#starrating) -* [Tab](#tab) +* [Tabs](#tabs) * [Ticker](#ticker) * [Toggle](#toggle) * [Tooltip](#tooltip) @@ -771,6 +772,92 @@ ReactDOM.render( ## Visual +### Accordion + +Renders an accordion menu with multiple collapsible content components. + +Define an `AccordionItem` component, pass it to the `Accordion` and remove unnecessary nodes expect for `AccordionItem` by identifying the function's name in `props.children`. +Each `AccordionItem` component renders a ` +
+ {props.children} +
+ + ); +} + +function Accordion(props) { + const [bindIndex, setBindIndex] = React.useState(props.defaultIndex); + + const changeItem = itemIndex => { + if (typeof props.onItemClick === 'function') props.onItemClick(itemIndex); + if (itemIndex !== bindIndex) setBindIndex(itemIndex); + }; + const items = props.children.filter(item => item.type.name === 'AccordionItem'); + + return ( +
+ {items.map(({ props }) => ( + changeItem(props.index)} + children={props.children} + /> + ))} +
+ ); +} +``` + +
+Examples + +```jsx +ReactDOM.render( + + + Lorem ipsum + + + Dolor sit amet + + , + document.getElementById('root') +); +``` +
+ +
[⬆ Back to top](#table-of-contents) + ### Carousel Renders a carousel component. @@ -1177,7 +1264,7 @@ ReactDOM.render(, document.getElementById('root'));
[⬆ Back to top](#table-of-contents) -### Tab +### Tabs Renders a tabbed menu and view component. diff --git a/data/snippet_data.json b/data/snippet_data.json index 8d5c68b77..59a1a7522 100644 --- a/data/snippet_data.json +++ b/data/snippet_data.json @@ -1,4 +1,20 @@ [ + { + "name": "Accordion.md", + "title": "Accordion", + "text": "Renders an accordion menu with multiple collapsible content components.\n\nDefine an `AccordionItem` component, pass it to the `Accordion` and remove unnecessary nodes expect for `AccordionItem` by identifying the function's name in `props.children`.\nEach `AccordionItem` component renders a `\n \n {props.children}\n \n \n );\n}\n\nfunction Accordion(props) {\n const [bindIndex, setBindIndex] = React.useState(props.defaultIndex);\n\n const changeItem = itemIndex => {\n if (typeof props.onItemClick === 'function') props.onItemClick(itemIndex);\n if (itemIndex !== bindIndex) setBindIndex(itemIndex);\n };\n const items = props.children.filter(item => item.type.name === 'AccordionItem');\n\n return (\n
\n {items.map(({ props }) => (\n changeItem(props.index)}\n children={props.children}\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```" + ], + "expertise": 2, + "tags": [ + "visual", + "children", + "state" + ], + "notes": [] + }, { "name": "AutoLink.md", "title": "AutoLink", @@ -266,7 +282,7 @@ }, { "name": "Tabs.md", - "title": "Tab", + "title": "Tabs", "text": "Renders a tabbed menu and view component.\n\nDefine a `TabItem` component, pass it to the `Tab` and remove unnecessary nodes expect for `TabItem` by identifying the function's name in `props.children`.\nUse the `React.useState()` hook to initialize the value of the `bindIndex` state variable to `props.defaultIndex`.\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 ` +
+ {props.children} +
+ + ); +} + +function Accordion(props) { + const [bindIndex, setBindIndex] = React.useState(props.defaultIndex); + + const changeItem = itemIndex => { + if (typeof props.onItemClick === 'function') props.onItemClick(itemIndex); + if (itemIndex !== bindIndex) setBindIndex(itemIndex); + }; + const items = props.children.filter(item => item.type.name === 'AccordionItem'); + + return ( +
+ {items.map(({ props }) => ( + changeItem(props.index)} + children={props.children} + /> + ))} +
+ ); +} +``` + +```jsx +ReactDOM.render( + + + Lorem ipsum + + + Dolor sit amet + + , + document.getElementById('root') +); +``` + + + +