This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.js
More file actions
63 lines (52 loc) · 1.35 KB
/
field.js
File metadata and controls
63 lines (52 loc) · 1.35 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
import { create, remove, addDelayRemoveClass } from 'uc-dom';
export default {
setValue: function(val) {
this.input.value = val;
},
getValue: function() {
return this.input.value;
},
render: function(opts) {
return create('label.input', `
<input
type="${opts.type || 'text'}"
${(opts.name ? `name="${opts.name}" ` : '')}
${(opts.value ? `value="${opts.value}" ` : '')}
>
<span>${opts.title}</span>
<em></em>`
);
},
addField: function(opts) {
this.el = opts.el || this.render(opts);
this.input = this.find('input').item(0);
this.elMessage = this.find('em').item(0);
},
removeField: function() {
clearTimeout(this.errorTimeout);
remove(this.el);
delete this.el;
delete this.elMessage;
},
getCarret: function() {
return this.input.selectionEnd;
},
setCarret: function(pos) {
this.input.setSelectionRange(pos, pos);
},
resetCarret: function(toBegin) {
this.input.focus();
const pos = toBegin ? 0 : this.input.value.length;
this.input.setSelectionRange(pos, pos);
},
error: function(msg) {
if (msg) {
this.addClass('input-message')
this.elMessage.textContent = msg;
}
this.errorTimeout = addDelayRemoveClass(this.el, 'error', 600);
},
active: function(state) {
this.input.disabled = !state;
}
}