-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathdynamic-options.js
More file actions
73 lines (66 loc) · 1.63 KB
/
Copy pathdynamic-options.js
File metadata and controls
73 lines (66 loc) · 1.63 KB
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
/* eslint-disable no-console */
import 'rc-cascader/assets/index.less';
import Cascader from 'rc-cascader';
import React from 'react';
import ReactDOM from 'react-dom';
const addressOptions = [{
label: '福建',
isLeaf: false,
value: 'fj',
}, {
label: '浙江',
isLeaf: false,
value: 'zj',
}, {
label: '北京',
isLeaf: false,
value: 'bj',
}];
class Demo extends React.Component {
state = {
inputValue: '',
options: addressOptions,
}
onChange = (value, selectedOptions) => {
console.log(value, selectedOptions);
this.setState({
inputValue: selectedOptions.map(o => o.label).join(', '),
});
}
loadData = (selectedOptions) => {
const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
// 动态加载下级数据
setTimeout(() => {
targetOption.loading = false;
if (targetOption.value === 'bj') {
targetOption.children = [];
} else {
targetOption.children = [{
label: `${targetOption.label}动态加载1`,
value: 'dynamic1',
}, {
label: `${targetOption.label}动态加载2`,
value: 'dynamic2',
}];
}
this.setState({
options: [...this.state.options],
});
}, 1000);
}
render() {
return (
<Cascader
options={this.state.options}
loadData={this.loadData}
onChange={this.onChange}
changeOnSelect
notFoundContent={'无数据'}
>
<input value={this.state.inputValue} readOnly />
</Cascader>
);
}
}
ReactDOM.render(<Demo />, document.getElementById('__react-content'));