aboutsummaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/pages/querybuilder/Components/Text/SimpleDropDownForm.jsx
blob: 94c6c01b619e3d1d1de3b18a040baefefc27adb3 (plain) (blame)
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
import React, { useContext, useEffect } from 'react';
import { QueryInputContext } from '../Contexts/QueryInputContext';

export default function SimpleDropDownForm({
  choices,
  id,
  className = 'input',
  onChange,
  value,
  initial,
}) {
  const { selectedItems } = useContext(QueryInputContext);

  //TODO: using the filtered list to render options results in dropdown not changing the displayed selection to what was actually selected.
  let filtered = Object.keys(choices).filter(
    (choice) => !selectedItems.includes(choice)
  );
  useEffect(() => {
    filtered = Object.keys(choices).filter(
      (choice) => !selectedItems.includes(choice)
    );
  }, [selectedItems]);

  const options = Object.keys(choices).map((choice) => {
    return (
      <option className="options" key={choice} value={choices[choice].name}>
        {choices[choice].name}
      </option>
    );
  });

  return (
    <form id={id}>
      <select
        className={className}
        id={id}
        defaultValue={initial}
        onChange={onChange}
      >
        {options}
      </select>
    </form>
  );
}