Skip to content

Commit 64b0b4e

Browse files
committed
Merge branch 'dmdimitrov/chat-ai-component' of https://github.com/IgniteUI/igniteui-webcomponents into dmdimitrov/chat-ai-component
2 parents a264bcf + 6c6fdd9 commit 64b0b4e

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

src/components/chat/chat-input.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { chatContext } from '../common/context.js';
1111
import { addKeybindings } from '../common/controllers/key-bindings.js';
1212
import { watch } from '../common/decorators/watch.js';
1313
import { registerComponent } from '../common/definitions/register.js';
14+
import { partMap } from '../common/part-map.js';
1415
import { isEmpty } from '../common/util.js';
1516
import IgcIconComponent from '../icon/icon.js';
1617
import { registerIconFromText } from '../icon/icon.registry.js';
@@ -103,7 +104,7 @@ export default class IgcChatInputComponent extends LitElement {
103104
}
104105

105106
@state()
106-
private containerPart = 'input-container';
107+
private _parts = { 'input-container': true, dragging: false };
107108

108109
private readonly _defaults: Readonly<DefaultInputRenderers> = Object.freeze({
109110
input: () => this._renderTextArea(),
@@ -125,7 +126,6 @@ export default class IgcChatInputComponent extends LitElement {
125126
}
126127

127128
protected override firstUpdated() {
128-
this.setupDragAndDrop();
129129
this._chatState.updateAcceptedTypesCache();
130130
this._chatState.textArea = this._textInputElement;
131131

@@ -144,18 +144,6 @@ export default class IgcChatInputComponent extends LitElement {
144144
};
145145
}
146146

147-
private setupDragAndDrop() {
148-
const container = this.shadowRoot?.querySelector(
149-
`div[part='input-container']`
150-
) as HTMLElement;
151-
if (container) {
152-
container.addEventListener('dragenter', this.handleDragEnter.bind(this));
153-
container.addEventListener('dragover', this.handleDragOver.bind(this));
154-
container.addEventListener('dragleave', this.handleDragLeave.bind(this));
155-
container.addEventListener('drop', this.handleDrop.bind(this));
156-
}
157-
}
158-
159147
private _getRenderer(
160148
name: keyof DefaultInputRenderers
161149
): ChatTemplateRenderer<any> {
@@ -170,45 +158,45 @@ export default class IgcChatInputComponent extends LitElement {
170158
);
171159
}
172160

173-
private handleDragEnter(event: DragEvent) {
161+
private _handleDragEnter(event: DragEvent): void {
174162
event.preventDefault();
175163
event.stopPropagation();
176164

177165
const validFiles = getChatAcceptedFiles(event, this._acceptedTypes);
178-
this.containerPart = `input-container ${!isEmpty(validFiles) ? ' dragging' : ''}`;
166+
this._parts = { 'input-container': true, dragging: !isEmpty(validFiles) };
179167
this._chatState.emitEvent('igcAttachmentDrag');
180168
}
181169

182-
private handleDragOver(e: DragEvent) {
183-
e.preventDefault();
184-
e.stopPropagation();
170+
private _handleDragOver(event: DragEvent): void {
171+
event.preventDefault();
172+
event.stopPropagation();
185173
}
186174

187-
private handleDragLeave(e: DragEvent) {
188-
e.preventDefault();
189-
e.stopPropagation();
175+
private _handleDragLeave(event: DragEvent): void {
176+
event.preventDefault();
177+
event.stopPropagation();
190178

191179
// Check if we're actually leaving the container
192-
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
193-
const x = e.clientX;
194-
const y = e.clientY;
180+
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
181+
const x = event.clientX;
182+
const y = event.clientY;
195183

196184
if (
197185
x <= rect.left ||
198186
x >= rect.right ||
199187
y <= rect.top ||
200188
y >= rect.bottom
201189
) {
202-
this.containerPart = 'input-container';
190+
this._parts = { 'input-container': true, dragging: false };
203191
}
204192
}
205193

206-
private handleDrop(e: DragEvent) {
207-
e.preventDefault();
208-
e.stopPropagation();
209-
this.containerPart = 'input-container';
194+
private _handleDrop(event: DragEvent): void {
195+
event.preventDefault();
196+
event.stopPropagation();
197+
this._parts = { 'input-container': true, dragging: false };
210198

211-
const validFiles = getChatAcceptedFiles(e, this._acceptedTypes);
199+
const validFiles = getChatAcceptedFiles(event, this._acceptedTypes);
212200
this._chatState.emitEvent('igcAttachmentDrop');
213201
this._chatState.attachFiles(validFiles);
214202
this.requestUpdate();
@@ -348,7 +336,13 @@ export default class IgcChatInputComponent extends LitElement {
348336
};
349337

350338
return html`
351-
<div part="${this.containerPart}">
339+
<div
340+
part=${partMap(this._parts)}
341+
@dragenter=${this._handleDragEnter}
342+
@dragover=${this._handleDragOver}
343+
@dragleave=${this._handleDragLeave}
344+
@drop=${this._handleDrop}
345+
>
352346
${this._chatState.inputAttachments &&
353347
this._chatState.inputAttachments.length > 0
354348
? html` <div part="attachments" role="list" aria-label="Attachments">

src/components/chat/chat.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import {
2323
simulateKeyboard,
2424
} from '../common/utils.spec.js';
2525
import { simulateFileUpload } from '../file-input/file-input.spec.js';
26+
import IgcInputComponent from '../input/input.js';
2627
import IgcChatComponent from './chat.js';
2728

2829
describe('Chat', () => {
2930
before(() => {
30-
defineComponents(IgcChatComponent);
31+
defineComponents(IgcChatComponent, IgcInputComponent);
3132
});
3233

3334
const DIFF_OPTIONS = {
@@ -1102,7 +1103,7 @@ describe('Chat', () => {
11021103
</div>
11031104
</div>
11041105
<div part="input-wrapper">
1105-
<igc-input placeholder="Type text here...">
1106+
<igc-input type="text" placeholder="Type text here...">
11061107
</div>
11071108
<div part="buttons-container">
11081109
<div>

0 commit comments

Comments
 (0)