All files / react-app/src/components/RHF SlotField.tsx

71.42% Statements 15/21
62.16% Branches 23/37
60% Functions 6/10
73.68% Lines 14/19

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287                                                                                      72x                       152x   152x                                                       1x   1x         1x                                                       7x 7x           2x                                                                                                               2x 2x 2x                                                                                                                                 72x 7x                                                                      
import { RHFComponentMap, RHFOption, RHFSlotProps, MultiselectOption } from "shared-types";
import { CalendarIcon } from "lucide-react";
import { format } from "date-fns";
import { cn } from "@/utils";
import { Popover, PopoverContent, PopoverTrigger } from "@/components";
import {
  DependencyWrapper,
  RHFFieldArray,
  RHFFormGroup,
  RHFSlot,
  RHFTextDisplay,
  ruleGenerator,
  sortFunctions,
  stringCompare,
} from ".";
import {
  Button,
  Calendar,
  Checkbox,
  FormControl,
  FormField,
  FormLabel,
  Input,
  Multiselect,
  RadioGroup,
  RadioGroupItem,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Switch,
  Textarea,
  Upload,
} from "@/components/Inputs";
import { useGetCounties } from "@/api";
 
type SlotFieldProps = RHFSlotProps & { control: any; field: any };
type SelectedSubsetProps = RHFOption & {
  control: any;
  parentId?: string;
};
 
