summaryrefslogtreecommitdiff
path: root/animism-align/frontend/common/form.component.js
blob: f3775a2166067d1f589e0af9037ba4ea3f687fdc (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
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
import React, { Component } from 'react';
import { courtesyS } from '../util'

export const TextInput = props => (
  <label className={props.error ? 'error' : 'text'}>
    {props.title && <span>{props.title}</span>}
    <input
      type="text"
      required={props.required}
      onChange={props.onChange}
      name={props.name}
      value={props.data[props.name]}
      placeholder={props.placeholder}
      autoComplete={props.autoComplete}
    />
  </label>
)

export const LabelDescription = props => (
  <label className={'text description'}>
    <span>{props.title}</span>
    <span>{props.children}</span>
  </label>
)

export const NumberInput = props => (
  <label className={props.error ? 'error' : 'text'}>
    <span>{props.title}</span>
    <input
      type="number"
      required={props.required}
      onChange={props.onChange}
      name={props.name}
      value={props.data[props.name]}
      min={props.min}
      max={props.max}
      step={props.step || 1}
    />
  </label>
)

export const ColorInput = props => (
  <label className={props.error ? 'error color' : 'text color'}>
    <span>{props.title}</span>
    <input
      type="color"
      required={props.required}
      onChange={props.onChange}
      name={props.name}
      value={props.data[props.name]}
    />
    <input
      type="text"
      required={props.required}
      onChange={props.onChange}
      name={props.name}
      value={props.data[props.name]}
    />
  </label>
)

export const TextArea = props => (
  <label className={props.error ? 'textarea error' : 'textarea'}>
    {props.title && <span>{props.title}</span>}
    <textarea
      onChange={props.onChange}
      name={props.name}
      value={props.data[props.name]}
    />
  </label>
)

export const Checkbox = props => (
  <label className="checkbox">
    <input
      type="checkbox"
      name={props.name}
      value={1}
      checked={props.checked}
      onChange={(e) => props.onChange(props.name, e.target.checked)}
    />
    <span>{props.label}</span>
  </label>
)

export const Radio = props => {
  return (
    <label className="radio">
      <input
        type="radio"
        name={props.name}
        value={props.value}
        checked={props.value === props.currentValue}
        onChange={() => props.onChange(props.name, props.value)}
      />
      <span>{props.label}</span>
    </label>
  )
}

export class Select extends Component {
  state = {
    focused: false,
  }

  render() {
    const { name, selected, options, defaultOption, title, loading, onChange, className } = this.props
    if (loading) {
      return <label className='select'><div>Loading...</div></label>
    }
    const { focused } = this.state
    return (
      <label>
        {title && <span>{title}</span>}
        <div className={(focused ? 'select focus' : 'select') + " " + (className || "")}>
          <div>{(options.find(opt => opt.name === selected) || {label: defaultOption}).label}</div>
          <select
            onFocus={() => this.setState({ focused: true })}
            onBlur={() => this.setState({ focused: false })}
            onChange={e => {
              onChange(name, e.target.value)
              // this.setState({ focused: false })
            }}
            value={selected || "__default__"}
          >
            {!selected && defaultOption && <option value="__default__">{defaultOption}</option>}
            {options.map((option, i) => (
              <option
                key={option.name}
                value={option.name}
                disabled={option.disabled}
              >{option.label}</option>
            ))}
          </select>
        </div>
      </label>
    )
  }
}

export class FileInputField extends Component {
  state = {
    count: 0,
  }

  handleChange(files) {
    const { multiple, onChange } = this.props
    if (!files) {
      this.setState({ count: 0 })
    } else {
      this.setState({ count: multiple ? files.length : 0 })
    }
    onChange(files)
  }
  
  render() {
    const { error, title, label, required, multiple, mime, name } = this.props
    return (
      <label className={error ? 'error' : 'text fileInput'}>
        <span>{title}</span>
        <div className="row">
          <button>
            {label || "Choose files"}
            <FileInput
              mime={mime}
              multiple={multiple}
              onChange={this.handleChange.bind(this)}
            />
          </button>
          {!!this.state.count && <span>{courtesyS(this.state.count, "file")}{" selected"}</span>}
        </div>
      </label>
    )
  }
}

export class FileInput extends Component {
  handleChange(e) {
    let { multiple, mime } = this.props
    if (!mime) {
      mime = "image/"
    }
    const files = e.dataTransfer ? e.dataTransfer.files : e.target.files
    let i
    let file, selectedFiles = []
    for (i = 0; i < files.length; i++) {
      file = files[i]
      if (file && file.type.indexOf(mime) === 0) {
        if (multiple) {
          selectedFiles.push(file)
        } else {
          break
        }
      }
    }
    if (multiple && selectedFiles.length) {
      this.props.onChange(selectedFiles)
    } else if (!multiple && file) {
      this.props.onChange(file)
    } else {
      this.props.onChange()
    }
  }

  render() {
    return (
      <input type="file" multiple={!!this.props.multiple} onChange={this.handleChange.bind(this)} />
    )
  }
}

export const SubmitButton = (props) => (
  <label>
    <span></span>
    <button
      className={props.className ? "submit " + props.className : "submit"}
      onClick={props.onClick}
    >{props.title}</button>
  </label>
)