Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
import { useEffect, useMemo, useState } from "react";
import { cn } from "../../../../utils/bem";
import { FormField } from "../../FormField";
import { default as Label } from "../Label/Label";
import { Select as SelectUI } from "@humansignal/ui";
 
const SelectOption = ({ value, label, disabled = false, hidden = false, ...props }) => {
  return (
    <option value={value} disabled={disabled} hidden={hidden} {...props}>
      {label ?? value}
    </option>
  );
};
 
const Select = ({ label, className, options, validate, required, skip, labelProps, groupProps, ghost, ...props }) => {
  const rootClass = cn("select-ls");
  const initialValue = useMemo(() => props.value ?? "", [props.value]);
  const [value, setValue] = useState(initialValue);
 
  const grouped = options.reduce((groupedOptions, option) => {
    const key = option.group || "NoGroup"; // fallback group for items without a group property
 
    (groupedOptions[key] = groupedOptions[key] || []).push(option);
    return groupedOptions;
  }, {});
 
  const classList = rootClass.mod({ ghost }).mix(className);
 
  useEffect(() => {
    setValue(initialValue);
  }, [initialValue]);
 
  const selectOptions = useMemo(() => {
    return Object.keys(grouped).flatMap((group) => {
      return group === "NoGroup"
        ? grouped[group]
        : (grouped[group] = {
            label: group,
            children: grouped[group],
          });
    });
  }, [grouped]);
 
  const selectWrapper = (
    <FormField
      name={props.name}
      label={label}
      validate={validate}
      required={required}
      skip={skip}
      setValue={(val) => setValue(val)}
      {...props}
    >
      {(ref) => {
        return (
          <SelectUI
            {...props}
            value={value}
            onChange={(val) => {
              setValue(val);
              props.onChange?.(val);
            }}
            ref={ref}
            options={selectOptions}
          />
        );
      }}
    </FormField>
  );
 
  return label ? (
    <Label {...(labelProps ?? {})} text={label} required={required}>
      {selectWrapper}
    </Label>
  ) : (
    selectWrapper
  );
};
 
export default Select;