Skip to content

Commit dff47e7

Browse files
committed
Add radio and select input support
1 parent d01632f commit dff47e7

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

src/components/form.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function FormInput({label, help_text, error, ...props}) {
1212
}
1313

1414

15-
export function CheckInput({label, help_text, error, value, ...props}) {
15+
export function FormCheckInput({label, help_text, error, value, ...props}) {
1616

1717
if (!label)
1818
label = props.name.toUpperCase();
@@ -34,6 +34,59 @@ export function CheckInput({label, help_text, error, value, ...props}) {
3434
}
3535

3636

37+
export function FormRadioInput({label, help_text, error, value, options, ...props}) {
38+
return (
39+
<div>
40+
<label>{label}</label>
41+
{options.map((option, i) => {
42+
let label, inputValue;
43+
if (typeof option === 'object') {
44+
label = option.label;
45+
inputValue = option.value;
46+
} else {
47+
label = option;
48+
inputValue = option;
49+
}
50+
51+
return (
52+
<label key={label + '_' + inputValue + '_' + i}>
53+
<input {...props} value={inputValue} checked={inputValue === value} /> {label}
54+
</label>
55+
);
56+
})}
57+
</div>
58+
);
59+
}
60+
61+
62+
export function FormSelectInput({label, help_text, error, value, options, ...props}) {
63+
return (
64+
<div>
65+
{label && <label>{label}</label>}
66+
<select defaultValue="" {...props}>
67+
<option disabled value="" key={'__placehlder'}>Select...</option>
68+
{options.map((option, i) => {
69+
let label, inputValue;
70+
if (typeof option === 'object') {
71+
label = option.label;
72+
inputValue = option.value;
73+
} else {
74+
label = option;
75+
inputValue = option;
76+
}
77+
78+
return (
79+
<option value={inputValue} key={label + '_' + inputValue + '_' + i}>
80+
{label}
81+
</option>
82+
);
83+
})}
84+
</select>
85+
</div>
86+
);
87+
}
88+
89+
3790
export function FileInput(props) {
3891
return <FormInput {...props} />
3992
}

src/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Button from './buttons';
2-
import {FormInput, CheckInput, FileInput} from './form';
2+
import {FormInput, FormCheckInput, FormRadioInput, FormSelectInput, FileInput} from './form';
33
import {FormRow, FormGroup} from './containers';
44

55
export {
66
Button,
7-
FormInput, CheckInput, FileInput,
7+
FormInput, FormCheckInput, FormRadioInput, FormSelectInput, FileInput,
88
FormRow, FormGroup
99
};

src/ui.js

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
import {getBlankData} from './data';
2-
import {Button, FormInput, CheckInput, FormRow, FormGroup} from './components';
2+
import {Button, FormInput, FormCheckInput, FormRadioInput, FormSelectInput,
3+
FormRow, FormGroup} from './components';
34
import {getVerboseName} from './util';
45

56

6-
const TYPE_MAP = {
7-
string: 'text',
8-
boolean: 'checkbox',
9-
number: 'number'
10-
};
11-
12-
const FIELD_MAP = {
13-
string: FormInput,
14-
integer: FormInput,
15-
number: FormInput,
16-
boolean: CheckInput
17-
};
18-
19-
20-
function handleChange(e, type, callback) {
7+
function handleChange(e, valueType, callback) {
8+
let type = e.target.type
219
let value;
22-
if (type === 'text') {
10+
11+
if (type === 'checkbox') {
12+
value = e.target.checked;
13+
} else {
2314
value = e.target.value;
2415
}
25-
else if (type === 'number') {
26-
value = e.target.value.trim();
2716

17+
if (valueType === 'number') {
18+
value = value.trim();
2819
if (value !== '' && !isNaN(Number(value)))
2920
value = Number(value);
3021
}
31-
else if (type === 'checkbox') {
32-
value = e.target.checked;
33-
}
3422

3523
callback(e.target.name, value);
3624
}
@@ -45,18 +33,13 @@ function FormField(props) {
4533
let type = props.schema.type;
4634
if (props.schema.choices) {
4735
inputProps.options = props.schema.choices;
48-
if (props.schema.multi)
49-
type = 'multiselect';
50-
else
51-
type = 'select';
36+
type = 'select';
5237
}
5338
if (props.schema.widget)
5439
type = props.schema.widget;
5540

5641
let InputField;
5742

58-
console.log(type)
59-
6043
switch (type) {
6144
case 'string':
6245
inputProps.type = 'text';
@@ -72,23 +55,18 @@ function FormField(props) {
7255
break;
7356
case 'boolean':
7457
inputProps.type = 'checkbox';
75-
InputField = CheckInput;
58+
InputField = FormCheckInput;
7659
break;
7760
case 'checkbox':
7861
inputProps.type = 'checkbox';
79-
InputField = CheckInput;
62+
InputField = FormCheckInput;
8063
break;
8164
case 'radio':
82-
inputProps.type = 'checkbox';
83-
InputField = CheckInput;
65+
inputProps.type = 'radio';
66+
InputField = FormRadioInput;
8467
break;
8568
case 'select':
86-
inputProps.type = 'checkbox';
87-
InputField = CheckInput;
88-
break;
89-
case 'multiselect':
90-
inputProps.type = 'checkbox';
91-
InputField = CheckInput;
69+
InputField = FormSelectInput;
9270
break;
9371
default:
9472
inputProps.type = 'text';
@@ -103,7 +81,7 @@ function FormField(props) {
10381
:
10482
props.schema.title
10583
}
106-
onChange={(e) => handleChange(e, inputProps.type, props.onChange)}
84+
onChange={(e) => handleChange(e, props.schema.type, props.onChange)}
10785
/>
10886
);
10987
}

0 commit comments

Comments
 (0)