import React from 'react' import PropTypes from 'prop-types' import InputString from './InputString' import InputNumber from './InputNumber' import InputButton from './InputButton' import {MdDelete} from 'react-icons/md' import FieldDocLabel from './FieldDocLabel' import InputEnum from './InputEnum' import capitalize from 'lodash.capitalize' import InputUrl from './InputUrl' export default class FieldDynamicArray extends React.Component { static propTypes = { value: PropTypes.array, type: PropTypes.string, default: PropTypes.array, onChange: PropTypes.func, style: PropTypes.object, fieldSpec: PropTypes.object, 'aria-label': PropTypes.string, } changeValue(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 if (this.props.type === 'url') { values.push(""); } else if (this.props.type === 'enum') { const {fieldSpec} = this.props; const defaultValue = Object.keys(fieldSpec.values)[0]; values.push(defaultValue); } else { values.push("") } this.props.onChange(values) } deleteValue(valueIdx) { const values = this.values.slice(0) values.splice(valueIdx, 1) this.props.onChange(values.length > 0 ? values : undefined); } render() { const inputs = this.values.map((v, i) => { const deleteValueBtn= let input; if(this.props.type === 'url') { input = } else if (this.props.type === 'number') { input = } else if (this.props.type === 'enum') { const options = Object.keys(this.props.fieldSpec.values).map(v => [v, capitalize(v)]); input = } else { input = } return
{deleteValueBtn}
{input}
}) return (
{inputs} Add value
); } } class DeleteValueInputButton extends React.Component { static propTypes = { onClick: PropTypes.func, } render() { return } doc={"Remove array item."} /> } }