DropdownPicker.js 1.6 KB
import React,{PropTypes} from 'react'; 
import Select from 'react-select';
import 'react-select/dist/react-select.css'; 
import cx from 'classnames';
import s from './FormRender.scss';


class DropdownPicker extends React.Component {
	constructor (props, context) {
	    super(props, context);
	    this.changeDropdown=this.changeDropdown.bind(this); 
	    
	}
	static propTypes={
		onChange: PropTypes.func,
		choices:PropTypes.array,
		value:PropTypes.oneOfType([
	      PropTypes.string,
	      PropTypes.array,
	      PropTypes.object
	    ]),
		placeholder:PropTypes.string,
		name:PropTypes.string,
		multi:PropTypes.bool,
		error:PropTypes.bool,
		disabled:PropTypes.bool,
		labelKey:PropTypes.string,
		valueKey:PropTypes.string
	} 
	static defaultProps={
        labelKey:'label',
        valueKey:'value',
        choices:[]
    }
	changeDropdown(value,values){
		this.props.onChange(value,values);
	} 
	filterOption(){
		const {choices,userInfo}=this.props;
		if(userInfo){
			const temp=[]
			choices.map(choice=>{
				if(choice.uuid!=userInfo.user_id)
					temp.push(choice);
			})
			return temp;
		}else{
			return choices;
		}
	}
	render(){
		return( 
			<Select 
				className={this.props.error?s.has_error:''}
				name={this.props.name}
			    value={this.props.value}
			    noResultsText='没找到' 
			    multi={this.props.multi}
			    disabled={this.props.disabled}
			    placeholder={this.props.placeholder}
			    options={this.filterOption()}
			    labelKey={this.props.labelKey}
			    valueKey={this.props.valueKey}
			    onChange={this.changeDropdown} /> 
		)
	}
}
 


export default DropdownPicker;