Skip to content

Commit b74447a

Browse files
committed
add handling of undefined/null options.
1 parent c31ab00 commit b74447a

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/sync.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ class SyncSelect extends Component {
1111
options: [],
1212
searchValue: '',
1313
}
14+
15+
testOptions = options =>
16+
typeof options === 'undefined' || options === null
17+
? []
18+
: options.map(this.transform)
19+
1420
componentWillMount() {
15-
const opts = this.props.options.map(this.transform)
21+
const opts = this.testOptions(this.props.options)
22+
1623
this.setState({
1724
sourceOptions: opts,
1825
options: this.computeOptions('', opts),
1926
})
2027
}
2128
componentWillReceiveProps(nextProps) {
22-
const newOpts = nextProps.options.map(this.transform)
29+
const newOpts = this.testOptions(nextProps.options)
2330
if (this.props.sourceOptions !== newOpts) {
2431
this.setState({
2532
sourceOptions: newOpts,

stories/async.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const createQuery = query =>
1717
query === ''
1818
? 'https://swapi.co/api/people'
1919
: `https://swapi.co/api/people/?search=${query}`
20-
const fakeApiCb = (query, cb) => fakeApi(query).then(r => cb(null, r.results))
20+
const fakeApiCb = (query, cb) => fakeApi(query).then(r => cb(null, r))
2121
const fakeApi = query =>
2222
fetch(createQuery(query))
2323
.then(response => response.json())
@@ -34,7 +34,7 @@ storiesOf('Async', module)
3434
transform={data => ({label: data.name, value: data.name})}>
3535
<Label />
3636
<List renderItem={Item} />
37-
</Container>,
37+
</Container>
3838
)
3939
.add('With promise', () =>
4040
<Container
@@ -52,7 +52,7 @@ storiesOf('Async', module)
5252
<List renderItem={<Item render={renderingItem} />} />
5353
</div>
5454
</div>
55-
</Container>,
55+
</Container>
5656
)
5757
.add('With custom debounce', () =>
5858
<Container
@@ -69,5 +69,5 @@ storiesOf('Async', module)
6969
<TagList renderTag={Tag} />
7070
<List renderItem={<Item render={renderingItem} />} />
7171
</div>
72-
</Container>,
72+
</Container>
7373
)

stories/sync.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ storiesOf('Sync', module)
8888
<List renderItem={Item} />
8989
</Container>
9090
)
91+
.add('Basic - Wraning on undefined options', () =>
92+
<Container name="context" onChange={onChange}>
93+
<Label />
94+
<List renderItem={Item} />
95+
</Container>
96+
)
9197
.add('Basic withCustom Key', () =>
9298
<Container
9399
name="context"

0 commit comments

Comments
 (0)