This is my attempt at learning REACT. There are many bugs in these components and they MUST not be copied for use in production.
Working example generated by Netify for testing: https://csb-9fldvf.netlify.app/
-
Import React and the "useState" hook from the "react" library.
-
Define a React functional component named "TabControl" that takes in props "tabs" and "className".
-
Use the "useState" hook to create state "selectedIndex" with a default value of 0 and a state updater function "setSelectedIndex".
-
Return a "div" element with the class "className" passed in as a prop.
-
Within the first "div", return another "div" element with the class "tab-list" and "role" attribute of "tablist".
-
Map over the "tabs" prop and return a "div" element for each item in the "tabs" array.
-
The returned "div" element has a class of "tab" and "selected" if the current "index" is equal to the "selectedIndex" state.
-
Within the "div" element, return a "button" element with the following attributes:
- "role" of "tab"
- "aria-selected" attribute equal to whether the current "index" is equal to "selectedIndex" state.
- "aria-controls" equal to "panel-index"
- "onClick" event handler that sets "selectedIndex" to the current "index" when clicked.
- The "tab.label" as the button text.
-
Close the mapping of the "tabs" array.
-
Return another "div" element with the class "tab-content".
-
Within this "div" element, map over the "tabs" prop and return a "div" element for each item in the "tabs" array.
-
The returned "div" element has the following attributes:
- "role" of "tabpanel"
- "id" equal to "panel-index"
- "aria-labelledby" equal to "tab.label"
- "hidden" attribute equal to whether the current "index" is not equal to "selectedIndex" state.
- "className" equal to "tab-panel"
- "key" equal to "panel-index".
- The "tab.content" is returned within the "div" element.
-
Close the mapping of the "tabs" array.
-
Close the two "div" elements.
-
Export the "TabControl" component as the default export.