MCPcopy Create free account
hub / github.com/nodejs/nodejs.org / Select

Function Select

packages/ui-components/src/Common/Select/index.tsx:55–197  ·  view source on GitHub ↗
({
  values = [],
  value,
  placeholder,
  label,
  inline,
  onChange,
  className,
  dropdownClassName,
  ariaLabel,
  loading = false,
  disabled = false,
  fallbackClass = '',
}: SelectProps<T>)

Source from the content-addressed store, hash-verified

53};
54
55const Select = <T extends string>({
56 values = [],
57 value,
58 placeholder,
59 label,
60 inline,
61 onChange,
62 className,
63 dropdownClassName,
64 ariaLabel,
65 loading = false,
66 disabled = false,
67 fallbackClass = '',
68}: SelectProps<T>): ReactNode => {
69 const id = useId();
70
71 const mappedValues = useMemo(() => mapValues(values), [values]) as Array<
72 SelectGroup<T>
73 >;
74
75 // We render the actual item slotted to fix/prevent the issue
76 // of the tirgger flashing on the initial render
77 const currentItem = useMemo(
78 () =>
79 mappedValues
80 .flatMap(({ items }) => items)
81 .find(item => item.value === value),
82 [mappedValues, value]
83 );
84
85 const memoizedMappedValues = useMemo(() => {
86 return mappedValues.map(({ label, items }, key) => (
87 <SelectPrimitive.Group key={label?.toString() ?? key}>
88 {label && (
89 <SelectPrimitive.Label
90 className={classNames(styles.item, styles.label)}
91 >
92 {label}
93 </SelectPrimitive.Label>
94 )}
95
96 {items.map(({ value, label, iconImage, badge, disabled }) => (
97 <SelectPrimitive.Item
98 key={value}
99 value={value}
100 disabled={disabled}
101 className={classNames(styles.item, styles.text)}
102 >
103 <SelectPrimitive.ItemText>
104 {iconImage}
105 <span>{label}</span>
106 {badge && (
107 <Badge size="small" kind={badge.kind} className={styles.badge}>
108 {badge.label}
109 </Badge>
110 )}
111 </SelectPrimitive.ItemText>
112 </SelectPrimitive.Item>

Callers

nothing calls this directly

Calls 1

mapValuesFunction · 0.90

Tested by

no test coverage detected