All files / src/components/field/MultiSelect MultiSelect.tsx

0% Statements 0/24
0% Branches 0/17
0% Functions 0/9
0% Lines 0/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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                                                                                                                                                                                                                                   
import React, { useState } from "react";
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { ControllerRenderProps } from "react-hook-form";
import { CircleX } from "lucide-react";
import { DropdownFormat } from "@/util/format";
 
export interface MultiSelectDropdownInterface {
  field: ControllerRenderProps<{ [x: string]: any }, string>;
  options: Record<string, Record<string, string>>;
  title: string;
  picks?: string[];
  max?: number;
  format?: DropdownFormat;
}
 
export const MultiSelectDropdown: React.FC<MultiSelectDropdownInterface> = (
  props
) => {
  const [value, setValue] = useState<string | undefined>(undefined);
  const [picks, setPicks] = useState<string[]>(props.picks ?? []);
 
  const handleSelect = (selectedValue: string) => {
    if (
      !picks.includes(selectedValue) &&
      (props.max === undefined || picks.length < props.max)
    ) {
      const updated = [...picks, selectedValue];
      setPicks(updated);
      props.field.onChange(updated);
      setValue(undefined);
    }
  };
 
  const removeItem = (valToRemove: string) => {
    const updated = picks.filter((v) => v !== valToRemove);
    setPicks(updated);
    props.field.onChange(updated);
    setValue(undefined);
  };
 
  return (
    <div className="space-y-2">
      <Select onValueChange={handleSelect}>
        <SelectTrigger
          className="bg-white"
          value=""
          defaultValue=""
          disabled={picks.length == props.max}
        >
          <span>{`Select ${props.title}${
            props.max !== undefined ? ` (max ${props.max})` : ""
          }`}</span>
        </SelectTrigger>
 
        <SelectContent>
          {Object.entries(props.options).map(([category, opts]) => (
            <SelectGroup key={category}>
              <SelectLabel>{category}</SelectLabel>
 
              {Object.entries(opts).map(([label, value]) => {
                const selectedValues = Array.isArray(props.field.value)
                  ? props.field.value
                  : [];
                const newLabel =
                  (props.format ?? "values") === "values"
                    ? value
                    : `${label} - ${value}`;
 
                return (
                  <SelectItem
                    key={value}
                    value={newLabel}
                    disabled={selectedValues.includes(value)}
                  >
                    {newLabel}
                  </SelectItem>
                );
              })}
            </SelectGroup>
          ))}
        </SelectContent>
      </Select>
 
      {/* Selected items */}
      <div className="flex flex-wrap gap-2">
        {picks.map((val: string) => {
          // Find label from all category lists
          const label =
            Object.values(props.options)
              .flat()
              .find((o) => o.value === val)?.label ?? val;
 
          return (
            <span
              key={val}
              className="bg-slate-800 text-white px-3 py-1 rounded-lg flex items-center gap-2"
            >
              {label}
 
              <button
                type="button"
                onClick={() => removeItem(val)}
                className="text-red-500 hover:text-red-700 !border-none !p-2"
              >
                <CircleX className="w-4 text-gray-400" />
              </button>
            </span>
          );
        })}
      </div>
    </div>
  );
};