All files / components/Courses SchoolTypeahead.jsx

100% Statements 8/8
100% Branches 7/7
100% Functions 5/5
100% Lines 8/8

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          69x                         69x 72x 72x   152x     69x         93x     12x                                
import { useBackend } from "main/utils/useBackend";
import { Typeahead } from "react-bootstrap-typeahead";
import { Controller } from "react-hook-form";
 
export function SchoolTypeahead({ control, rules, testid }) {
  const { data: schools = [] } = useBackend(
    [`/api/systemInfo/schools`],
    {
      method: "GET",
      url: `/api/systemInfo/schools`,
    },
    undefined,
    true,
    {
      staleTime: "static",
    },
  );
 
  const filterByFields = (option, props) => {
    const search = props.text.toLowerCase();
    return (
      option.displayName.toLowerCase().includes(search) ||
      option.alternateNames.some((name) => name.toLowerCase().includes(search))
    );
  };
  return (
    <Controller
      control={control}
      name="school"
      rules={rules}
      render={({ field, fieldState }) => (
        <Typeahead
          selected={field.value ? [field.value] : []}
          onChange={(selected) => field.onChange(selected[0] ?? null)}
          id="school-typeahead"
          isInvalid={fieldState.invalid}
          inputProps={{
            "aria-label": "Choose a school",
            "data-testid": testid,
          }}
          options={schools}
          labelKey="displayName"
          filterBy={filterByFields}
          placeholder="Start typing to select a school..."
        />
      )}
    />
  );
}