All files / components/FilterComponent CheckBoxGroup.tsx

96.66% Statements 29/30
91.66% Branches 11/12
100% Functions 9/9
96.66% Lines 29/30

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 72 73 74 75 76 77 78 79 80 81 82 83 84 856x 6x   6x             6x 10x 10x       10x 5x     10x     10x   10x 1x 1x 1x     1x   1x 1x       10x 2x 2x 1x   2x     1x   2x   2x 2x       10x                                 20x                     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 CheckBoxGroup: React.FC<Props> = ({ options, paramName }: Props) => {
  const [searchParams, setSearchParams] = useSearchParams();
  const [checkedList, setCheckedList] = useState<string[]>(
    searchParams.get(paramName)?.split(",") || []
  );
 
  useEffect(()=>{
    setCheckedList(searchParams.get(paramName)?.split(",") || [])
  },[paramName, searchParams])
 
  const checkAll = options.length === checkedList.length;
 
  const indeterminate =
    checkedList.length > 0 && checkedList.length < options.length;
 
  const onChange = (list: string[]) => {
    setCheckedList(list);
    setSearchParams((searchParams) => {
      Iif (list.length === 0) {
        searchParams.delete(paramName);
      } else {
        searchParams.set(paramName, list.join(","));
      }
      searchParams.delete("page")
      return searchParams;
    });
  };
 
  const onCheckAllChange: CheckboxProps["onChange"] = (e) => {
    setSearchParams((searchParams) => {
      if (e.target.checked) {
        searchParams.set(
          paramName,
          options.map((option) => option.value.toString()).join(",")
        );
      } else {
        searchParams.delete(paramName);
      }
      return searchParams;
    });
    setCheckedList(
      e.target.checked ? options.map((option) => option.value.toString()) : []
    );
  };
 
  return (
    <>
      <Space
        direction="vertical"
        style={{ maxHeight: "200px", overflow: "auto" }}
      >
        <Checkbox
          id="all"
          indeterminate={indeterminate}
          onChange={onCheckAllChange}
          checked={checkAll}
        >
          All
        </Checkbox>
        <Checkbox.Group value={checkedList} onChange={onChange}>
          <Space direction="vertical">
            {options.map((option, index) => (
              <Checkbox value={option.value.toString()} key={index} id={option.value.toString()}>
                {option.label}
              </Checkbox>
            ))}
          </Space>
        </Checkbox.Group>
      </Space>
    </>
  );
};
 
export default CheckBoxGroup;