File size: 2,272 Bytes
210312d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { ReviewDomain } from "../types";

interface DomainOption {
  value: ReviewDomain;
  label: string;
}

interface DomainMultiSelectProps {
  options: DomainOption[];
  value: ReviewDomain[];
  onChange: (value: ReviewDomain[]) => void;
}

/** Checkbox panel for choosing one or more review domains. Selected values are
 * always kept in the order of the options, so the same choice never looks like
 * a change just because it was ticked in a different order. */
export function DomainMultiSelect({ options, value, onChange }: DomainMultiSelectProps) {
  const allSelected = options.length > 0 && value.length === options.length;

  function commit(next: ReviewDomain[]) {
    onChange(options.filter((o) => next.includes(o.value)).map((o) => o.value));
  }

  function toggle(domain: ReviewDomain) {
    commit(value.includes(domain) ? value.filter((v) => v !== domain) : [...value, domain]);
  }

  const summary =
    value.length === 0
      ? "Choose review domain(s)"
      : allSelected
        ? "All domains selected"
        : `${value.length} selected`;

  const chosenLabels = options.filter((o) => value.includes(o.value)).map((o) => o.label);

  return (
    <details className="domain-multiselect">
      <summary>{summary}</summary>
      <div className="domain-multiselect-actions">
        <button
          type="button"
          className="change-btn"
          disabled={allSelected || options.length === 0}
          onClick={() => commit(options.map((o) => o.value))}
        >
          Select all
        </button>
        <button
          type="button"
          className="change-btn"
          disabled={value.length === 0}
          onClick={() => commit([])}
        >
          Clear
        </button>
      </div>
      <div className="domain-multiselect-list">
        {options.map((o) => (
          <label key={o.value} className="source-checkbox">
            <input
              type="checkbox"
              checked={value.includes(o.value)}
              onChange={() => toggle(o.value)}
            />
            {o.label}
          </label>
        ))}
      </div>
      {chosenLabels.length > 0 && !allSelected && (
        <div className="form-hint">{chosenLabels.join(", ")}</div>
      )}
    </details>
  );
}