-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathform.js
81 lines (73 loc) · 1.58 KB
/
form.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
import React from 'react';
import {render} from 'react-dom';
import RangeSelector, {initialFormState, generatePropsWithData} from '../../src/index';
const selectors = [
{
name: 'Cores',
range: [3,8]
},
{
name: 'Frequency',
customiseRange: [2.5,3.4,4.2,6.3],
initialSelected: [0,2]
},
{
name: 'PCIE slots',
range: [1,4],
initialSelected: [0,1]
},
{
name: 'Ram size',
customiseRange: [2,4,6,8],
initialSelected: [2,3]
}
];
class Form extends React.Component {
displayName: 'TestForm';
constructor(props) {
super(props);
this.state = {
...initialFormState(selectors)
};
}
listOfSelectors () {
return selectors.map(
(selector) => {
const data = {
...selector,
callback: (data) => this.rangeUpdate(data)
};
return (
<RangeSelector {...generatePropsWithData(data)}/>
);
}
);
}
rangeUpdate({selectedIndex,section,values}) {
this.setState((prev) => {
let newSet = prev.indexRange,
newValues = prev.values;
newSet[section] = selectedIndex;
newValues[section] = values;
return {indexRange: newSet, values: newValues};
});
}
submit(e) {
e.preventDefault();
alert(JSON.stringify(this.state));
}
render() {
return (
<form >
<fieldset>
{this.listOfSelectors()}
<input onClick={e => this.submit(e)}
type="submit"
value="submit"
/>
</fieldset>
</form>
);
}
}
render(<Form/>, document.getElementById('form'));