Support updating of ArrayInput #39

This commit is contained in:
Lukas Martinelli 2016-12-31 14:56:26 +01:00
parent e00cdde3af
commit b0e9790382

View file

@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import input from '../../config/input.js' import input from '../../config/input.js'
import StringInput from './StringInput' import StringInput from './StringInput'
import NumberInput from './StringInput' import NumberInput from './NumberInput'
import { margins } from '../../config/scales.js' import { margins } from '../../config/scales.js'
@ -15,17 +15,38 @@ class ArrayInput extends React.Component {
onChange: React.PropTypes.func, onChange: React.PropTypes.func,
} }
changeValue(idx, newValue) {
console.log(idx, newValue)
const values = this.values.slice(0)
values[idx] = newValue
this.props.onChange(values)
}
get values() {
return this.props.value || this.props.default || []
}
render() { render() {
const values = this.props.value || this.props.default || []
const commonStyle = { const commonStyle = {
width: '49%', width: '49%',
marginRight: '1%', marginRight: '1%',
} }
const inputs = values.map((v, i) => { const inputs = this.values.map((v, i) => {
if(this.props.type === 'number') { if(this.props.type === 'number') {
return <NumberInput key={i} value={v} style={commonStyle} /> return <NumberInput
key={i}
value={v}
style={commonStyle}
onChange={this.changeValue.bind(this, i)}
/>
} else {
return <StringInput
key={i}
value={v}
style={commonStyle}
onChange={this.changeValue.bind(this, i)}
/>
} }
return <StringInput key={i} value={v} style={commonStyle} />
}) })
return <div style={{display: 'inline-block', width: '50%'}}> return <div style={{display: 'inline-block', width: '50%'}}>