@codecademy/gamut 72.2.3-alpha.e393cd.0 → 72.2.3-alpha.e7e7ca.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,236 +0,0 @@
1
- ---
2
- name: gamut-select-dropdown
3
- description: Use when implementing or auditing SelectDropdown — single/multi modes, controlled vs uncontrolled value, creatable options, FormGroup wiring, and onChange contract. Pair with gamut-forms for error live regions, ConnectedForm, and field-level validation.
4
- ---
5
-
6
- # Gamut SelectDropdown
7
-
8
- Styled dropdown built on react-select.
9
-
10
- Source: `@codecademy/gamut` — [SelectDropdown.tsx](https://github.com/Codecademy/gamut/blob/main/packages/gamut/src/Form/SelectDropdown/SelectDropdown.tsx)
11
-
12
- See also: [`gamut-forms`](../gamut-forms/SKILL.md) — FormGroup wiring, error regions, and validation UX.
13
-
14
- Storybook: [Atoms / FormInputs / SelectDropdown](https://gamut.codecademy.com/?path=/docs-atoms-forminputs-selectdropdown--docs)
15
-
16
- ---
17
-
18
- ## When to use SelectDropdown vs Select
19
-
20
- Use `Select` for standard single-select forms with minimal bundle cost. Use `SelectDropdown` when designs specify the styled dropdown menu, search, multi-select tags, creatable options, icons, groups, or abbreviations. SelectDropdown has a larger JavaScript dependency (react-select).
21
-
22
- ---
23
-
24
- ## Options
25
-
26
- `options` accepts plain strings or option objects. `value` is always a string and references an option's `value`.
27
-
28
- | Field | Required | Notes |
29
- | -------------- | -------- | -------------------------------------------------------------------- |
30
- | `label` | yes | Display text |
31
- | `value` | yes | Unique string; what `value` / `string[]` reference |
32
- | `disabled` | no | Option cannot be selected |
33
- | `subtitle` | no | Secondary text below the label |
34
- | `rightLabel` | no | Text on the right side of the option |
35
- | `icon` | no | A `@codecademy/gamut-icons` component |
36
- | `abbreviation` | no | Short text shown in the input while the full label shows in the menu |
37
-
38
- Grouped options: `{ label, options: [...], divider? }` (extends react-select `GroupBase`; `divider` draws a rule above the group).
39
-
40
- ---
41
-
42
- ## Controlled vs uncontrolled
43
-
44
- SelectDropdown does **not** accept `defaultValue`.
45
-
46
- | Mode | Uncontrolled | Controlled |
47
- | ---------------- | -------------------------------------------------- | --------------------------------------------------------------------------------- |
48
- | Single | Not supported | `value` (string) + update in `onChange` |
49
- | Multi | Omit `value` or pass non-array (`undefined`, `''`) | `value: string[]` + update in `onChange` |
50
- | Creatable single | Not supported | Same as single; `onCreateOption` appends to `options` |
51
- | Creatable multi | Omit `value`; `onCreateOption` for options | `value: string[]`; update in `onChange` on every change including `create-option` |
52
-
53
- Single-select selection is derived from the `value` prop only — internal state is not kept. Multi-select without `value: string[]` keeps selection in internal `multiValues`.
54
-
55
- **Controlled creatable multi pitfall:** Updating `options` alone without syncing `value` in `onChange` clears selection when options re-render.
56
-
57
- ### When to use uncontrolled (multi only)
58
-
59
- Uncontrolled multi is appropriate when:
60
-
61
- - No other part of the UI needs to react to the current selection (no live summary, no dependent field, no enabled/disabled button).
62
- - You only need the value at form submission — via `FormData`, a submit handler reading the DOM, or react-hook-form's `getValues`.
63
- - Simplicity is the priority; omitting `value` means one less piece of state to manage.
64
-
65
- ```tsx
66
- // Good fit: a "tags" field where only the submitted array matters
67
- <SelectDropdown
68
- multiple
69
- name="tags"
70
- options={tagOptions}
71
- onCreateOption={(v) => setTagOptions((prev) => [...prev, v])}
72
- />
73
- ```
74
-
75
- ### When to use controlled
76
-
77
- Use controlled when:
78
-
79
- - Another part of the UI must reflect the current selection in real time (summary text, a filtered list, an enable/disable condition).
80
- - You need to pre-populate from an API response, reset on cancel, or sync with a form library like react-hook-form.
81
- - You are using single-select (the only supported mode for single).
82
-
83
- ```tsx
84
- // Good fit: pre-populate from API, clear on cancel, show live summary
85
- const [selected, setSelected] = useState<string[]>(initialValues);
86
-
87
- <SelectDropdown
88
- multiple
89
- name="languages"
90
- options={languageOptions}
91
- value={selected}
92
- onChange={(opts) => setSelected(opts.map((o) => o.value))}
93
- />
94
- <p>Selected: {selected.join(', ') || 'none'}</p>
95
- ```
96
-
97
- ---
98
-
99
- ## onChange contract
100
-
101
- `onChange` receives option object(s), not `event.target.value`:
102
-
103
- ```tsx
104
- // Single
105
- onChange={(option) => setValue(option.value)}
106
-
107
- // Multi
108
- onChange={(selected) => setValue(selected.map((o) => o.value))}
109
- ```
110
-
111
- Second argument is react-select `ActionMeta`. For creatable creates: `meta.action === 'create-option'`. Do **not** pass `onCreateOption` to react-select directly — Gamut invokes it from `changeHandler` while still forwarding `create-option` to consumer `onChange`.
112
-
113
- ---
114
-
115
- ## Creatable
116
-
117
- - `isCreatable` forces `isSearchable: true` (TypeScript enforces this).
118
- - `onCreateOption(inputValue)` — convenience hook to append to `options`.
119
- - `onChange(selected, meta)` — use `meta.action === 'create-option'` to sync controlled `value` and `options` together.
120
- - `isValidNewOption` — return `false` to hide the Add row.
121
- - `validationMessage` — replaces menu "No options" text; mirror in `FormGroup` `error` for field-level feedback.
122
-
123
- **Validation after blur:** react-select clears input on blur before `onBlur` fires, so the value is gone by the time you'd validate it. Store the last typed value in a ref and re-validate from it on `input-blur`:
124
-
125
- ```tsx
126
- const lastInput = useRef('');
127
-
128
- <SelectDropdown
129
- isCreatable
130
- onInputChange={(value, { action }) => {
131
- if (action === 'input-change') lastInput.current = value;
132
- if (action === 'input-blur') validate(lastInput.current);
133
- }}
134
- />;
135
- ```
136
-
137
- ---
138
-
139
- ## FormGroup wiring
140
-
141
- - `FormGroup` `htmlFor` must match control `id` (not `name`). Alternatively, pass `htmlFor` directly on SelectDropdown and it becomes `id` downstream.
142
- - Pass `name` on SelectDropdown (required for forms).
143
- - Pass `aria-label` (required for forms); it must match the FormGroupLabel `htmlFor`.
144
- - Pass `error` boolean when FormGroup has an error.
145
- - Generic FormGroup live-region behavior: see [`gamut-forms`](../gamut-forms/SKILL.md).
146
-
147
- ```tsx
148
- <FormGroup htmlFor="country" isSoloField label="Country" error={errors.country}>
149
- <SelectDropdown
150
- id="country"
151
- name="country"
152
- aria-label="country"
153
- options={options}
154
- value={value}
155
- error={Boolean(errors.country)}
156
- onChange={(option) => setValue(option.value)}
157
- />
158
- </FormGroup>
159
- ```
160
-
161
- ---
162
-
163
- ## Styling & layout props
164
-
165
- | Prop | Type | Default | Notes |
166
- | ------------------- | ------------------------ | -------- | --------------------------------------------------------- |
167
- | `size` | `'small' \| 'medium'` | `medium` | Control height/density |
168
- | `shownOptionsLimit` | `1`–`6` | `6` | Visible options before the menu scrolls |
169
- | `inputWidth` | `string \| number` | — | Width of the input independent of the menu |
170
- | `dropdownWidth` | `string \| number` | — | Width of the menu independent of the input |
171
- | `menuAlignment` | `'left' \| 'right'` | `left` | Menu edge alignment |
172
- | `zIndex` | `number` | auto | Menu z-index |
173
- | `inputProps` | `{ hidden?, combobox? }` | — | `data-*` / `aria-*` only, forwarded to the input elements |
174
-
175
- ---
176
-
177
- ## Examples
178
-
179
- ### Single (controlled)
180
-
181
- ```tsx
182
- const [value, setValue] = useState('us');
183
-
184
- <SelectDropdown
185
- name="country"
186
- options={options}
187
- value={value}
188
- onChange={(option) => setValue(option.value)}
189
- />;
190
- ```
191
-
192
- ### Multi (uncontrolled)
193
-
194
- ```tsx
195
- <SelectDropdown
196
- multiple
197
- name="tags"
198
- options={options}
199
- onChange={(selected) => console.log(selected)}
200
- />
201
- ```
202
-
203
- ### Creatable multi (uncontrolled)
204
-
205
- ```tsx
206
- const [options, setOptions] = useState(['Apple', 'Banana']);
207
-
208
- <SelectDropdown
209
- isCreatable
210
- multiple
211
- name="fruits"
212
- options={options}
213
- onCreateOption={(v) => setOptions((prev) => [...prev, v])}
214
- />;
215
- ```
216
-
217
- ### Creatable multi (controlled)
218
-
219
- ```tsx
220
- const [options, setOptions] = useState(['Apple', 'Banana']);
221
- const [value, setValue] = useState<string[]>([]);
222
-
223
- <SelectDropdown
224
- isCreatable
225
- multiple
226
- name="fruits"
227
- options={options}
228
- value={value}
229
- onChange={(selected, meta) => {
230
- setValue(selected.map((o) => o.value));
231
- if (meta.action === 'create-option' && meta.option) {
232
- setOptions((prev) => [...prev, meta.option.value]);
233
- }
234
- }}
235
- />;
236
- ```