aboutsummaryrefslogtreecommitdiff
path: root/src/lib/form.ts
blob: a1f8cfd02efcc0780a486cea9b176a28f2647852 (plain)
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
import { h } from 'lib/h'
import * as Layout from 'lib/layout'
import * as Button from 'lib/button'

export function input(id: string, label: string, attrs: object): Element {
  return h('div',
    { className: 'g-Form__Field' },
    h('div',
      { className: 'g-Form__Label' },
      h('label', { for: id }, label)
    ),
    h('input', { id: id, ...attrs })
  )
}

export function colorInput(
  defaultColors: string[],
  id: string,
  label: string,
  initValue: string,
  onInput: (value: string) => void
): Element {
  const input = h('input',
    { id,
      value: initValue,
      type: 'color',
      oninput: (e: Event) => {
        if (e.target !== null) {
          onInput((e.target as HTMLInputElement).value)
        }
      }
    }
  ) as HTMLInputElement
  return h('div',
    { className: 'g-Form__Field' },
    h('div',
      { className: 'g-Form__Label' },
      h('label', { for: id }, label)
    ),
    Layout.line(
      {},
      ...(defaultColors.map(color =>
        Button.raw({ className: 'g-Form__DefaultColor',
          style: `background-color: ${color}`,
          type: 'button',
          onclick: () => {
            input.value = color
            onInput(color)
          }
        })
      ).concat(input))
    )
  )
}

export function textarea(
  id: string,
  label: string,
  initValue: string,
  onInput: (value: string) => void
): Element {
  return h('div',
    { className: 'g-Form__Field' },
    h('div',
      { className: 'g-Form__Label' },
      h('label', { for: id }, label)
    ),
    h('textarea',
      { id,
        className: 'g-Form__Textarea',
        oninput: (e: Event) => {
          if (e.target !== null) {
            onInput((e.target as HTMLTextAreaElement).value)
          }
        }
      },
      initValue
    )
  )
}