Add ArrayInput

This commit is contained in:
Lukas Martinelli 2016-12-29 22:37:54 +01:00
parent 007bdad70a
commit 51a2eabc91
2 changed files with 47 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import CheckboxInput from '../inputs/CheckboxInput'
import StringInput from '../inputs/StringInput' import StringInput from '../inputs/StringInput'
import SelectInput from '../inputs/SelectInput' import SelectInput from '../inputs/SelectInput'
import MultiButtonInput from '../inputs/MultiButtonInput' import MultiButtonInput from '../inputs/MultiButtonInput'
import ArrayInput from '../inputs/ArrayInput'
import capitalize from 'lodash.capitalize' import capitalize from 'lodash.capitalize'
@ -41,6 +42,7 @@ export default class SpecField extends React.Component {
value: React.PropTypes.oneOfType([ value: React.PropTypes.oneOfType([
React.PropTypes.string, React.PropTypes.string,
React.PropTypes.number, React.PropTypes.number,
React.PropTypes.array,
]), ]),
/** Override the style of the field */ /** Override the style of the field */
style: React.PropTypes.object, style: React.PropTypes.object,
@ -91,6 +93,14 @@ export default class SpecField extends React.Component {
{...commonProps} {...commonProps}
/> />
) )
case 'array': return (
<ArrayInput
{...commonProps}
type={this.props.fieldSpec.value}
length={this.props.fieldSpec.length}
default={this.props.fieldSpec.default}
/>
)
default: return null default: return null
} }
} }

View file

@ -0,0 +1,37 @@
import React from 'react'
import input from '../../config/input.js'
import StringInput from './StringInput'
import NumberInput from './StringInput'
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,
}
render() {
const values = this.props.value || this.props.default || []
const commonStyle = {
width: '15%',
marginRight: margins[0],
}
const inputs = values.map((v, i) => {
if(this.props.type === 'number') {
return <NumberInput key={i} value={v} style={commonStyle} />
}
return <StringInput key={i} value={v} style={commonStyle} />
})
return <div style={{display: 'inline-block', width: '51%'}}>
{inputs}
</div>
}
}
export default ArrayInput