Skip to content

Commit ae87ec0

Browse files
committed
chore: add CollectionItem docs * 3
1 parent 0cf2dfc commit ae87ec0

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

src/components/CollectionItem.docs.mdx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ListBox, FilterListBox, Select, ComboBox, FilterPicker, Picker, Menu, CommandMen
1212

1313
## Usage Patterns
1414

15+
There are two ways to provide items to collection components:
16+
1517
### Static JSX
1618

1719
Define items directly. You **must** provide `key` props:
@@ -25,9 +27,10 @@ Define items directly. You **must** provide `key` props:
2527

2628
### Data-driven (Recommended)
2729

28-
Use `items` prop with render function. You **must** pass `key` explicitly:
30+
**🚨 IMPORTANT:** The `items` prop **must be an array of objects**, where each object contains at least an `id` or `key` property.
2931

3032
```jsx live=false
33+
// ✅ Correct: Array of objects with id/key
3134
const items = [
3235
{ id: 'apple', name: 'Apple' },
3336
{ id: 'banana', name: 'Banana' },
@@ -38,6 +41,14 @@ const items = [
3841
</ListBox>
3942
```
4043

44+
```jsx live=false
45+
// ❌ Wrong: Array of strings won't work
46+
const items = ['apple', 'banana']; // Don't do this!
47+
48+
// ❌ Wrong: Array of primitives won't work
49+
const items = [1, 2, 3]; // Don't do this!
50+
```
51+
4152
## Item Requirements
4253

4354
| Property | Required | Type | Purpose |
@@ -181,15 +192,21 @@ const items = [
181192
## Best Practices
182193

183194
```jsx live=false
184-
//Always provide id/key in data
195+
//Items must be an array of objects
185196
const items = [{ id: 'user-1', name: 'John' }];
186197

187-
// ✅ Always pass key prop
198+
// ✅ Always provide id/key in each object
199+
const items = [
200+
{ id: 'apple', name: 'Apple' },
201+
{ id: 'banana', name: 'Banana' },
202+
];
203+
204+
// ✅ Always pass key prop to Item
188205
<ListBox items={items}>
189206
{(item) => <ListBox.Item key={item.id}>{item.name}</ListBox.Item>}
190207
</ListBox>
191208

192-
// ✅ Consistent types
209+
// ✅ Consistent types for id/key
193210
const items = [
194211
{ id: '1', name: 'First' },
195212
{ id: '2', name: 'Second' },
@@ -208,11 +225,23 @@ const items = useMemo(
208225
[data]
209226
);
210227

228+
// ❌ Don't pass array of strings
229+
const items = ['apple', 'banana']; // Wrong!
230+
231+
// ❌ Don't pass array of numbers
232+
const items = [1, 2, 3]; // Wrong!
233+
211234
// ❌ Don't omit key prop
212235
<ListBox items={items}>
213236
{(item) => <ListBox.Item>{item.name}</ListBox.Item>}
214237
</ListBox>
215238

239+
// ❌ Don't omit id/key in objects
240+
const items = [
241+
{ name: 'First' }, // Missing id!
242+
{ name: 'Second' }, // Missing id!
243+
];
244+
216245
// ❌ Don't mix types
217246
const items = [
218247
{ id: 1, name: 'First' },
@@ -259,10 +288,11 @@ const items: User[] = [
259288

260289
## Essential Rules
261290

262-
1. **🚨 Always provide `id` or `key` in data** - Required for selection callbacks to work properly. Without it, React Aria uses order-based keys which are useless in select components.
263-
2. **🚨 Always pass `key` prop to Item** - Must explicitly pass `key={item.id}` or `key={item.key}` in the render function
264-
3. **Provide explicit `textValue`** - For icons, complex content, or searchable components
265-
4. **Use consistent types** - All strings or all numbers, never mixed
291+
1. **🚨 The `items` prop must be an array of objects** - Never pass arrays of strings, numbers, or other primitives. Each object must have an `id` or `key` property.
292+
2. **🚨 Always provide `id` or `key` in each object** - Required for selection callbacks to work properly. Without it, React Aria uses order-based keys which are useless in select components.
293+
3. **🚨 Always pass `key` prop to Item** - Must explicitly pass `key={item.id}` or `key={item.key}` in the render function
294+
4. **Provide explicit `textValue`** - For icons, complex content, or searchable components
295+
5. **Use consistent types** - All strings or all numbers, never mixed
266296

267297
## Related Components
268298

0 commit comments

Comments
 (0)