-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb-input.tsx
224 lines (192 loc) · 7.15 KB
/
db-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
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
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Component, Event, h, Host, Prop, State } from '@stencil/core';
import { uuid } from '../../utils/utils';
@Component({
tag: 'db-input',
styleUrl: 'db-input.scss'
})
export class DbInput {
@State() valueSize = 0;
private inputElement!: HTMLInputElement;
/**
* The ariainvalid attribute is used to indicate that the value entered into an input field does not conform to the format expected by the application.
*/
@Prop({ reflect: true }) ariainvalid?:
| 'false'
| 'grammar'
| 'spelling'
| 'true' = null;
/**
* The ariarequired attribute can be applied to a form element, to indicate to an AT that it is required to complete the form.
*/
@Prop({ reflect: true }) ariarequired?: 'false' | 'true' = null;
/**
* User agents sometimes have features for helping users fill forms in, for example prefilling the user's address based on earlier user input.
*/
@Prop({ reflect: true }) autocomplete?: 'off' | 'on' = null;
/**
* The autofocus content attribute allows the author to indicate that a control is to be focused as soon as the page is loaded, allowing the user to just start typing without having to manually focus the main control.
*/
@Prop({ reflect: true }) autofocus = false;
/**
* The description attribute specifies the description/hint of the input.
*/
@Prop({ reflect: true }) description?: string;
/**
* The dirname attribute on a form control element enables the submission of the directionality of the element, and gives the name of the control that contains this value during form submission. If such an attribute is specified, its value must not be the empty string.
*/
@Prop({ reflect: true }) dirname?: string;
/**
* The disabled attribute can be set to keep a user from clicking on the input.
*/
@Prop({ reflect: true }) disabled = false;
/**
* The input_id of a labelable form-related element in the same document as the label element. The first element in the document with an id matching the value of the for attribute is the labeled control for this label element, if it is a labelable element.
*/
@Prop({ reflect: true }) input_id?: string = 'input-' + uuid();
/**
* The label attribute specifies the caption of the input.
*/
@Prop({ reflect: true }) label!: string;
/**
* The list attribute is used to identify an element that lists predefined options suggested to the user.
*/
@Prop({ reflect: true }) list?: string;
/**
/* The maxlength attribute, controlled by a dirty value flag, declares a limit on the number of characters a user can input.
*/
@Prop({ reflect: true }) maxlength?: number;
/**
/* The minlength attribute, when it applies, is a form control minlength attribute.
*/
@Prop({ reflect: true }) minlength?: number;
/**
* The name content attribute gives the name of the form control, as used in form submission and in the form element's elements object. If the attribute is specified, its value must not be the empty string.
*/
@Prop({ reflect: true }) name?: string;
/**
* The pattern attribute specifies a regular expression against which the control's value is to be checked.
*/
@Prop({ reflect: true }) pattern?: string;
/**
* The step attribute specifies the granularity that the value must obey to on increasing or decreasing by the users selection.
*/
@Prop({ reflect: true }) step?: number;
/**
* The min attribute specifies the minimum value that is sufficient for this input.
*/
@Prop({ reflect: true }) min?: number | string;
/**
* The max attribute specifies the maximum value that is sufficient for this input.
*/
@Prop({ reflect: true }) max?: number | string;
/**
* The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry.
*/
@Prop({ reflect: true }) placeholder?: string;
/**
* The readonly attribute controls whether or not the user can edit the form control.
*/
@Prop({ reflect: true }) readonly?: boolean;
/**
* The required attribute is a boolean attribute. When specified, the element is required.
*/
@Prop({ reflect: true }) required?: boolean;
/**
* The size attribute gives the number of characters that, in a visual rendering, the user agent is to allow the user to see while editing the element's value.
*/
@Prop({ reflect: true }) size?: number;
/**
* The type attribute changes the input type to text, number etc.
*/
@Prop({ reflect: true }) type?: string = 'text';
/**
* The value content attribute gives the default value of the input element.
*/
@Prop({ reflect: true }) value?: string;
/**
* The variant attribute specifies a visual expression of a select.
*/
@Prop({ reflect: true }) variant?:
| 'semitransparent'
| 'white'
| 'solid'
| 'outline' = 'semitransparent';
/**
* The label-hidden attribute is a boolean attribute. When specified, the elements label gets visually hidden (it's important to still keep it displayed for accessibility reasons).
*/
@Prop({ reflect: true }) labelHidden: string;
private handleChange(event) {
this.dbChange.emit(event);
}
/**
* Mapping for default change Event
*/
@Event() dbChange;
componentDidRender() {
this.valueSize = this.inputElement?.value?.length || 0;
}
render() {
return (
<Host>
<input
type={this.type}
class="elm-input"
id={this.input_id}
aria-invalid={this.ariainvalid}
aria-required={this.ariarequired}
autocomplete={this.autocomplete}
autofocus={this.autofocus}
data-dirname={this.dirname}
disabled={this.disabled}
list={this.list}
max={this.max}
maxlength={this.maxlength}
min={this.min}
minlength={this.minlength}
name={this.name}
pattern={this.pattern}
placeholder={this.placeholder}
readonly={this.readonly}
required={this.required}
size={this.size}
step={this.step}
value={this.value}
aria-labelledby={this.input_id + '-label'}
data-variant={this.variant}
onChange={(event) => this.handleChange(event)}
onInput={(event) => {
this.valueSize = (event.target as HTMLInputElement).value.length;
}}
ref={(el) => (this.inputElement = el as HTMLInputElement)}
/>
<label
class="elm-label"
htmlFor={this.input_id}
aria-hidden="true"
id={this.input_id + '-label'}
data-label-hidden={this.labelHidden}
>
{this.label}
</label>
{this.maxlength && (
<output htmlFor={this.input_id} id={`${this.input_id}-result`}>
{`${this.valueSize} / ${this.maxlength}`}
</output>
)}
{this.description && (
<p id={this.input_id + '-hint'} class="description">
{this.description}
</p>
)}
{this.list ? (
<datalist id={this.list}>
<slot />
</datalist>
) : (
''
)}
</Host>
);
}
}