-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathct-input-container.ts
More file actions
executable file
·302 lines (264 loc) · 6.87 KB
/
ct-input-container.ts
File metadata and controls
executable file
·302 lines (264 loc) · 6.87 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
@license
Copyright (c) 2020 Herberth Obregón. All rights reserved.
This code may only be used under the BSD style license found at
https://open.grupoconectate.com/LICENSE.txt The complete set of authors may be found at
https://open.grupoconectate.com/AUTHORS.txt The complete set of contributors may be
found at https://open.grupoconectate.com/CONTRIBUTORS.txt Code distributed by Herberth Obregón as
part of the Conectate Open Source Project is also subject to an additional IP rights grant
found at https://open.grupoconectate.com/PATENTS.txt
*/
import { css, html } from "lit";
import { CtLit, customElement, property, query, state } from "./ct-lit.js";
/**
* `ct-input-container`
* Input element
* @cssprop --color-on-surface - Text color on surface.
* @cssprop --ct-indicator - Indicator color for requirements.
* @cssprop --border-radius - Container border radius.
* @cssprop --ct-input-padding - Custom input padding.
* @cssprop --color-primary - Primary color for input.
*
* @slot prefix - Description of the prefix slot.
* @slot input - Description of the input slot.
* @slot suffix - Description of the suffix slot.
*
* @group lit-ct-components
* @element ct-input-container
*/
@customElement("ct-input-container")
export class CtInputContainer extends CtLit {
static styles = [
css`
:host {
display: inline-block;
margin-bottom: 8px;
color: var(--color-on-surface, #535353);
}
:host([block]) {
display: block;
}
:host([disabled]) {
pointer-events: none;
opacity: 0.33;
}
:host([hidden]) {
display: none !important;
}
:host([required]) > .label:after {
content: var(--ct-indicator, "*");
color: #ed4f32;
width: 1.5em;
margin-left: 4px;
text-align: center;
}
#container > div {
display: flex;
flex-direction: row;
align-items: center;
position: relative;
flex: 1;
}
#container:hover,
#container:focus {
background: rgba(26, 57, 96, 0.15);
}
#container {
display: flex;
flex-direction: row;
flex: 1;
align-items: center;
position: relative;
margin: 0 auto;
border-radius: var(--border-radius, 16px);
background: rgba(121, 130, 142, 0.1);
color: var(--color-on-surface, #535353);
transition: all 0.2s;
padding: var(--ct-input-padding, 0em 1em);
}
#container.error {
background: rgba(255, 0, 45, 0.15);
}
#container.error > div > .float-label,
#container.error > div > input::placeholder {
color: #ed4f32 !important;
}
#container.active {
background: rgba(26, 57, 96, 0.03);
}
::slotted(*),
#container.has-value > div > .charCount {
transition: all 0.2s;
}
::slotted([slot="suffix"]) {
display: inline-block;
}
::slotted([slot="prefix"]) {
display: inline-block;
padding-right: 0.5em;
}
::slotted([slot="input"]) {
height: var(--ct-input-height, 3.3em);
box-sizing: border-box;
}
::slotted(input) {
height: var(--ct-input-height, 3.3em);
box-sizing: border-box;
background: none;
font: inherit;
border: none;
outline: none;
margin: 0;
display: inline-block;
color: inherit;
font-family: inherit;
font-weight: inherit;
font-size: inherit;
letter-spacing: inherit;
word-spacing: inherit;
line-height: inherit;
text-shadow: inherit;
transition: all 0.2s;
caret-color: var(--color-primary, #2cb5e8);
}
input:invalid {
outline: none;
}
#container.has-value ::slotted([slot="input"]) {
padding: 1.4em 0 0.6em;
}
input::selection {
background: var(--color-primary, #2cb5e8);
color: white;
}
::-webkit-input-placeholder {
color: inherit;
opacity: 0.5;
}
h4 {
margin: 5px 8px 8px;
color: var(--color-on-surface, #535353);
display: block;
font-weight: 500;
font-size: 0.8rem;
max-width: 100%;
transition: all 0.2s;
}
.float-label {
font-size: 11px;
font-weight: 700;
display: block;
position: absolute;
top: 6px;
font-size: 0.75em;
font-weight: 700;
color: #676767;
opacity: 0;
transition: all 0.2s ease-in-out;
width: 100%;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
#container.has-value > div > .float-label {
opacity: 1;
visibility: visible;
}
.charCount {
text-align: right;
padding-left: 8px;
font-size: 0.75em;
}
`
];
render() {
this.dispatchEvent(new CustomEvent("my-event", { detail: {} }));
return html`
${this.label ? html` <h4 class="label">${this.label}</h4> ` : html``}
<div id="container" class="${this.getHasValue(this.value)}">
<slot name="prefix"></slot>
<div>
${this.placeholder && html` <label class="float-label">${this.placeholder}</label> `}
<slot name="input" id="input"></slot>
<slot name="suffix"></slot>
${this.charCounter ? html` <div class="charCount">${this.countChar}/${this.maxlength}</div> ` : ``}
</div>
</div>
`;
}
getHasValue(value: string) {
if (this.placeholder && value) {
return `has-value`;
}
return ``;
}
@query("#container") container!: HTMLElement;
@state() private__isFirstValueUpdate = true;
@property({ type: Boolean }) focused = false;
@property({ type: Boolean, reflect: true }) required = false;
/**
* The value of the searchbox
*/
@property({ type: String }) value = "";
/**
* Placeholder text when searchbox is empty
*/
@property({ type: String }) placeholder = "";
/**
* Input type
*/
@property({ type: String }) type = "text";
@state() private _invalid = false;
/**
* Mensaje de error al no complir con el pattern
*/
@property({ type: String }) errorMessage = "";
@property({ type: String }) label = "";
@property({ type: Boolean }) autocorrect: boolean = false;
@property({ type: String }) autocapitalize = "none";
@property({ type: Boolean }) noHover = false;
@property({ type: Boolean }) charCounter = false;
@property({ type: Number }) countChar = 0;
@property({ type: Number }) size = 30;
@property({ type: Number }) minlength = 0;
@property({ type: Number }) maxlength = 5000;
@property({ type: Number }) min = this.minlength;
@property({ type: Number }) max = this.maxlength;
/**
* Cuando Hago focus en el input
* @private
*/
_onFocus() {
this.container.classList.add("active");
this.container.classList.remove("error");
this.focused = true;
}
/**
* Cuando dejo de enfocar el element
* @private
*/
_onBlur() {
this.container.classList.remove("active");
this.focused = false;
}
set invalid(val) {
if (this.container == null) {
return;
}
this._invalid = val;
if (!val) {
// remover error
this.container.classList.remove("error");
} else {
this.container.classList.add("error");
}
}
get invalid() {
return this._invalid;
}
}
declare global {
interface HTMLElementTagNameMap {
"ct-input-container": CtInputContainer;
}
}