CheckboxList.tsx 2.2 KB

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