用于检测“输入”的KeyPress会阻止输入文本字段上的任何数据条目

时间:2017-08-09 19:43:02

标签: javascript reactjs redux

React的初学者,我遇到了一个问题。我有一个个人资料页面,用户可以在其中更改每个字段(名字,电子邮件等),按下“Enter”后,它会保存该特定字段(redux / axios / promise)。

我遇到的问题是,当我使用onKeyPress / Down / Up作为事件触发器时,它会阻止任何数据输入。意思是,我无法在字段中输入任何内容,就好像它是只读的或被阻止的一样。如果我使用onChange,它可以工作。

class Account extends Component {

    constructor( props ) {
        super(props);

        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(e) {

        if( e.key == 'Enter' ) { // this is detected (i console.logged it)

            e.preventDefault(); // also tried without these
            e.stopPropagation(); // also tried without these

            // this is triggered but the text field doesn't change so it updates nothing
            this.props.setUserKvPair( e.target.name, e.target.value );
        }
    }

    render() {

        return (
            <div className="app-account row">

                <div className="component-container col-12">
                    <div className="inner">

                        <p className="form-group">
                            <label className="form-text text-muted">Email Address</label>
                            <input 
                                type="text" 
                                name="email"
                                className="form-control" 
                                value={this.props.user.email} 
                                onKeyDown={this.handleChange} />
                        </p>
                        <p className="form-group">
                            <label className="form-text text-muted">First Name</label>
                            <input 
                                type="text" 
                                name="first_name"
                                className="form-control" 
                                value={this.props.user.first_name} 
                                onKeyPress={this.handleChange} />
                        </p>
                    </div>
                </div>

            </div>
        );
    }

}

const mapStateToProps = ( state ) => {
    return {
        user: state.user.data,
        properties: state.properties.data,
    }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators(userActions, dispatch);
}


export default connect( mapStateToProps, mapDispatchToProps )(Account);

基本上,一切正常但只是文本输入的值不会改变,除非我使用onChange。

如何删除该字段上的阻塞/只读?

2 个答案:

答案 0 :(得分:1)

如何使用valueonChange在输入中写一些内容?

value={this.props.user.email}没有onChange这意味着您的输入是只读的

https://jsfiddle.net/69z2wepo/84207/

答案 1 :(得分:1)

我建议使用表单,因为它授予免费onEnter功能,而无需连接一堆不同的事件处理程序。另外,我建议使用Controlled Components,它使用onChange处理程序但缓存组件状态中的所有输入值。这使得使用数据非常容易。

请参阅以下代码

import React, { Component } from "react";

class SomeComponent extends Component {
    constructor() {
        super();

        this.state = {
            text_1: "",
            text_2: ""
        };

        this.submitHandler = this.submitHandler.bind(this);
    }

    submitHandler(e) {
        e.preventDefault();

        const { text_1, text_2 } = this.state;

        console.log(text_1, text_2);
    }

    render() {
        return (
            // Using a form gives free on 'Enter' functionality
            <form onSubmit={this.submitHandler}>
                {/* Controlled Component 1 */}
                <input
                    type="text"
                    value={this.state.text_1}
                    onChange={e => this.setState({ text_1: e.target.value })}
                />

                {/* Controlled Component 2 */}
                <input
                    type="text"
                    value={this.state.text_2}
                    onChange={e => this.setState({ text_2: e.target.value })}
                />
            </form>
        );
    }
}
相关问题