All files / components/FilterComponent ExclusiveCheckBoxes.tsx

100% Statements 23/23
100% Branches 4/4
100% Functions 7/7
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 726x 6x   6x             6x       8x 8x       8x 6x     8x 1x 1x 1x 1x 1x       8x 1x 1x 1x 1x 1x       8x                             16x                           6x  
import React, { useEffect, useState } from "react";
import { Checkbox, Space } from "antd";
import type { CheckboxProps } from "antd";
import { useSearchParams } from "react-router-dom";
 
interface Props {
  options: { value: string; label: string }[];
  paramName: string;
}
 
const ExvlusiveCheckBoxes: React.FC<Props> = ({
  options,
  paramName,
}: Props) => {
  const [searchParams, setSearchParams] = useSearchParams();
  const [checked, setChecked] = useState<string>(
    searchParams.get(paramName) || "All"
  );
 
  useEffect(()=>{
    setChecked(searchParams.get(paramName) || "All")
  },[paramName, searchParams])
 
  const onChange: CheckboxProps["onChange"] = (e) => {
    setChecked(e.target.value);
    setSearchParams((p) => {
      p.set(paramName, e.target.value);
      p.delete("page");
      return p;
    });
  };
 
  const onCheckAll: CheckboxProps["onChange"] = (e) => {
    setChecked(e.target.value);
    setSearchParams((p) => {
      p.delete(paramName);
      p.delete("page");
      return p;
    });
  };
 
  return (
    <>
      <Space
        direction="vertical"
        style={{ maxHeight: "200px", overflow: "auto" }}
      >
        <Checkbox
          value={"All"}
          key={"all"}
          onChange={onCheckAll}
          checked={checked === "All"}
        >
          {"All"}
        </Checkbox>
        {options.map((option, index) => (
          <Checkbox
            value={option.value.toString()}
            key={index}
            onChange={onChange}
            checked={option.value.toString() === checked}
          >
            {option.label}{" "}
          </Checkbox>
        ))}
      </Space>
    </>
  );
};
 
export default ExvlusiveCheckBoxes;