index.tsx 943 B

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