blob: 53ebf03103676d2ae037034ed3e2dfbb27a49d49 (
plain) (
blame)
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
|
import styled from "styled-components";
import React from "react";
const Form = styled.div``;
const Row = styled.div``;
const Label = styled.label`
display: inline-block;
min-width: 5rem;
font-weight: bold;
`;
const Input = styled.input`
line-height: 2.2rem;
`;
interface Props {
types: { [key: string]: string };
value: { [key: string]: any };
onChange: (value: any) => void;
onBlur: () => void;
}
class PropertiesForm extends React.Component<Props> {
public static defaultProps: Props = {
types: {},
value: {},
onChange: () => {},
onBlur: () => {},
};
render() {
const types = this.props.types;
const values = this.props.value;
return (
<Form>
{Object.keys(types).map((name) => {
const type = types[name];
let inputType = "";
let onChange = this.bindValue.bind(this);
if (type === "string") {
inputType = "text";
} else if (type === "number") {
inputType = "number";
} else if (type === "boolean") {
inputType = "checkbox";
// Settings are saved onBlur, but checkbox does not fire it
onChange = (e) => {
this.bindValue(e);
this.props.onBlur();
};
} else {
return null;
}
return (
<Row key={name}>
<Label>
<span>{name}</span>
<Input
type={inputType}
name={name}
value={values[name] ? values[name] : ""}
onChange={onChange}
onBlur={this.props.onBlur}
checked={values[name]}
/>
</Label>
</Row>
);
})}
</Form>
);
}
bindValue(e: React.ChangeEvent<HTMLInputElement>) {
const name = e.target.name;
const next = { ...this.props.value };
if (e.target.type.toLowerCase() === "checkbox") {
next[name] = e.target.checked;
} else if (e.target.type.toLowerCase() === "number") {
next[name] = Number(e.target.value);
} else {
next[name] = e.target.value;
}
this.props.onChange(next);
}
}
export default PropertiesForm;
|