Skip to content

Commit d01632f

Browse files
committed
Prepare for more input types
1 parent a4f25a3 commit d01632f

File tree

1 file changed

+92
-13
lines changed

1 file changed

+92
-13
lines changed

src/ui.js

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {getVerboseName} from './util';
66
const TYPE_MAP = {
77
string: 'text',
88
boolean: 'checkbox',
9+
number: 'number'
910
};
11+
1012
const FIELD_MAP = {
1113
string: FormInput,
1214
integer: FormInput,
@@ -17,33 +19,110 @@ const FIELD_MAP = {
1719

1820
function handleChange(e, type, callback) {
1921
let value;
20-
if (type === 'text')
22+
if (type === 'text') {
2123
value = e.target.value;
22-
else if (type === 'checkbox')
24+
}
25+
else if (type === 'number') {
26+
value = e.target.value.trim();
27+
28+
if (value !== '' && !isNaN(Number(value)))
29+
value = Number(value);
30+
}
31+
else if (type === 'checkbox') {
2332
value = e.target.checked;
33+
}
34+
2435
callback(e.target.name, value);
2536
}
2637

2738

39+
function FormField(props) {
40+
let inputProps = {
41+
name: props.name,
42+
value: props.data,
43+
};
44+
45+
let type = props.schema.type;
46+
if (props.schema.choices) {
47+
inputProps.options = props.schema.choices;
48+
if (props.schema.multi)
49+
type = 'multiselect';
50+
else
51+
type = 'select';
52+
}
53+
if (props.schema.widget)
54+
type = props.schema.widget;
55+
56+
let InputField;
57+
58+
console.log(type)
59+
60+
switch (type) {
61+
case 'string':
62+
inputProps.type = 'text';
63+
InputField = FormInput;
64+
break;
65+
case 'number':
66+
inputProps.type = 'number';
67+
InputField = FormInput;
68+
break;
69+
case 'integer':
70+
inputProps.type = 'number';
71+
InputField = FormInput;
72+
break;
73+
case 'boolean':
74+
inputProps.type = 'checkbox';
75+
InputField = CheckInput;
76+
break;
77+
case 'checkbox':
78+
inputProps.type = 'checkbox';
79+
InputField = CheckInput;
80+
break;
81+
case 'radio':
82+
inputProps.type = 'checkbox';
83+
InputField = CheckInput;
84+
break;
85+
case 'select':
86+
inputProps.type = 'checkbox';
87+
InputField = CheckInput;
88+
break;
89+
case 'multiselect':
90+
inputProps.type = 'checkbox';
91+
InputField = CheckInput;
92+
break;
93+
default:
94+
inputProps.type = 'text';
95+
InputField = FormInput;
96+
}
97+
98+
return (
99+
<InputField
100+
{...inputProps}
101+
label={
102+
props.editable ? <span>{porps.schema.title} <Button className="edit" onClick={props.onEdit} title="Edit">Edit</Button></span>
103+
:
104+
props.schema.title
105+
}
106+
onChange={(e) => handleChange(e, inputProps.type, props.onChange)}
107+
/>
108+
);
109+
}
110+
111+
28112
export function getStringFormRow(data, schema, name, onChange, onRemove, removable, onEdit, editable) {
29-
let InputField = FIELD_MAP[schema.type];
30-
let inputType = TYPE_MAP[schema.type];
31113

32114
return (
33115
<FormRow
34116
key={name}
35117
onRemove={removable ? (e) => onRemove(name) : null}
36118
>
37-
<InputField
119+
<FormField
120+
data={data}
121+
schema={schema}
38122
name={name}
39-
label={
40-
editable ? <span>{schema.title} <Button className="edit" onClick={onEdit} title="Edit">Edit</Button></span>
41-
:
42-
schema.title
43-
}
44-
value={data}
45-
onChange={(e) => handleChange(e, inputType, onChange)}
46-
type={inputType}
123+
onChange={onChange}
124+
onEdit={onEdit}
125+
editable={editable}
47126
/>
48127
</FormRow>
49128
);

0 commit comments

Comments
 (0)