Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI TextField multiline, make a new line only when pressed Shift Enter

I have a chat system built in React and I am using Material UI textfield for submitting a message. I want it to function like whatsapp or telegram on the web version. On enter send the message but on shift enter, make a new line. I have this

             <TextField
                autoFocus={true}
                color={"primary"}
                multiline
                rowsMax={4}
                value={inputValue}
                fullWidth
                placeholder={"Please enter a message"}
                onChange={e => {
                  setInputValue(e.target.value);
                }}
              />

What should I add to acheive that functionality, is it easy?

like image 279
Missak Boyajian Avatar asked Jan 26 '26 07:01

Missak Boyajian


1 Answers

Add onKeyDown prop to your TextField

onKeyDown={(e) => {
  if(e.keyCode === 13 && !e.shiftKey) {
    e.preventDefault();
    setInputValue("");
  }
}}
like image 88
Hamidreza Avatar answered Jan 29 '26 12:01

Hamidreza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!