-
Notifications
You must be signed in to change notification settings - Fork 0
/
hole.ts
58 lines (50 loc) · 1.75 KB
/
hole.ts
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
/// <reference path="./jquery.d.ts" />
declare var Bacon: any;
module hole {
var addOptionalTextToLabel = ($inputElem, $labelElem) => {
var required = $inputElem.prop('required')
var originalLabelText = $labelElem.text()
var labelText = required ? originalLabelText : originalLabelText + " (optional)"
$labelElem.text(labelText)
$inputElem.attr({placeholder: labelText})
}
var addClassToMiniLabelWhenFocused = ($inputElem, $labelElem) => {
var isFocused = $inputElem.focusE().map(true).merge($inputElem.blurE().map(false))
isFocused.onValue((isFocused) => {
$labelElem.toggleClass('holelib-minilabel-focused', isFocused)
})
}
var hideMiniLabelWhenValueIsEmpty = ($inputElem, $labelElem) => {
var isEmpty = Bacon.$.textFieldValue($inputElem).map((val) => val.length == 0)
isEmpty.onValue((fieldIsEmpty) => {
$labelElem.toggleClass('holelib-minilabel-hidden', fieldIsEmpty)
})
}
var holeTextInput = ($container, opts) => {
var $inputElem = opts.$inputElem
var $labelElem = opts.$labelElem.addClass('holelib-minilabel')
addOptionalTextToLabel($inputElem, $labelElem)
addClassToMiniLabelWhenFocused($inputElem, $labelElem)
hideMiniLabelWhenValueIsEmpty($inputElem, $labelElem)
return $container.append(
$labelElem,
$inputElem
)
}
export function create (opts) {
var $container = $('<div>').addClass('holelib-container')
var $inputElem = opts.$inputElem
var $labelElem = opts.$labelElem
$inputElem.addClass('holelib-input')
$labelElem.addClass('holelib-label')
switch (opts.type) {
case 'text':
case 'tel':
case 'email':
case 'textarea':
default:
return holeTextInput($container, opts)
break;
}
}
}