import { Component } from 'substance'

export default class SelectInput extends Component {
  didMount () {
    this.context.appState.addObserver(['overlayId'], this.rerender, this, { stage: 'render' })
  }

  dispose () {
    this.context.appState.removeObserver(this)
  }

  render($$) {
    const value = this.props.value
    const options = this.props.availableOptions

    const el = $$('div').addClass('sc-select-input')
    const selectEl = $$('select').addClass('se-select')
      .ref('input')
      .on('change', this._onChange)


    const defaultOpt = $$('option').attr({value: false})
      .append(this.getLabel('select-default-value'))

    if(!value) {
      defaultOpt.attr({selected: 'selected'})
    }

    selectEl.append(defaultOpt)

    options.forEach(opt => {
      const optEl = $$('option').attr({value: opt.id}).append(opt.text)
      if(opt.id === value) optEl.attr({selected: 'selected'})
      selectEl.append(optEl)
    })

    selectEl.on('click', this._onClick, this)

    el.append(selectEl)

    if(this.props.warning) el.addClass('sm-warning')

    return el
  }

  _onChange() {
    const name = this.props.name
    const value = this._getValue()
    this.send('set-value', name, value)
  }

  _onClick (event) {
    event.stopPropagation()
    this.send('toggleOverlay', this.getId())
  }

  _getValue() {
    const input = this.refs.input
    return input.val()
  }
}
@@ -952,55 +952,8 @@
 %7D)%0A%0A
-    selectEl.on('click', this._onClick, this)%0A%0A
     
@@ -1179,107 +1179,8 @@
  %7D%0A%0A
-  _onClick (event) %7B%0A    event.stopPropagation()%0A    this.send('toggleOverlay', this.getId())%0A  %7D%0A%0A
   _g

Fix dropdown glitches.