export const SlotField = ({
  props,
  field,
  rhf,
  text,
  control,
  parentId,
  fields,
  name,
  horizontalLayout,
  index,
}: SlotFieldProps) => {
  const counties = useGetCounties();
 
  switch (rhf) {
    case "Input":
      return <Input {...props} {...field} aria-label={field.name} />;
    case "Textarea":
      return <Textarea {...props} {...field} aria-label={field.name} />;
    case "Switch":
      return <Switch {...props} {...field} aria-label={field.name} />;
    case "TextDisplay":
      return (
        <p {...props} data-testid={field.name}>
          <RHFTextDisplay text={text ?? "UNDEFINED TEXT FIELD"} index={index} />
        </p>
      );
    case "Upload":
      return <Upload {...props} files={field?.value ?? []} setFiles={field.onChange} />;
    case "FieldArray":
      return (
        <RHFFieldArray
          control={control}
          name={name}
          rhf={rhf}
          fields={fields ?? []}
          parentId={parentId}
          {...props}
        />
      );
    case "Select": {
      let opts;
      switch (props?.apiCall) {
        case undefined:
          opts = props?.options?.sort((a, b) =>
            props.customSort
              ? sortFunctions[props.customSort](a.label, b.label)
              : stringCompare(a, b),
          );
          break;
 
        case "countySelect":
          opts =
            counties.sort((a, b) =>
              props.customSort
                ? sortFunctions[props.customSort](a.label, b.label)
                : stringCompare(a, b),
            ) || [];
          break;
      }
 
      return (
        <Select {...props} onValueChange={field.onChange} defaultValue={field.value}>
          <SelectTrigger {...props} aria-label={field.name}>
            <SelectValue {...props} />
          </SelectTrigger>
          <SelectContent className="overflow-auto max-h-60">
            {opts?.map((OPT) => (
              <SelectItem key={`OPT-${OPT.value}`} value={OPT.value}>
                {OPT.label}
              </SelectItem>
            ))}
          </SelectContent>
        </Select>
      );
    }
    case "Multiselect": {
      const options = props?.options as MultiselectOption[];
      const value = field.value as string[];
 
      return (
        <Multiselect
          options={options}
          value={value}
          onChange={(selectedValues) => field.onChange(selectedValues)}
          {...props}
        />
      );
    }
    case "DatePicker":
      return (
        <FormControl>
          <Popover>
            <PopoverTrigger asChild>
              <Button
                variant={"outline"}
                className={cn(
                  "w-[240px] pl-3 text-left font-normal text-[#212121] justify-start",
                  !field.value && "text-muted-foreground",
                )}
              >
                <CalendarIcon className="h-4" />
                {field.value ? (
                  format(new Date(field.value), "MM/dd/yyyy")
                ) : (
                  <span>Pick a date</span>
                )}
              </Button>
            </PopoverTrigger>
            <PopoverContent className="w-auto p-0" align="start">
              <Calendar
                {...props}
                mode="single"
                selected={field.value && new Date(field.value)}
                defaultMonth={field.value && new Date(field.value)}
                onSelect={(date) => field.onChange(date)}
                initialFocus
              />
            </PopoverContent>
          </Popover>
        </FormControl>
      );
    case "Checkbox":
      return (
        <div className="flex flex-col gap-3">
          {(props as RHFComponentMap["Checkbox"]).options.map((OPT) => (
            <DependencyWrapper
              name={OPT.value}
              dependency={OPT.dependency}
              changeMethod={field.onChange}
              parentValue={field.value}
              key={`CHECK-dw-${OPT.value}`}
            >
              <div key={`CHECK-${OPT.value}`}>
                <Checkbox
                  label={OPT.label}
                  value={OPT.value}
                  checked={field.value?.includes(OPT.value)}
                  styledLabel={<RHFTextDisplay text={(OPT.styledLabel || OPT.label) as string} />}
                  onCheckedChange={(c) => {
                    const filtered = field.value?.filter((f: unknown) => f !== OPT.value) || [];
                    Iif (!c) return field.onChange(filtered);
                    field.onChange([...filtered, OPT.value]);
                  }}
                  dependency={OPT.dependency}
                  parentValue={field.value}
                  changeMethod={field.onChange}
                  aria-label={field.name}
                  optionlabelClassName={OPT.optionlabelClassName}
                />
                {field.value?.includes(OPT.value) && (
                  <OptChildren {...OPT} parentId={parentId} control={control} />
                )}
              </div>
            </DependencyWrapper>
          ))}
        </div>
      );
    case "Radio":
      return (
        <RadioGroup
          onValueChange={field.onChange}
          defaultValue={field.value}
          className={`flex  ${horizontalLayout ? "pl-5 gap-5" : "flex-col space-y-6"}`}
        >
          {(props as RHFComponentMap["Radio"]).options.map((OPT) => {
            return (
              <div key={`OPT-${OPT.value}`} className="flex flex-col">
                <div className="flex gap-2">
                  <RadioGroupItem value={OPT.value} id={OPT.value} aria-label={OPT.value} />
                  {
                    <FormLabel className="font-normal" htmlFor={OPT.value}>
                      <RHFTextDisplay text={(OPT.styledLabel || OPT.label) as string} />
                    </FormLabel>
                  }
                </div>
                {field.value?.includes(OPT.value) && (
                  <OptChildren {...OPT} parentId={parentId} control={control} />
                )}
              </div>
            );
          })}
        </RadioGroup>
      );
    case "WrappedGroup":
      return (
        <div className={props?.wrapperClassName}>
          {fields?.map((S, i) => {
            return (
              <DependencyWrapper {...S}>
                <FormField
                  key={`wrappedSlot-${i}`}
                  control={control}
                  name={parentId + S.name}
                  rules={ruleGenerator(S.rules, S.addtnlRules)}
                  render={RHFSlot({ ...S, control, parentId })}
                />
              </DependencyWrapper>
            );
          })}
        </div>
      );
    case "Divider":
      return <div className={cn("w-full border-slate-400 border-2", props?.wrapperClassName)} />;
  }
};
 
export const OptChildren = ({ form, slots, control, parentId }: SelectedSubsetProps) => {
  const childClasses = "ml-[0.6rem] mt-4 pl-6 px-4 space-y-6 border-l-4 border-l-primary";
 
  return (
    <>
      {form && (
        <div className={childClasses}>
          {form.map((FORM, index) => (
            <div key={`rhf-form-${index}-${FORM.description}`}>
              <RHFFormGroup form={FORM} control={control} parentId={parentId} className="py-0" />
            </div>
          ))}
        </div>
      )}
      {slots && (
        <div className={childClasses}>
          {slots.map((SLOT, index) => (
            <div key={SLOT.name + index}>
              <FormField
                control={control}
                name={parentId + SLOT.name}
                rules={ruleGenerator(SLOT.rules, SLOT.addtnlRules)}
                render={RHFSlot({
                  ...SLOT,
                  control,
                  parentId,
                  name: parentId + SLOT.name,
                })}
              />
            </div>
          ))}
        </div>
      )}
    </>
  );
};