在React.js中处理多个输入值

NiseNise

我正在尝试创建一种表单:1)渲染输入的初始字段集2)使用该渲染通过按添加按钮来渲染更多内容3)我希望能够捕获与表单实例/键相关联的用户输入希望这有道理。

所以我的输出是这样的

{[id:1,name:xxx,数量:400],[id:2,name:xxx,数量:400]}

代替

{name:xxx,amount:400,name0:xxx,amount0:400}

代码是执行此操作的最佳方法还是我遗漏了某些东西?任何帮助将不胜感激。

const propTypes = {
    name : PropTypes.string,
    onNumberChange : PropTypes.func,
    onTextChange: PropTypes.func,
    text : PropTypes.string,
    helpText : PropTypes.string,
    className : PropTypes.string,
    autoFocus : PropTypes.bool,
    options : PropTypes.array
}

class NumberTextInput extends React.Component {

    constructor(props) {
        super(props)

        this.state = {
            inputForm: [],
            fieldName : '',
            fieldValue : '',
            fields : {}
        }
    }

    /**
     * [handleChange description]
     * @param  {[type]} event [description]
     * @return {[type]}       [description]
     */
    handleChange(event) {

        const inputFields = this.state.fields // array of inputted fields

        inputFields[event.target.name] = event.target.value

        this.setState({
            fields : inputFields
        }, this.props.onTextChange.bind(null, inputFields));
    }


    /**
     * addForm 
     * @param {[type]} e [description]
     */
    addForm(e) {
        e.preventDefault();

        let currentOptions = this.state.inputForm;

        currentOptions.push({name:null})

        this.setState({inputForm : currentOptions})

    }

    /** [removeForm description] */
    removeForm(index) {

        let currentOptions = this.state.inputForm;

        currentOptions.splice(index,1);

        this.setState({inputForm : currentOptions})
    }


    /**
     * Renders out the different form input types associated with a question
     */
    inputRender(itemIndex) {

        // Checks if the itemIndex exists then a assign value this to output unique names
        // when added to the object, also meet accessibilty requirements of form
        let itemIncrement = itemIndex === undefined ? '' : itemIndex;

        return (

            <div>
            {this.props.options.map( (option, index) =>

                <div className="fieldset" key={option.Name}>
                <label htmlFor={option.Name+itemIncrement}>{option.Text}</label>        
                {(() => {
                        if (option.Type ==='textInput') {
                            return (
                                <FormInput 
                                    type="text" 
                                    onChange={this.handleChange.bind(this)} 
                                    id={option.Name+itemIncrement}
                                    name={option.Name+itemIncrement} 
                                    className={this.props.className} 
                                    placeholder="" 
                                    pattern="[a-zA-Z0-9]+"
                                    autoFocus={index === 0 ? true : false}
                                />

                            )

                        }
                        if(option.Type === 'numberInput') {
                            return (

                                <FormInput 
                                    type="number" 
                                    onChange={this.handleChange.bind(this)} 
                                    placeholder="£" 
                                    pattern="\d*" 
                                    id={option.Name+itemIncrement}
                                    name={option.Name+itemIncrement}
                                    className={this.props.className} 
                                    autoFocus={index === 0 ? true : false}
                                />
                            )
                        }
                })()}
                </div>
            )}
            </div>
        )
    }

/**
 * Renders the out the input form field each time the 'add another debt is clicked'
 * 
 */
renderForms() {

let itemIndex;

return(

    // define each row of input forms and get the index and pass to the inputRender
    // to track unique input form.
    this.state.inputForm.map((item, index) => {
        {itemIndex = index}
        return (
            <div key={index} className="well">

                {this.inputRender(itemIndex)}

                <a onClick={this.removeForm.bind(this, index)} className="remove-field remove">Remove</a>
            </div>

        )
    })

    )
}

/**
 * Render out the all the input forms
 * 
 */
render() {
        return (
            <div>

                <div className="well">
                    {this.inputRender()} 
                </div> 
                 {this.renderForms()}

                <Button 
                    text="Add another debt" 
                    className="btn-secondary plus-button-icon" 
                    onClick={this.addForm.bind(this)}
                    value="add" 
                />
            </div>
        );
    }
}

NumberTextInput.propTypes = propTypes;

export default NumberTextInput;
布拉德·字节

您可以实现一些代码来完成此操作..不确定将其放置在何处。

基本上,您遍历输入的对象,解析出数字以获取ID,然后分配给由该ID索引的新对象数组。

var testInputs = {name: 'name', amount: 0, name1: 'name1', amount1: 1, name2: 'name2', amount2: 2}

var result = []

for (var key in testInputs) {
  var field = key.replace(/\d/,'')
  var id = key.match(/\d/)
  id = id ? id[0] : 0
  var newInput = {id: id}
  if (!result[id]) {
    result[id] = newInput
  }
  result[id][field] = testInputs[key]
}
console.log(result)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在React JS中如何处理输入选择的更改值?

来自分类Dev

如何通过React中的单个组件处理多个输入字段

来自分类Dev

React Js-无法在输入字段中输入值

来自分类Dev

在React.JS中处理API调用的输入更改

来自分类Dev

在 React JS 中处理 If 条件中的状态值

来自分类Dev

在React中访问输入值

来自分类Dev

使用 react.js 在 switch case 中返回多个值

来自分类Dev

如何处理React的onChange事件中的十进制值作为输入?

来自分类Dev

表单中的多个输入元素-React

来自分类Dev

React Js:设置同级组件的输入值

来自分类Dev

React JS:检查输入值内的逗号

来自分类Dev

如何使用React钩子随机显示多个输入中的一个值

来自分类Dev

如何将一个事件处理程序附加到 React JS 中的不同输入?

来自分类Dev

在Python中处理多个输入值

来自分类Dev

在React事件处理程序中此值

来自分类Dev

useState更新React中的多个值

来自分类Dev

如何在React中过滤多个值

来自分类Dev

处理Radio在React JS中的变化?

来自分类Dev

处理React js中的验证场景

来自分类Dev

React JS从输入数量中获取价值

来自分类Dev

对React JS中的输入进行验证

来自分类Dev

在React JS中验证用户输入

来自分类Dev

在React.js表单中要求输入

来自分类Dev

react.js处理多个复选框字段

来自分类Dev

React.js:具有多个输入的表单不提交

来自分类Dev

Webform onSubmit无法在React中处理更多输入

来自分类Dev

使用这些动态输入处理 React 中的状态

来自分类Dev

单击表单对象中的react react js添加输入字段

来自分类Dev

如何在React JS中获取多个复选框值?