All files / components/Teams TeamDropdown.jsx

100% Statements 13/13
100% Branches 10/10
100% Functions 6/6
100% Lines 11/11

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                          53x   53x 34x 2x       60x         60x   53x 9x 4x       53x                   11x                        
import { useEffect, useRef } from "react";
import { Typeahead } from "react-bootstrap-typeahead";
import { Form } from "react-bootstrap";
import "react-bootstrap-typeahead/css/Typeahead.css";
 
export default function TeamDropdown({
  teams,
  value,
  onChange,
  isInvalid,
  disabled = false,
  testId = "TeamDropdown",
}) {
  const typeaheadRef = useRef(null);
 
  useEffect(() => {
    if (value === "" && typeaheadRef.current?.getInput().value) {
      typeaheadRef.current.clear();
    }
  }, [value]);
 
  const options = teams.map((team) => ({
    id: team.id,
    name: team.name,
  }));
 
  const selectedOption = options.filter((option) => option.name === value);
 
  const handleSelectionChange = (selected) => {
    if (selected.length > 0) {
      onChange(selected[0].name);
    }
  };
 
  return (
    <Form.Group controlId="team">
      <Typeahead
        ref={typeaheadRef}
        id="team-typeahead"
        options={options}
        labelKey="name"
        placeholder="Select a team."
        selected={selectedOption}
        onChange={handleSelectionChange}
        onInputChange={(text) => onChange(text)}
        highlightOnlyResult
        disabled={disabled}
        inputProps={{
          "aria-label": "Select Team",
          "data-testid": testId,
          className: isInvalid ? "form-control is-invalid" : "form-control",
        }}
      />
    </Form.Group>
  );
}