summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/inputs/Selector.tsx
blob: ba253ae648c61c77e0d2117ce576a251a3aa4c14 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { LOG } from "@/utilities/console";
import {
  MultiSelect,
  MultiSelectProps,
  Select,
  SelectItem,
  SelectProps,
} from "@mantine/core";
import { isNull, isUndefined } from "lodash";
import { useCallback, useMemo, useRef } from "react";

export type SelectorOption<T> = Override<
  {
    value: T;
    label: string;
  },
  SelectItem
>;

type SelectItemWithPayload<T> = SelectItem & {
  payload: T;
};

function DefaultKeyBuilder<T>(value: T) {
  if (typeof value === "string") {
    return value;
  } else if (typeof value === "number") {
    return value.toString();
  } else {
    LOG("error", "Unknown value type", value);
    throw new Error(
      `Invalid type (${typeof value}) in the SelectorOption, please provide a label builder`
    );
  }
}

export type SelectorProps<T> = Override<
  {
    value?: T | null;
    defaultValue?: T | null;
    options: SelectorOption<T>[];
    onChange?: (value: T | null) => void;
    getkey?: (value: T) => string;
  },
  Omit<SelectProps, "data">
>;

export function Selector<T>({
  value,
  defaultValue,
  options,
  onChange,
  getkey = DefaultKeyBuilder,
  ...select
}: SelectorProps<T>) {
  const keyRef = useRef(getkey);
  keyRef.current = getkey;

  const data = useMemo(
    () =>
      options.map<SelectItemWithPayload<T>>(({ value, label, ...option }) => ({
        label,
        value: keyRef.current(value),
        payload: value,
        ...option,
      })),
    [keyRef, options]
  );

  const wrappedValue = useMemo(() => {
    if (isNull(value) || isUndefined(value)) {
      return value;
    } else {
      return keyRef.current(value);
    }
  }, [keyRef, value]);

  const wrappedDefaultValue = useMemo(() => {
    if (isNull(defaultValue) || isUndefined(defaultValue)) {
      return defaultValue;
    } else {
      return keyRef.current(defaultValue);
    }
  }, [defaultValue, keyRef]);

  const wrappedOnChange = useCallback(
    (value: string) => {
      const payload = data.find((v) => v.value === value)?.payload ?? null;
      onChange?.(payload);
    },
    [data, onChange]
  );

  return (
    <Select
      data={data}
      defaultValue={wrappedDefaultValue}
      value={wrappedValue}
      onChange={wrappedOnChange}
      {...select}
    ></Select>
  );
}

export type MultiSelectorProps<T> = Override<
  {
    value?: readonly T[];
    defaultValue?: readonly T[];
    options: readonly SelectorOption<T>[];
    onChange?: (value: T[]) => void;
    getkey?: (value: T) => string;
    buildOption?: (value: string) => T;
  },
  Omit<MultiSelectProps, "data">
>;

export function MultiSelector<T>({
  value,
  defaultValue,
  options,
  onChange,
  getkey = DefaultKeyBuilder,
  buildOption,
  ...select
}: MultiSelectorProps<T>) {
  const labelRef = useRef(getkey);
  labelRef.current = getkey;

  const buildRef = useRef(buildOption);
  buildRef.current = buildOption;

  const data = useMemo(
    () =>
      options.map<SelectItemWithPayload<T>>(({ value, ...option }) => ({
        value: labelRef.current(value),
        payload: value,
        ...option,
      })),
    [options]
  );

  const wrappedValue = useMemo(
    () => value && value.map(labelRef.current),
    [value]
  );
  const wrappedDefaultValue = useMemo(
    () => defaultValue && defaultValue.map(labelRef.current),
    [defaultValue]
  );

  const wrappedOnChange = useCallback(
    (values: string[]) => {
      const payloads: T[] = [];
      for (const value of values) {
        const payload = data.find((v) => v.value === value)?.payload;
        if (payload) {
          payloads.push(payload);
        } else if (buildRef.current) {
          payloads.push(buildRef.current(value));
        }
      }
      onChange?.(payloads);
    },
    [data, onChange]
  );

  return (
    <MultiSelect
      {...select}
      value={wrappedValue}
      defaultValue={wrappedDefaultValue}
      onChange={wrappedOnChange}
      data={data}
    ></MultiSelect>
  );
}