const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); require('./combobox.less'); const Combobox = createClass({ displayName : 'Combobox', getDefaultProps : function() { return { className : '', trigger : 'hover', default : '', placeholder : '', autoSuggest : { clearAutoSuggestOnClick : true, suggestMethod : 'includes', filterOn : [] // should allow as array to filter on multiple attributes, or even custom filter }, }; }, getInitialState : function() { return { showDropdown : false, value : '', options : [...this.props.options], inputFocused : false }; }, componentDidMount : function() { if(this.props.trigger == 'click') document.addEventListener('click', this.handleClickOutside); this.setState({ value : this.props.default }); }, componentWillUnmount : function() { if(this.props.trigger == 'click') document.removeEventListener('click', this.handleClickOutside); }, handleClickOutside : function(e){ // Close dropdown when clicked outside if(this.refs.dropdown && !this.refs.dropdown.contains(e.target)) { this.handleDropdown(false); } }, handleDropdown : function(show){ this.setState({ showDropdown : show, inputFocused : this.props.autoSuggest.clearAutoSuggestOnClick ? show : false }); }, handleInput : function(e){ e.persist(); this.setState({ value : e.target.value, inputFocused : false }, ()=>{ this.props.onEntry(e); }); }, handleSelect : function(e){ this.setState({ value : e.currentTarget.getAttribute('data-value') }, ()=>{this.props.onSelect(this.state.value);}); ; }, renderTextInput : function(){ return (