Skip to content

Commit 4ef81ab

Browse files
author
jonisaa
committed
#15 adding select with url options, added isomorphic-fetch in package.json
1 parent d52d993 commit 4ef81ab

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"homepage": "https://github.com/redux-autoform/redux-autoform-bootstrap-ui#readme",
2929
"dependencies": {
3030
"bootstrap": "^3.3.6",
31+
"isomorphic-fetch": "^2.2.1",
3132
"react": "^15.1.0",
3233
"react-bootstrap": "^0.30.0-rc.1",
3334
"react-dom": "^15.2.1",

src/components/fieldComponents/Select.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React, { Component, PropTypes } from 'react';
2+
import FormGroup from '../FormGroup';
23
import Input from '../Input';
4+
import ReSelect from 'react-select';
5+
import fetch from 'isomorphic-fetch';
36

47
class Select extends Component {
58
static propTypes = {
@@ -23,14 +26,36 @@ class Select extends Component {
2326
));
2427
};
2528

29+
fetchItems = () => {
30+
const { options } = this.props;
31+
32+
return fetch(options.url)
33+
.then(response => response.json())
34+
.then(json => {
35+
return { options: json }
36+
});
37+
};
38+
2639
render() {
27-
let options = this.getOptions();
28-
29-
return (
30-
<Input componentClass="select" {...this.props}>
31-
{ options }
32-
</Input>
33-
);
40+
let { value, name, displayName, help, error, touched, onChange, onBlur, fieldLayout, options } = this.props;
41+
let formGroupProps = { error, touched, displayName, name, help, fieldLayout };
42+
let selectProps = { value, name, onChange, onBlur, loadOptions: this.fetchItems, valueKey: options.value? options.value : 'value', labelKey: options.label? options.label : 'label' };
43+
44+
if (!options.url && Array.isArray(options)) {
45+
return (
46+
<Input componentClass="select" {...this.props}>
47+
{ this.getOptions() }
48+
</Input>
49+
);
50+
} else if (options.url) {
51+
return (
52+
<FormGroup {...formGroupProps}>
53+
<ReSelect.Async {...selectProps}/>
54+
</FormGroup>
55+
);
56+
}
57+
58+
return false;
3459
}
3560
}
3661

0 commit comments

Comments
 (0)