-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrasa-chat-input.tsx
71 lines (63 loc) · 1.92 KB
/
rasa-chat-input.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Component, Prop, h, EventEmitter, Event, State, Watch } from '@stencil/core';
import { configStore } from '../../store/config-store';
import { messageQueueService } from '../../store/message-queue';
import { widgetState } from '../../store/widget-state-store';
@Component({
tag: 'rasa-chat-input',
styleUrl: 'rasa-chat-input.scss',
shadow: true,
})
export class RasaChatInput {
/**
* Input value
*/
@Prop() initialValue?: string = '';
/**
* Send message event
*/
@Event() sendMessageHandler: EventEmitter<string>;
@State() value: string;
@Watch('initialValue')
valueChange(newVal: string) {
this.value = newVal;
}
public componentWillLoad() {
this.value = this.initialValue;
}
private sendMessageClick = () => {
if (!this.value.trim()) return;
const { isRendering, messageQueue } = messageQueueService.getState().state;
if (isRendering || messageQueue.length > 0) return;
if (!widgetState.isConnected()) return;
this.sendMessageHandler.emit(this.value);
this.value = '';
};
private inputChangeHandler = (event: Event) => {
this.value = (event.target as HTMLInputElement).value;
};
private handleKeyUp = (event: KeyboardEvent) => {
if (event.key === 'Enter') {
this.sendMessageClick();
}
event.preventDefault();
};
render() {
return (
<div class="rasa-chat-input">
<input
type="text"
class="rasa-chat-input__input"
placeholder={configStore().inputMessagePlaceholder}
value={this.value}
onInput={this.inputChangeHandler}
maxLength={500}
onKeyUp={event => this.handleKeyUp(event)}
enterkeyhint="done"
/>
<button class="rasa-chat-input__button" onClick={this.sendMessageClick} aria-label="Send message">
<rasa-icon-paper-plane class="rasa-chat-input__icon"></rasa-icon-paper-plane>
</button>
</div>
);
}
}