Created
April 24, 2020 10:22
-
-
Save jestho/f988ad39faa61aa73618bcce55aebd45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import styled from "styled-components"; | |
export const Tabs: React.FC<{ | |
disabled?: boolean; | |
titles: string[]; | |
children: React.ReactNodeArray; | |
}> = p => { | |
if (p.disabled) { | |
return <>{p.children}</>; | |
} | |
const [index, setIndex] = useState(0); | |
const current = React.Children.toArray(p.children)[index]; | |
const tabs = p.titles.map((tab, i) => ( | |
<_tab | |
key={tab} | |
role="button" | |
isSelected={i === index} | |
onClick={() => setIndex(i)} | |
> | |
{tab} | |
</_tab> | |
)); | |
return ( | |
<> | |
<_tabs>{tabs}</_tabs> | |
<_content>{current}</_content> | |
</> | |
); | |
}; | |
const _tabs = styled.div``; | |
const _tab = styled.div<{ isSelected?: boolean }>``; | |
const _content = styled.div``; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment