forked from jifalops/chat-window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-input.html
124 lines (111 loc) · 3.31 KB
/
chat-input.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-input/paper-textarea.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<!--
CSS custom property | Description | Default
------------------- | ----------- | -------
`--chat-input-input` | Applied to the internal input field | {}
`--chat-input-button` | Appied to the paper-icon-button | {}
-->
<dom-module id="chat-input">
<template>
<style>
:host {
display: block;
@apply --layout-horizontal;
@apply --layout-center;
}
paper-input, paper-textarea {
@apply --layout-flex;
@apply --chat-input-input;
}
paper-icon-button {
color: var(--primary-color);
@apply --chat-input-button;
}
</style>
<paper-input
no-label-float
hidden$="[[!singleLine]]"
value="{{text}}"
on-keydown="_checkForEnter">
</paper-input>
<paper-textarea
no-label-float
hidden$="[[singleLine]]"
value="{{text}}"
on-keydown="_checkForEnter">
</paper-textarea>
<paper-icon-button noink icon="[[_getIcon(icon)]]" src="[[_getSrc(icon)]]" on-tap="_fireSendEvent"></paper-icon-button>
</template>
<script>
/**
* `chat-input`
* Single or multi-line input field for `chat-window`
*
* @customElement
* @polymer
* @demo demo/index.html
*/
Polymer({
is: 'chat-input',
properties: {
/**
* Whether to allow single line messages only. Internally a paper-input
* will be used instead of a paper-textarea.
*/
singleLine: {
type: Boolean,
value: false
},
/**
* The text inside the input field.
*/
text: {
type: String,
notify: true
},
/**
* The paper-icon-button's `icon` or a URL to use as the paper-icon-button's `src`.
*/
icon: {
type: String,
value: 'send'
},
/**
* Whether to send the message when a user presses `enter` on the keyboard.
* The default behavior is to send-on-enter when in single-line input mode,
* and not send if in multiline input mode.
*/
sendOnEnter: Boolean
},
_fireSendEvent: function(e) {
// console.log('firing send event...', this.text);
this.fire('send', this.text);
},
_getIcon: function(icon) {
return this._iconIsUrl(icon) ? '' : icon;
},
_getSrc: function(icon) {
return this._iconIsUrl(icon) ? icon : '';
},
_iconIsUrl: function(icon) {
return icon && (icon.includes('/') || icon.includes('.'));
},
_checkForEnter: function (e) {
if (e.keyCode === 13 &&
(this.sendOnEnter || (this.sendOnEnter === undefined && this.singleLine))) {
this._fireSendEvent();
}
}
/**
* Triggered when a user taps the send button. The text may be empty.
* @event send
* @param {text} string The message to be sent.
*/
});
</script>
</dom-module>