11import React , { Component , PropTypes } from 'react' ;
2+ import FormGroup from '../FormGroup' ;
23import Input from '../Input' ;
4+ import ReSelect from 'react-select' ;
5+ import fetch from 'isomorphic-fetch' ;
36
47class 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