Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit f215e7b

Browse files
committed
Update Custom Menu example to use isItemSelectable
1 parent 7134228 commit f215e7b

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

Diff for: examples/custom-menu/app.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import DOM from 'react-dom'
33
import Autocomplete from '../../lib/index'
4-
import { fakeRequest } from '../../lib/utils'
4+
import { fakeCategorizedRequest } from '../../lib/utils'
55

66
class App extends React.Component {
77

@@ -13,7 +13,6 @@ class App extends React.Component {
1313
loading: false
1414
}
1515
this.requestTimer = null
16-
this.renderItems = this.renderItems.bind(this)
1716
}
1817

1918
render() {
@@ -22,29 +21,33 @@ class App extends React.Component {
2221
<h1>Custom Menu</h1>
2322
<p>
2423
While Autocomplete ships with a decent looking menu, you can control the
25-
look as well as the rendering of it. In this case we put headers for each
26-
letter of the alphabet.
24+
look as well as the rendering of it. In this example we'll group the states
25+
into the region where they belong.
2726
</p>
2827
<label htmlFor="states-autocomplete">Choose a state from the US</label>
2928
<Autocomplete
3029
value={this.state.value}
3130
inputProps={{ id: 'states-autocomplete' }}
32-
wrapperStyle={{ position: 'relative', display: 'inline-block' }}
3331
items={this.state.unitedStates}
3432
getItemValue={(item) => item.name}
3533
onSelect={(value, state) => this.setState({ value, unitedStates: [state] }) }
3634
onChange={(event, value) => {
3735
this.setState({ value, loading: true, unitedStates: [] })
3836
clearTimeout(this.requestTimer)
39-
this.requestTimer = fakeRequest(value, (items) => {
37+
this.requestTimer = fakeCategorizedRequest(value, (items) => {
4038
this.setState({ unitedStates: items, loading: false })
4139
})
4240
}}
4341
renderItem={(item, isHighlighted) => (
44-
<div
45-
className={`item ${isHighlighted ? 'item-highlighted' : ''}`}
46-
key={item.abbr}
47-
>{item.name}</div>
42+
item.header ?
43+
<div
44+
className="item item-header"
45+
key={item.header}
46+
>{item.header}</div>
47+
: <div
48+
className={`item ${isHighlighted ? 'item-highlighted' : ''}`}
49+
key={item.abbr}
50+
>{item.name}</div>
4851
)}
4952
renderMenu={(items, value) => (
5053
<div className="menu">
@@ -54,25 +57,14 @@ class App extends React.Component {
5457
<div className="item">Loading...</div>
5558
) : items.length === 0 ? (
5659
<div className="item">No matches for {value}</div>
57-
) : this.renderItems(items)}
60+
) : items}
5861
</div>
5962
)}
63+
isItemSelectable={(item) => !item.header}
6064
/>
6165
</div>
6266
)
6367
}
64-
65-
renderItems(items) {
66-
return items.map((item, index) => {
67-
const text = item.props.children
68-
if (index === 0 || items[index - 1].props.children.charAt(0) !== text.charAt(0)) {
69-
return [<div className="item item-header">{text.charAt(0)}</div>, item]
70-
}
71-
else {
72-
return item
73-
}
74-
})
75-
}
7668
}
7769

7870
DOM.render(<App/>, document.getElementById('container'))

0 commit comments

Comments
 (0)