Skip to content

Commit b5efa4d

Browse files
jeffcarbslevithomason
authored andcommitted
feat(Dropdown): Enable custom DropdownItem render (#854)
1 parent b0ed3b7 commit b5efa4d

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
import { Dropdown, Header } from 'semantic-ui-react'
3+
4+
const options = [
5+
{ text: 'Mobile',
6+
value: 1,
7+
content: <Header icon='mobile' content='Mobile' subheader='The smallest size' /> },
8+
{ text: 'Tablet',
9+
value: 2,
10+
content: <Header icon='tablet' content='Tablet' subheader='The size in the middle' /> },
11+
{ text: 'Desktop',
12+
value: 3,
13+
content: <Header icon='desktop' content='Desktop' subheader='The largest size' /> },
14+
]
15+
16+
const DropdownExampleItemContent = () => (
17+
<Dropdown
18+
selection
19+
fluid
20+
options={options}
21+
placeholder='Choose an option'
22+
/>
23+
)
24+
25+
export default DropdownExampleItemContent

docs/app/Examples/modules/Dropdown/Usage/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const DropdownUsageExamples = () => (
4444
description='A "multiple" dropdown can render customized label for selected items.'
4545
examplePath='modules/Dropdown/Usage/DropdownExampleMultipleCustomLabel'
4646
/>
47+
<ComponentExample
48+
title='Item Content'
49+
description='A dropdown item can be rendered differently inside the menu.'
50+
examplePath='modules/Dropdown/Usage/DropdownExampleItemContent'
51+
/>
4752

4853
</ExampleSection>
4954
)

src/modules/Dropdown/DropdownItem.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export default class DropdownItem extends Component {
3232
/** Additional classes. */
3333
className: PropTypes.string,
3434

35+
/** Shorthand for primary content. */
36+
content: customPropTypes.contentShorthand,
37+
3538
/** Additional text with less emphasis. */
3639
description: customPropTypes.itemShorthand,
3740

@@ -86,6 +89,7 @@ export default class DropdownItem extends Component {
8689
active,
8790
children,
8891
className,
92+
content,
8993
disabled,
9094
description,
9195
flag,
@@ -130,7 +134,7 @@ export default class DropdownItem extends Component {
130134
{flagElement}
131135
{labelElement}
132136
{descriptionElement}
133-
{createShorthand('span', val => ({ className: 'text', children: val }), text)}
137+
{createShorthand('span', val => ({ className: 'text', children: val }), content || text)}
134138
</ElementType>
135139
)
136140
}
@@ -141,7 +145,7 @@ export default class DropdownItem extends Component {
141145
{iconElement}
142146
{flagElement}
143147
{labelElement}
144-
{text}
148+
{content || text}
145149
</ElementType>
146150
)
147151
}

test/specs/modules/Dropdown/DropdownItem-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ describe('DropdownItem', () => {
4848
})
4949
})
5050

51+
describe('content', () => {
52+
it('renders text if no content', () => {
53+
const wrapper = shallow(<DropdownItem text='hey' />)
54+
55+
wrapper.text().should.include('hey')
56+
})
57+
58+
it('renders content if present', () => {
59+
const wrapper = shallow(<DropdownItem text='hey' content='you' />)
60+
61+
wrapper.text().should.not.include('hey')
62+
wrapper.text().should.include('you')
63+
})
64+
})
65+
5166
describe('onClick', () => {
5267
it('omitted when not defined', () => {
5368
const click = () => shallow(<DropdownItem />).simulate('click')
@@ -59,7 +74,7 @@ describe('DropdownItem', () => {
5974

6075
const value = faker.hacker.phrase()
6176
const event = { target: null }
62-
const props = { value, foo: 'bar' }
77+
const props = { value, 'data-foo': 'bar' }
6378

6479
shallow(<DropdownItem onClick={spy} {...props} />)
6580
.simulate('click', event)

0 commit comments

Comments
 (0)