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

0% Statements 0/7
0% Branches 0/2
0% Functions 0/3
0% Lines 0/7

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                                                                                                               
import React, { useState } from "react";
 
export interface DropdownLink {
  name: string;
  link?: string;
}
 
export interface DropdownInterface {
  prompt: string;
  options: Record<string, DropdownLink>;
  onChange: (event: React.ChangeEvent<HTMLSelectElement>) => void;
}
 
const Dropdown: React.FC<DropdownInterface> = (props) => {
  const [selectedOption, setSelectedOption] = useState("");
 
  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    setSelectedOption(event.target.value);
    props.onChange(event);
  };
 
  return (
    <div>
      <label htmlFor="dropdown">{props.prompt}:</label>
      <select
        id="dropdown"
        value={selectedOption}
        onChange={handleChange}
        // Both class names are written out in full: Tailwind only emits what
        // it can see in the source, so `bg-${…}` produced a class that was
        // purged from the stylesheet and left this control unstyled. Letting
        // the `dark:` variant do the switching also makes it react to a theme
        // change, which reading the DOM once during render never did.
        className="bg-white dark:bg-black rounded-sm"
      >
        <option value="">--Select--</option>
        {Object.entries(props.options).map(([key, val]) => {
          return <option value={key}>{val.name}</option>;
        })}
      </select>
      <br></br>
      <br></br>
      {selectedOption && (
        <p className="font-bold">
          You selected:{" "}
          <a href={props.options[selectedOption].link}>
            {props.options[selectedOption].name}
          </a>
        </p>
      )}
    </div>
  );
};
 
export default Dropdown;