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
|
import { h, Html, Rx, RxAble } from 'lib/rx'
import * as rx from 'lib/rx'
import * as L from 'lib/loadable'
import * as icons from 'lib/icons'
interface InputParams {
label: string
type?: string
initValue?: string
select?: boolean
onUpdate: (value: string) => void
required?: boolean
}
export function input({ label, type, initValue, select, onUpdate, required }: InputParams): Html {
return h('label',
{ className: 'g-Label' },
label,
h('input',
{
type: type ?? 'text',
className: 'g-Input',
onmount: (element: HTMLInputElement) => {
if (select) {
element.select()
}
},
oninput: (event: Event) => {
let value = (event.target as HTMLInputElement).value
onUpdate(type == 'password' ? value : value.trim())
},
value: initValue,
required
}
)
)
}
interface TextareaParams {
label: string
initValue?: string
select?: boolean
onUpdate: (value: string) => void
required?: boolean
}
export function textarea({ label, initValue, select, onUpdate, required }: TextareaParams): Html {
return h('label',
{ className: 'g-Label' },
label,
h('textarea',
{
className: 'g-Textarea',
onmount: (element: HTMLInputElement) => {
if (select) {
element.select()
}
},
oninput: (event: Event) => onUpdate((event.target as HTMLInputElement).value.trim()),
value: initValue,
required
}
)
)
}
interface SelectParams {
label: string
initValue: string
values: { [key: string]: string }
onUpdate: (value: string) => void
required?: boolean
}
export function select({ label, initValue, values, onUpdate, required }: SelectParams): Html {
let keys = Object.keys(values)
keys.sort((a, b) => values[a].localeCompare(values[b]))
return h('label',
{ className: 'g-Label' },
label,
h('select',
{
className: 'g-Select',
onchange: (event: Event) => {
const element = event.target as HTMLSelectElement
onUpdate(element.value)
},
required
},
h('option', { label: ' ' }),
keys.map(key =>
h('option',
{
value: key,
selected: initValue == key
},
values[key]
)
)
)
)
}
export function error(requestVar: Rx<L.Loadable<any>>): Html {
return requestVar.map(l =>
L.isFailure(l)
? h('div', { className: 'g-FormError' }, l.error)
: undefined
)
}
interface SubmitParams {
label: string
className?: string
disabled?: RxAble<boolean>
requestVar?: Rx<L.Loadable<any>>
}
export function submit({ label, className, disabled, requestVar }: SubmitParams): Html {
const loadingClassname = requestVar
? requestVar.map(l => L.isLoading(l) ? 'g-Button--Loading' : '')
: rx.pure('')
return h('div',
{ className: 'g-Form__SubmitParent' },
h('input', {
type: 'submit',
className: loadingClassname.map(lc => `g-Button g-Button--Primary ${className ?? ''} ${lc}`),
value: label,
disabled
}),
loadingClassname.map(l =>
l && h('div',
{ className: 'g-Form__SubmitSpinner' },
icons.spinner()
)
)
)
}
interface ButtonParams {
style?: string
label: string
className?: string
disabled?: RxAble<boolean>
requestVar?: Rx<L.Loadable<any>>
onClick: () => void,
}
export function button({ style, label, className, disabled, requestVar, onClick }: ButtonParams): Html {
const loadingClassname = requestVar
? requestVar.map(l => L.isLoading(l) ? 'g-Button--Loading' : '')
: rx.pure('')
return h('div',
{ className: 'g-Form__SubmitParent' },
h('input',
{
type: 'button',
style,
className: loadingClassname.map(lc => `g-Button g-Button--Primary ${className ?? ''} ${lc}`),
disabled,
onclick: () => onClick(),
value: label
}
),
loadingClassname.map(l =>
l && h('div',
{ className: 'g-Form__SubmitSpinner' },
icons.spinner()
)
)
)
}
|