-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.js
110 lines (100 loc) · 3.13 KB
/
App.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { useRef, useState, useCallback, useEffect } from "react";
import { SelectBox } from "devextreme-react/select-box";
import Form, { SimpleItem, Label } from "devextreme-react/form";
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';
import service from "./data.js";
const states = service.getStates();
const cities = service.getCities();
const stateEditorOptions = {
dataSource: states,
valueExpr: "ID",
displayExpr: "Name"
};
const cityEditorOptions = {
dataSource: cities,
valueExpr: "ID",
displayExpr: "Name"
};
export default function App() {
const [formData, setFormData] = useState(service.getAddress);
const [stateValue, setStateValue] = useState(null);
const [cityValue, setCityValue] = useState(null);
const citySelectBoxRef = useRef();
let cityDataSource = null;
useEffect(() => {
cityDataSource = citySelectBoxRef.current.instance.getDataSource();
}, [])
const stateValueChanged = (e) => {
cityDataSource.filter(["StateID", "=", e.value]);
cityDataSource.load();
setStateValue(e.value);
setCityValue(null)
}
const cityValueChanged = e => {
setCityValue(e.value);
};
const onFieldDataChanged = useCallback((e) => {
if (e.dataField === "StateID") {
let cityEditor = e.component.getEditor("CityID");
let dataSource = cityEditor.getDataSource();
dataSource.filter(["StateID", "=", e.value]);
dataSource.load();
setFormData({ ...formData, ...{ CityID: null, StateID: e.value } });
}
}, [])
return (
<div>
<div className="dx-fieldset" style={{width:"50%"}}>
<div className="dx-fieldset-header">Select State, City</div>
<div className="dx-field">
<div className="dx-field-label">State</div>
<div className="dx-field-value">
<SelectBox
dataSource={states}
valueExpr="ID"
displayExpr="Name"
value={stateValue}
onValueChanged={stateValueChanged}
/>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">City</div>
<div className="dx-field-value">
<SelectBox
ref={citySelectBoxRef}
dataSource={cities}
onValueChanged={cityValueChanged}
valueExpr="ID"
displayExpr="Name"
value={cityValue}
/>
</div>
</div>
<div className="dx-fieldset-header">In Form</div>
<div className="dx-field">
<Form
formData={formData}
onFieldDataChanged={onFieldDataChanged}
>
<SimpleItem
dataField="StateID"
editorType="dxSelectBox"
editorOptions={stateEditorOptions}
>
<Label text="State" />
</SimpleItem>
<SimpleItem
dataField="CityID"
editorType="dxSelectBox"
editorOptions={cityEditorOptions}
>
<Label text="City" />
</SimpleItem>
</Form>
</div>
</div>
</div>
);
}