Add DynamicArrayInput for handling variable-length array fields

This commit is contained in:
Torben Barsballe 2017-05-25 13:00:34 -07:00
parent a98444b4e7
commit 2adb1bf917
3 changed files with 131 additions and 5 deletions

View file

@ -8,6 +8,7 @@ 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 ArrayInput from '../inputs/ArrayInput'
import DynamicArrayInput from '../inputs/DynamicArrayInput'
import FontInput from '../inputs/FontInput' import FontInput from '../inputs/FontInput'
import IconInput from '../inputs/IconInput' import IconInput from '../inputs/IconInput'
import capitalize from 'lodash.capitalize' import capitalize from 'lodash.capitalize'
@ -105,11 +106,18 @@ export default class SpecField extends React.Component {
fonts={this.props.fieldSpec.values} fonts={this.props.fieldSpec.values}
/> />
} else { } else {
if (this.props.fieldSpec.length) {
return <ArrayInput return <ArrayInput
{...commonProps} {...commonProps}
type={this.props.fieldSpec.value} type={this.props.fieldSpec.value}
length={this.props.fieldSpec.length} length={this.props.fieldSpec.length}
/> />
} else {
return <DynamicArrayInput
{...commonProps}
type={this.props.fieldSpec.value}
/>
}
} }
default: return null default: return null
} }

View file

@ -0,0 +1,99 @@
import React from 'react'
import StringInput from './StringInput'
import NumberInput from './NumberInput'
import Button from '../Button'
import DeleteIcon from 'react-icons/lib/md/delete'
import DocLabel from '../fields/DocLabel'
class DynamicArrayInput extends React.Component {
static propTypes = {
value: React.PropTypes.array,
type: React.PropTypes.string,
default: React.PropTypes.array,
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 || []
}
addValue() {
const values = this.values.slice(0)
if (this.props.type === 'number') {
values.push(0)
} else {
values.push("")
}
this.props.onChange(values)
}
deleteValue(valueIdx) {
const values = this.values.slice(0)
values.splice(valueIdx, 1)
this.props.onChange(values)
}
render() {
const inputs = this.values.map((v, i) => {
const deleteValueBtn= <DeleteValueButton onClick={this.deleteValue.bind(this, i)} />
const input = this.props.type === 'number'
? <NumberInput
key={i}
value={v}
onChange={this.changeValue.bind(this, i)}
/>
: <StringInput
key={i}
value={v}
onChange={this.changeValue.bind(this, i)}
/>
return <div
style={this.props.style}
className="maputnik-array-block"
>
<div className="maputnik-array-block-action">
{deleteValueBtn}
</div>
<div className="maputnik-array-block-content">
{input}
</div>
</div>
})
return <div className="maputnik-array">
{inputs}
<Button
className="maputnik-array-add-value"
onClick={this.addValue.bind(this)}
>
Add value
</Button>
</div>
}
}
function DeleteValueButton(props) {
return <Button
className="maputnik-delete-stop"
onClick={props.onClick}
>
<DocLabel
label={<DeleteIcon />}
doc={"Remove array entry."}
/>
</Button>
}
export default DynamicArrayInput

View file

@ -52,6 +52,25 @@
> * { > * {
margin-bottom: $margin-3; margin-bottom: $margin-3;
} }
.maputnik-array-block {
.maputnik-array-block-action {
vertical-align: top;
display: inline-block;
width: 14%;
}
.maputnik-array-block-content {
vertical-align: top;
display: inline-block;
width: 86%;
}
}
.maputnik-array-add-value {
display: inline-block;
float: right;
}
} }
// SELECT // SELECT