summaryrefslogtreecommitdiff
path: root/app/client/common/selectGroup.component.js
blob: 5c1af51b13b5282a8e6643a7192878ab5328591f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { h, Component } from 'preact'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

class SelectGroup extends Component {
  constructor(props){
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }
  handleChange(e){
    clearTimeout(this.timeout)
    let new_value = e.target.value
    if (new_value === 'PLACEHOLDER') return
    this.props.onChange && this.props.onChange(this.props.name, new_value)
  }
  render() {
    const currentValue = this.props.live ? this.props.opt[this.props.name] : this.props.value
    let lastValue
    const options = (this.props.options || []).map((group, i) => {
      const groupName = group.name
      const children = group.options.map(key => {
        let name = key.length < 2 ? key.toUpperCase() : key
        name = name.replace(/_/g, ' ')
        let value = key
        lastValue = value
        return (
          <option value={value} key={value}>
            {name}
          </option>
        )
      })
      return (
        <optgroup label={groupName} key={groupName}>
          {children}
        </optgroup>
      )
    })
    return (
      <div className='select param'>
        <label>
          <span>{this.props.title}</span>
          <select
            onChange={this.handleChange}
            value={currentValue || lastValue}
          >
            {this.props.placeholder && <option value="PLACEHOLDER">{this.props.placeholder}</option>}
            {options}
          </select>
        </label>
        {this.props.children}
      </div>
    )
  }
}

function capitalize(s){
  return (s || "").replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
}

const mapStateToProps = (state, props) => ({
  opt: props.opt || state.live.opt,
})

const mapDispatchToProps = (dispatch, ownProps) => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(SelectGroup)