-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinputs.js
84 lines (72 loc) · 2.51 KB
/
inputs.js
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
import React, { useState } from "react";
import { ErrorList } from "./error-list";
const CheckboxInput = ({name, id, value, label, disabled, onChange, helpText}) => {
return (
<div className="checkbox-row">
<input
type="checkbox"
id={id}
name={name}
checked={value}
disabled={disabled}
onChange={(event ) => {
if (onChange) {
onChange(event.target.checked);
}
}}
/>
{label ? <label className="vCheckboxLabel" htmlFor={id}>{ label }</label> : null}
{helpText ? <div><span className="help" htmlFor={id}>{helpText}</span></div>: null}
</div>
);
};
const TextInput = ({id, name, value, label, onChange, hidden, helpText}) => {
return (
<div>
{label ? <label htmlFor={id}>{label}</label> : null}
<input
type="text"
id={id}
name={name}
hidden={hidden}
onChange={ (event) => {
if (onChange) {
onChange(event.target.value);
}
}}
value={value}
/>
{helpText ? <div><span className="help" htmlFor={id}>{helpText}</span></div>: null}
</div>
);
};
const SelectInput = ({choices, name, id, label, onChange, initialValue, errors, helpText}) => {
const [currentValue, setCurrentValue] = useState(initialValue || "");
const [_errors, setErrors] = useState(errors || []);
const options = choices.map( ([value, label], index) =>
<option key={index} value={value}>{label}</option>
);
return (
<div>
<ErrorList errors={_errors} />
<label className="required" htmlFor={id}>{label}</label>
<select
name={name}
required={true}
id={id}
value={currentValue}
onChange={ (event, value) => {
setCurrentValue(value);
setErrors([]);
if (onChange) {
onChange(event.target.value);
}
}}
>
{options}
</select>
{helpText ? <div><span className="help" htmlFor={id}>{helpText}</span></div>: null}
</div>
)
}
export { CheckboxInput, TextInput, SelectInput };