import React, {Component} from 'react'
import ComposedComponent from './ComposedComponent'
import MenuItem from '@material-ui/core/MenuItem'
import MuiSelect from '@material-ui/core/Select'
import InputLabel from '@material-ui/core/InputLabel'
import FormControl from '@material-ui/core/FormControl'
import { getValueFromModel} from './utils'

class Select extends Component {
    constructor(props) {
        super(props)
        this.state = {
            currentValue: getValueFromModel(this.props.model, this.props.form.key) || ''
        }
    }

    static getDerivedStateFromProps(props) {
        if (props.model && props.form.key) {
            return {
                currentValue: getValueFromModel(props.model, props.form.key)
            }
        }
    }

    onSelected = (event) => {
        let currentValue = event.target.value
        this.setState({currentValue})
        this.props.onChangeValidate(event)
    }

    render() {
        const {form} = this.props
        const menuItems = form.titleMap.map((item, idx) => (
            <MenuItem key={idx} value={item.value}>{item.name}</MenuItem>
        ))
        return (
            <FormControl fullWidth>
                <InputLabel>{form.title}</InputLabel>
                <MuiSelect
                    value={this.state.currentValue || ''}
                    placeholder={form.title}
                    disabled={form.readonly}
                    onChange={this.onSelected}>
                    {menuItems}
                </MuiSelect>
            </FormControl>
        )
    }
}

export default ComposedComponent(Select)

@@ -1,12 +1,21 @@
+// @flow%0A
 import React
@@ -17,16 +17,17 @@
 React, %7B
+ 
 Componen
@@ -31,74 +31,24 @@
 nent
+ 
 %7D from 
-'
+%22
 react
-'%0Aimport ComposedComponent from './ComposedComponent'
+%22;
 %0Aimp
@@ -61,25 +61,25 @@
 nuItem from 
-'
+%22
 @material-ui
@@ -92,17 +92,18 @@
 MenuItem
-'
+%22;
 %0Aimport 
@@ -113,25 +113,25 @@
 Select from 
-'
+%22
 @material-ui
@@ -142,17 +142,18 @@
 e/Select
-'
+%22;
 %0Aimport 
@@ -164,25 +164,25 @@
 tLabel from 
-'
+%22
 @material-ui
@@ -197,17 +197,18 @@
 putLabel
-'
+%22;
 %0Aimport 
@@ -224,17 +224,17 @@
 ol from 
-'
+%22
 @materia
@@ -254,17 +254,71 @@
 mControl
-'
+%22;%0Aimport ComposedComponent from %22./ComposedComponent%22;
 %0Aimport 
@@ -340,24 +340,143 @@
 odel
+ 
 %7D from 
-'
+%22
 ./utils
-'
+%22;%0A%0Atype Props = %7B%0A    model: any,%0A    form: any,%0A    onChangeValidate: any%0A%7D;%0A%0Atype State = %7B%0A    currentValue: any%0A%7D;
 %0A%0Acl
@@ -503,16 +503,30 @@
 omponent
+%3CProps, State%3E
  %7B%0A    c
@@ -565,16 +565,61 @@
 r(props)
+;%0A        const %7B model, form %7D = this.props;
 %0A       
@@ -682,37 +682,15 @@
 del(
-this.props.model, this.props.
+model, 
 form
@@ -698,18 +698,18 @@
 key) %7C%7C 
-''
+%22%22
 %0A       
@@ -702,32 +702,33 @@
  %7C%7C %22%22%0A        %7D
+;
 %0A    %7D%0A%0A    stat
@@ -916,24 +916,25 @@
            %7D
+;
 %0A        %7D%0A 
@@ -932,16 +932,37 @@
       %7D%0A
+        return null;%0A
     %7D%0A%0A 
@@ -977,23 +977,21 @@
 ected = 
-(
 event
-)
  =%3E %7B%0A  
@@ -996,18 +996,69 @@
         
-le
+const %7B onChangeValidate %7D = this.props;%0A        cons
 t curren
@@ -1084,16 +1084,17 @@
 et.value
+;
 %0A       
@@ -1109,16 +1109,17 @@
 tState(%7B
+ 
 currentV
@@ -1126,10 +1126,12 @@
 alue
+ 
 %7D)
+;
 %0A   
@@ -1139,19 +1139,8 @@
     
-this.props.
 onCh
@@ -1158,22 +1158,24 @@
 e(event)
+;
 %0A    %7D
+;
 %0A%0A    re
@@ -1198,20 +1198,22 @@
  const %7B
+ 
 form
+ 
 %7D = this
@@ -1218,16 +1218,62 @@
 is.props
+;%0A        const %7B currentValue %7D = this.state;
 %0A       
@@ -1342,58 +1342,153 @@
     
-%3CMenuItem key=%7Bidx%7D value=%7Bitem.value%7D%3E%7Bitem.name%7D
+// eslint-disable-next-line react/no-array-index-key%0A            %3CMenuItem key=%7Bidx%7D value=%7Bitem.value%7D%3E%0A                %7Bitem.name%7D%0A            
 %3C/Me
@@ -1505,16 +1505,17 @@
       ))
+;
 %0A       
@@ -1672,19 +1672,8 @@
 ue=%7B
-this.state.
 curr
@@ -1688,10 +1688,10 @@
  %7C%7C 
-''
+%22%22
 %7D%0A  
@@ -1824,16 +1824,33 @@
 elected%7D
+%0A                
 %3E%0A      
@@ -1940,16 +1940,17 @@
        )
+;
 %0A    %7D%0A%7D
@@ -1991,9 +1991,10 @@
 (Select)
+;
 %0A

fix eslint errors