index.tsx 870 B

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