Skip to content

Commit 7b7a4a1

Browse files
committed
refactor(chat): use keybinidng controller for textarea enter keydown
1 parent d0abb54 commit 7b7a4a1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/components/chat/chat-input.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { consume } from '@lit/context';
22
import { html, LitElement, nothing, type TemplateResult } from 'lit';
33
import { query, state } from 'lit/decorators.js';
44
import { ifDefined } from 'lit/directives/if-defined.js';
5+
import { createRef, ref } from 'lit/directives/ref.js';
56
import { addThemingController } from '../../theming/theming-controller.js';
67
import IgcIconButtonComponent from '../button/icon-button.js';
78
import IgcChipComponent from '../chip/chip.js';
89
import { chatContext } from '../common/context.js';
10+
import {
11+
addKeybindings,
12+
enterKey,
13+
} from '../common/controllers/key-bindings.js';
914
import { watch } from '../common/decorators/watch.js';
1015
import { registerComponent } from '../common/definitions/register.js';
1116
import IgcIconComponent from '../icon/icon.js';
@@ -77,6 +82,8 @@ export default class IgcChatInputComponent extends LitElement {
7782
@query(IgcTextareaComponent.tagName)
7883
private textInputElement!: IgcTextareaComponent;
7984

85+
private readonly _textAreaRef = createRef<IgcTextareaComponent>();
86+
8087
@watch('acceptedFiles', { waitUntilFirstUpdate: true })
8188
protected acceptedFilesChange(): void {
8289
this._chatState?.updateAcceptedTypesCache();
@@ -94,6 +101,11 @@ export default class IgcChatInputComponent extends LitElement {
94101
constructor() {
95102
super();
96103
addThemingController(this, all);
104+
// Configure keybindings to not skip textarea events and bind Enter key
105+
addKeybindings(this, {
106+
skip: ['input', 'select'],
107+
ref: this._textAreaRef,
108+
}).setActivateHandler(this.handleKeyDown);
97109
registerIconFromText('attachment', attachmentIcon, 'material');
98110
registerIconFromText('send-message', sendButtonIcon, 'material');
99111
registerIconFromText('file-document', fileDocumentIcon, 'material');
@@ -127,12 +139,12 @@ export default class IgcChatInputComponent extends LitElement {
127139
public get defaultTextArea(): TemplateResult {
128140
return html` <igc-textarea
129141
part="text-input"
142+
${ref(this._textAreaRef)}
130143
.placeholder=${this.inputPlaceholder}
131144
resize="auto"
132145
rows="1"
133146
.value=${this.inputValue}
134147
@igcInput=${this.handleInput}
135-
@keydown=${this.handleKeyDown}
136148
@focus=${this.handleFocus}
137149
@blur=${this.handleBlur}
138150
></igc-textarea>`;
@@ -196,7 +208,7 @@ export default class IgcChatInputComponent extends LitElement {
196208
}
197209

198210
private handleKeyDown(e: KeyboardEvent) {
199-
if (e.key === 'Enter' && !e.shiftKey) {
211+
if (e.key === enterKey && !e.shiftKey) {
200212
e.preventDefault();
201213
this.sendMessage();
202214
} else {

0 commit comments

Comments
 (0)