index.tsx 897 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { useState } from 'react';
  2. import { Wrapper, BtnGroup, Btn } from './styled';
  3. type OptionProps = {
  4. key: string;
  5. content: React.ReactNode;
  6. child: React.ReactNode;
  7. };
  8. type Props = {
  9. options: OptionProps[];
  10. };
  11. const Tabs: React.FunctionComponent<Props> = ({
  12. options,
  13. }: Props) => {
  14. const [selectedIndex, setSelect] = useState(0);
  15. const handleClick = (index: number): void => {
  16. setSelect(index);
  17. };
  18. return (
  19. <Wrapper>
  20. <BtnGroup>
  21. {
  22. options.map((ele, index) => (
  23. <Btn
  24. key={ele.key}
  25. isActive={index === selectedIndex}
  26. onClick={(): void => { handleClick(index); }}
  27. >
  28. {ele.content}
  29. </Btn>
  30. ))
  31. }
  32. </BtnGroup>
  33. {options[selectedIndex] ? options[selectedIndex].child : null}
  34. </Wrapper>
  35. );
  36. };
  37. export default Tabs;