RadioBoxGroup.js 2.8 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';  
import {Input,FormControls} from 'react-bootstrap'; 
import cx from 'classnames';
import s from './CustomForm.scss';


class RadioBoxGroup extends React.Component {
    constructor(props) {
        super(props);    
        this.changeRadio=this.changeRadio.bind(this);
    }
    static propTypes = {    
        displayName:PropTypes.string,
        field:PropTypes.object, 
        inlineFlag:PropTypes.bool,
        placeholder:PropTypes.string,
        options:PropTypes.array,
        style:PropTypes.object 
    } 
    static defaultProps={
        inlineFlag:false 
    }
    validationState(field){
        if(field.error&&field.touched){
            return 'error';
        }else if(field.touched)
            return ''; 
    }
    changeRadio(e){ 
        const {field}=this.props; 
        if("true"==e.target.value){
            field.onChange(true);  
        }else if('false'==e.target.value){
            field.onChange(false);
        }
    }
    render(){  
        const {style,displayName,inlineFlag,field,placeholder,options,name,labelStyle}=this.props,self=this;  
        let validateState=this.validationState(field); 
        return( 
            <div className={cx(s.field_wrap,'form-group',{
                'has-success':validateState=='success',
                'has-warning':validateState=='warning',
                'has-error':validateState=='error',
                'row':inlineFlag
            })} style={style}> 
                <label className={cx('control-label',{
                    'col-sm-3':inlineFlag
                })} style={labelStyle}>{displayName}</label>
                <div className={cx(s.input_wrap,{
                    'col-sm-9':inlineFlag
                })} > 
                    <div className={cx(s.checkbox_group_wrap)}  onChange={field.onChange} placeholder={placeholder} value={field.value}>
                    {
                        options.map((option,i)=>{ 
                            return (
                                <div key={i} className={cx(s.checkbox_wrap,'checkbox')}>
                                    <label  >
                                        <input name={name} type="radio" checked={(field.value+"")==option.value.toString()} value={option.value} onChange={this.changeRadio} />
                                        <span>{option.label}</span>
                                    </label>
                                </div>
                            );
                        })
                    }
                    </div>
                    <span className={cx(s.input_help,'help-block')}><small>{field.touched?field.error:''}</small></span>
                </div>
            </div> 
        )
    }
}



export default RadioBoxGroup;