maputnik/src/components/inputs/ArrayInput.jsx

59 lines
1.3 KiB
React
Raw Normal View History

2016-12-29 22:37:54 +01:00
import React from 'react'
import input from '../../config/input.js'
import StringInput from './StringInput'
2016-12-31 14:56:26 +01:00
import NumberInput from './NumberInput'
2016-12-29 22:37:54 +01:00
import { margins } from '../../config/scales.js'
class ArrayInput extends React.Component {
static propTypes = {
value: React.PropTypes.array,
type: React.PropTypes.string,
length: React.PropTypes.number,
default: React.PropTypes.array,
style: React.PropTypes.object,
onChange: React.PropTypes.func,
}
2016-12-31 14:56:26 +01:00
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 || []
}
2016-12-29 22:37:54 +01:00
render() {
const commonStyle = {
2017-01-09 21:39:35 +01:00
width: '48%',
marginRight: '2%',
2016-12-29 22:37:54 +01:00
}
2016-12-31 14:56:26 +01:00
const inputs = this.values.map((v, i) => {
2016-12-29 22:37:54 +01:00
if(this.props.type === 'number') {
2016-12-31 14:56:26 +01:00
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)}
/>
2016-12-29 22:37:54 +01:00
}
})
2016-12-31 12:17:02 +01:00
return <div style={{display: 'inline-block', width: '50%'}}>
2016-12-29 22:37:54 +01:00
{inputs}
</div>
}
}
export default ArrayInput