CheckboxList.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from "react";
  2. import styled from "styled-components";
  3. type PropsType = {
  4. label?: string;
  5. options: { disabled?: boolean; value: string; label: string }[];
  6. selected: { value: string; label: string }[];
  7. setSelected: (x: { value: string; label: string }[]) => void;
  8. };
  9. const CheckboxList = ({ label, options, selected, setSelected }: PropsType) => {
  10. let onSelectOption = (option: { value: string; label: string }) => {
  11. if (!selected.includes(option)) {
  12. selected.push(option);
  13. setSelected(selected);
  14. } else {
  15. selected.splice(selected.indexOf(option), 1);
  16. setSelected(selected);
  17. }
  18. };
  19. return (
  20. <StyledCheckboxList>
  21. {label && <Label>{label}</Label>}
  22. {options.map((option: { value: string; label: string }, i: number) => {
  23. return (
  24. <CheckboxOption
  25. isLast={i === options.length - 1}
  26. onClick={() => onSelectOption(option)}
  27. key={i}
  28. >
  29. <Checkbox checked={selected.includes(option)}>
  30. <i className="material-icons">done</i>
  31. </Checkbox>
  32. {option.label}
  33. </CheckboxOption>
  34. );
  35. })}
  36. </StyledCheckboxList>
  37. );
  38. };
  39. export default CheckboxList;
  40. const Checkbox = styled.div`
  41. width: 16px;
  42. height: 16px;
  43. border: 1px solid #ffffff55;
  44. margin: 1px 15px 0px 1px;
  45. border-radius: 3px;
  46. background: ${(props: { checked: boolean }) =>
  47. props.checked ? "#ffffff22" : "#ffffff11"};
  48. display: flex;
  49. align-items: center;
  50. justify-content: center;
  51. > i {
  52. font-size: 12px;
  53. padding-left: 0px;
  54. display: ${(props: { checked: boolean }) => (props.checked ? "" : "none")};
  55. }
  56. `;
  57. const CheckboxOption = styled.div<{ isLast: boolean }>`
  58. width: 100%;
  59. height: 35px;
  60. padding-left: 10px;
  61. display: flex;
  62. cursor: pointer;
  63. align-items: center;
  64. border-bottom: ${props => (props.isLast ? "" : "1px solid #ffffff22")};
  65. font-size: 13px;
  66. :hover {
  67. background: #ffffff18;
  68. }
  69. `;
  70. const Label = styled.div`
  71. color: #ffffff;
  72. margin-bottom: 10px;
  73. `;
  74. const StyledCheckboxList = styled.div`
  75. border-radius: 3px;
  76. border: 1px solid #ffffff55;
  77. padding: 0;
  78. background: #ffffff11;
  79. margin-bottom: 15px;
  80. margin-top: 20px;
  81. `;