Skip to content

Commit defe949

Browse files
docs(dropdown): explain itemToElement and itemToString relationship (#11822)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 3b3778d commit defe949

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

packages/react/src/components/Dropdown/Dropdown.mdx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ const items = ['Option 1', 'Option 2', 'Option 3']
115115

116116
The `Dropdown` takes in an `items` array and can then be formatted by
117117
`itemToElement` and `itemToString`. `itemToElement` allows you to wrap each
118-
dropdown item in a custom element.
118+
dropdown item in a custom element. The
119+
[`itemToString` prop](#dropdown-itemtostring) can be used with this to provide
120+
the text of the item in the input on selection.
119121

120122
<Dropdown
121123
items={[
@@ -161,6 +163,26 @@ dropdown item in a custom element.
161163
If the `items` array is not an array of strings, you'll need to use
162164
`itemToString` to grab the text to be used as the `Dropdown` item text.
163165

166+
<Dropdown
167+
items={[
168+
{ id: 'option-0', text: 'Option 0' },
169+
{ id: 'option-1', text: 'Option 1' },
170+
{ id: 'option-2', text: 'Option 2' },
171+
]}
172+
itemToString={(item) => (item ? item.text : '')}
173+
itemToElement={(item) =>
174+
item ? (
175+
<span className="test" style={{ color: 'red' }}>
176+
{item.text} 🔥
177+
</span>
178+
) : (
179+
''
180+
)
181+
}
182+
label="Select an option..."
183+
id="item-to-element"
184+
/>
185+
164186
```jsx
165187
<Dropdown
166188
items={[
@@ -169,6 +191,17 @@ If the `items` array is not an array of strings, you'll need to use
169191
{ id: 'option-2', text: 'Option 2' },
170192
]}
171193
itemToString={(item) => (item ? item.text : '')}
194+
itemToElement={(item) =>
195+
item ? (
196+
<span className="test" style={{ color: 'red' }}>
197+
{item.text} 🔥
198+
</span>
199+
) : (
200+
''
201+
)
202+
}
203+
label="Select an option..."
204+
id="item-to-element"
172205
/>
173206
```
174207

0 commit comments

Comments
 (0)