简体   繁体   English

React - 在更改事件中检测“输入”按键

[英]React - detect 'enter' key press in change event

I have a React component that is, more or less, a simple textarea where a user can type into.我有一个 React 组件,它或多或少是一个用户可以输入的简单文本区域。 Here is what the code looks like so far:到目前为止,代码如下所示:

import React from 'react';
import {connect} from 'react-redux';

function handleChange(event) {
    const {setText} = this.props;

    //is there some way I can detect [ENTER] here and call this.props.add instead?
    setText(event.target.value);
}

class TextArea extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        const {value} = this.props;

        return (
            <div className="list-item">
                <textarea
                    className="form-control"
                    value={value}
                    onChange={handleChange.bind(this)}
                />
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        value: state.value
    }
}

function mapDispatchToProps(dispatch) {
    return {
        setText(text) {
            const action = {
                type: 'set-text',
                text: text
            };

            dispatch(action);
        },
        add() {
            const action = {
                type: 'add'
            };

            dispatch(action);
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(TextArea)

This component is connected to a redux store, which is updated every time the user types something into the text area.这个组件连接到一个 redux 存储,每次用户在文本区域输入内容时都会更新该存储。 The redux store sends the new value back to the textarea component, and the textarea component re-renders to display the new value. redux store 将新值发送回 textarea 组件,textarea 组件重新渲染以显示新值。

While this is working so far, I would like this component to behave differently if the enter key is pressed.虽然到目前为止这是有效的,但如果按下回车键,我希望这个组件的行为有所不同。 Since a change event doesn't expose a keyCode (at least not that I'm aware), I have no idea how to make the handleChange function call the add prop if an enter key is pressed instead of calling the setText prop.由于更改事件不会公开 keyCode(至少我不知道),我不知道如果按下回车键而不是调用setText道具,如何使handleChange函数调用add道具。

One thing I tried was to attach an onKeyDown handler to the textarea instead, which I could use to detect the enter keyCode (13), but this made me unable to set the text correctly, because if I converted the keyCode attached to the event to a character and appended it to the current value, there would be no way for me to determine whether it should be upper or lower case.我尝试的一件事是将onKeyDown处理程序附加到 textarea,我可以使用它来检测输入 keyCode (13),但这使我无法正确设置文本,因为如果我将附加到事件的 keyCode 转换为一个字符并将其附加到当前值,我无法确定它应该是大写还是小写。

I'm pretty stuck here.我被困在这里了。 I really don't think there's a way for me to detect the enter key on a change event, and a keyDown event doesn't have a way for me to handle all possible inputs.我真的不认为我有办法在更改事件上检测回车键,并且 keyDown 事件没有办法让我处理所有可能的输入。 Is there a way I could possibly combine the two?有没有办法可以将两者结合起来?

Thanks in advance!提前致谢!

You can use both of handlers same time.您可以同时使用这两个处理程序。

In onKeyDown you can check key.在 onKeyDown 中,您可以检查密钥。 If it is ENTER pressed call event.preventDefault() to prevent onChange call, or if it is not - do nothing如果是 ENTER 按下调用event.preventDefault()来阻止onChange调用,或者如果不是 - 什么也不做

Javascript event queue doesn't contain change event until default handler for keydown event is called.在调用keydown事件的默认处理程序之前,Javascript 事件队列不包含change事件。 If you call event.preventDefault() in onKeyDown handler no change event will be fired.如果您在onKeyDown处理程序中调用event.preventDefault() ,则不会触发任何change事件。

You may try this.你可以试试这个。

          onChange={(evt: any) => {               
          }}
          onKeyDown={(evt: any) => {
            const keyCode = evt.keyCode;
            if (keyCode === 8) evt.preventDefault();
          }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM