LookupField.js 6.3 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom'; 
import LookupPicker from '../LookupPicker';
import cx from 'classnames';
import s from '../FormRender.scss';
import {post} from '../../../utils/fetch';
import {buildQuery,queryRecord,listRecord} from '../../../utils/formUtil';
 

class LookupField extends React.Component {
    constructor(props) {
        super(props);  
        this.changeChose=this.changeChose.bind(this);
        this.getOptions=this.getOptions.bind(this);
        this.loadOptions=this.loadOptions.bind(this);
        this.onFocus=this.onFocus.bind(this);
        this.state={
            options:[],
            choseNode:[],
            needLoad:true
        }  
    }
    static propTypes = {    
        formField:PropTypes.object,
        field:PropTypes.object,
        validateState:PropTypes.string,
        inlineFlag:PropTypes.bool,
        baseUrl:PropTypes.string,
        allFields:PropTypes.object,
        organizations:PropTypes.array,
        employees:PropTypes.array
    } 
    static defaultProps={
        organizations:[],
        employees:[]
    }
    loadOptions(){
        const {formField,fieldsConfig,field,allFields,service}=this.props,self=this;
        const {needLoad}=this.state;
        let filterCons=[];
        if(formField&&formField.related_form_name!='employee'&&formField.related_form_name!='organization'&&service){
            if(formField.related_lookup_key&&formField.related_lookup_key.length>0&&formField.related_lookup_value_from&&allFields[formField.related_lookup_value_from+'_ID'].value){
               filterCons=[{'key':formField.related_lookup_key+'_ID','value':allFields[formField.related_lookup_value_from+'_ID'].value}]; 
            } 
            if(fieldsConfig){
            
            }
            if(filterCons.length<1&&needLoad){
                listRecord(service,formField.related_form_name).then(function(data){ 
                    self.setState({
                        needLoad:false,
                        options:data.items
                    });
                }).catch(function(error){

                });
            }else if(needLoad){
                queryRecord(service,formField.related_form_name,buildQuery(filterCons))
                .then(function(data){ 
                    self.setState({
                        needLoad:false,
                        options:data.items
                    });
                }).catch(function(error){

                });
            } 
        }   
    }
    getOptions(){
        const {formField,organizations,employees}=this.props;
        let options=[];
        if(formField.related_form_name=='organization'){
            options=organizations;
        }else if(formField.related_form_name=='employee'){
            options=employees;
        }else{
            options=this.state.options;
        }
        return options;
    }
    componentDidMount(){ 

    }
    componentWillReceiveProps(nextProps){ 
       const {formField,fieldsConfig,field,allFields,service}=nextProps;
       if(formField&&formField.related_lookup_value_from&&allFields[formField.related_lookup_value_from+'_ID'].value!=this.props.allFields[formField.related_lookup_value_from+'_ID'].value){
            this.setState({
                needLoad:true
            })
       }
    }
    onFocus(e){
        const {needLoad}=this.state;
        const {formField,organizations,employees}=this.props;
        if(needLoad&&formField&&formField.related_form_name!='organization'&&formField.related_form_name!='employee'){
            this.changeChose('',[]);
            this.loadOptions(); 
        } 
    }
    changeChose(value,values){
        const {field}=this.props; 
        this.setState({
            choseNode:values
        });
        field.onChange(value);
    }
    renderSubField(){
        const {formField,inlineFlag}=this.props;
        const {choseNode}=this.state;
        return Object.keys(formField.related_sub_fields).map((key,i)=>{
            return (
                <div key={i} className={cx(s.field_wrap,'form-group',{ 
                    'row':inlineFlag
                })}> 
                    <label className={cx('control-label',{
                        'col-sm-3':inlineFlag
                    })}> {formField.related_sub_fields[key]['field_display_name']}</label>
                    <div className={cx(s.input_wrap,{
                        'col-sm-9':inlineFlag
                    })} > 
                        <div className={cx(s.placeholder_wrap,'form-control',s.sub_fields)}>
                            {choseNode[0][key]}
                        </div>
                    </div>
                </div>
            )
        }); 
    }
    render(){  
        const {formField,validateState,inlineFlag,field,fieldsConfig,type,fieldConfig}=this.props,self=this;  
        const options=JSON.parse(formField.options);   console.log("@@@@@"); console.log(fieldConfig);
        const {choseNode}=this.state;
        return( 
            <div>
                <div className={cx(s.field_wrap,'form-group',{
                    'has-success':validateState=='success',
                    'has-warning':validateState=='warning',
                    'has-error':validateState=='error',
                    'row':inlineFlag
                })}> 
                    <label className={cx('control-label',{
                        'col-sm-3':inlineFlag
                    })}>{formField.display_name}</label>
                    <div className={cx(s.input_wrap,{
                        'col-sm-9':inlineFlag
                    })} > 
                        <LookupPicker name={formField.name} value={field.value} choices={this.getOptions()} 
                        placeholder={options.tooltip} onChange={this.changeChose} onFocus={this.onFocus} 
                        multi={formField.display_type?formField.display_type=='multiselect':false}
                        labelKey={formField.related_display_field_name} valueKey='uuid'
                        error={validateState=='error'} disabled={fieldConfig.readOnly||'readOnly'==type}/>
                        <span className={cx(s.input_help,'help-block')}><small>{field.touched?field.error:''}</small></span>
                    </div>
                </div>  
                {(formField.related_sub_fields&&choseNode.length>0)&&this.renderSubField()}
            </div> 
        )
    }
}



export default LookupField;