RadioSelector.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. type PropsType = {
  4. selected: string;
  5. setSelected: (x: string) => void;
  6. options: { value: string; label: string }[];
  7. };
  8. type StateType = {};
  9. export default class RadioSelector extends Component<PropsType, StateType> {
  10. render() {
  11. return (
  12. <StyledRadioSelector>
  13. {this.props.options.map(
  14. (option: { label: string; value: string }, i: number) => {
  15. let selected = option.value === this.props.selected;
  16. return (
  17. <RadioRow
  18. key={option.value}
  19. onClick={() => this.props.setSelected(option.value)}
  20. >
  21. <Indicator selected={selected}>
  22. {selected && <Circle />}
  23. </Indicator>
  24. {option.label}
  25. </RadioRow>
  26. );
  27. }
  28. )}
  29. </StyledRadioSelector>
  30. );
  31. }
  32. }
  33. const RadioRow = styled.div`
  34. display: flex;
  35. align-items: center;
  36. cursor: pointer;
  37. margin-bottom: 12px;
  38. :hover {
  39. > div {
  40. background: #ffffff22;
  41. }
  42. }
  43. `;
  44. const Indicator = styled.div<{ selected: boolean }>`
  45. border-radius: 15px;
  46. display: flex;
  47. margin-right: 4px;
  48. align-items: center;
  49. justify-content: center;
  50. width: 16px;
  51. height: 16px;
  52. border: 1px solid #ffffff55;
  53. margin: 1px 10px 0px 1px;
  54. background: ${(props) => (props.selected ? "#ffffff22" : "#ffffff11")};
  55. `;
  56. const Circle = styled.div`
  57. width: 8px;
  58. height: 8px;
  59. background: #ffffff55;
  60. border-radius: 15px;
  61. `;
  62. const StyledRadioSelector = styled.div``;