import React,{Component} from 'react'

import { Select as Select1 } from 'antd';
import FormItem from './FormItem'

const Option = Select1.Option;
const styleWidth = {width:'100%'}
export  class Select extends Component{
    onChange=val=>{
        this.props.onChange && this.props.onChange(val)
        this.props.onAfterChange && this.props.onAfterChange(val)
    }
    render()
    {

        const {formBlur,code,style,disabled,...rest} = this.props
        let mixstyle = {...styleWidth,...style}
        return (<Select1 size="small" style={mixstyle} allowClear={true} {...rest} disabled={disabled} onChange={this.onChange}> 
                {
                    this.props.code && this.props.code.map(item=>{
                       return  (
                        <Select1.Option value={item.value} key={item.value} >{item.label}</Select1.Option>
                       )
                    })
                }
                
            </Select1>
        )
    }
}

export  class SelectItem extends Component{
    constructor(props)
    {
        super(props)
        this.state = {hadBlur:false}
    }
    shouldComponentUpdate(nextProps,nextState)
    {
        if(isEquals(nextProps,this.props) && isEquals(nextState,this.state))
            return false
        return true;
    }
    setBlur=()=>{
        if(!this.state.hadBlur)
            this.setState({hadBlur:true})
        this.props.onBlur && this.props.onBlur()
    }
    render()
    {
        const {InputProps,ItemProps,disabled} = this.props
        const  help = (InputProps && InputProps.formBlur) || this.state.hadBlur ? InputProps.help :''
        let {code} = InputProps
        if(this.props.code)
            code = this.props.code
        
        const {inDisplay,displayChanged,...rest} =  InputProps
        return (
            <FormItem validateStatus={help?"error":null}  help={help} {...ItemProps}>
                <Select {...rest} onBlur={this.setBlur} code={code} disabled={disabled} style={{color:displayChanged?'red':undefined}} />
            </FormItem>
        )
    }
}