Skip to content

Commit

Permalink
Help Center: Prevent line breaks on Enter key press in ResizableTexta…
Browse files Browse the repository at this point in the history
…rea component (#96313)
  • Loading branch information
agrullon95 authored Nov 13, 2024
1 parent b90f42e commit c80383a
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export const ResizableTextarea: React.FC< {
[ inputRef, sendMessageHandler, keyUpHandle, setSubmitDisabled ]
);

const onKeyDown = useCallback(
async ( event: KeyboardEvent< HTMLTextAreaElement > ) => {
// Prevent line break when user sends a message
if ( event.key === 'Enter' && ! event.shiftKey && inputRef.current?.value.trim() !== '' ) {
event.preventDefault();
}
},
[ inputRef ]
);

useEffect( () => {
// Set's back the textarea height after sending messages, it is needed for long messages.
if ( inputRef.current ) {
Expand All @@ -54,6 +64,7 @@ export const ResizableTextarea: React.FC< {
rows={ 1 }
className={ className }
onKeyUp={ onKeyUp }
onKeyDown={ onKeyDown }
placeholder={ __( 'Type a message…', __i18n_text_domain__ ) }
style={ { transition: 'none' } }
/>
Expand Down

0 comments on commit c80383a

Please sign in to comment.