-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.jsx
90 lines (79 loc) · 2.11 KB
/
Index.jsx
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
import $ from 'jquery';
import React, { PropTypes } from 'react';
const levels = ['省份', '城市', '区县'];
class ChinaCity extends React.Component {
constructor(props) {
super(props);
this.state = {
selected: props.selected,
list: props.list,
};
this.select = e => {
const dom = e.target;
const level = +dom.dataset.level;
if (level === levels.length - 1 || +dom.value === 0) return;
this.getList(dom.value, result => {
const list = this.state.list.map((x, i) => {
if (i > level) {
this.refs[`select${i}`].value = 0;
}
if (i === level + 1) return result;
if (i > level + 1) return [];
return x;
});
this.setState({ list });
});
};
}
componentDidMount() {
const { selected } = this.state;
if (!selected) return;
const result = [
`${selected / 1000 | 0}000`,
`${selected / 100 | 0}00`,
selected,
];
result.forEach((val, index) => {
this.refs[`select${index}`].value = val;
});
}
getList(id, cb) {
$.get(`${this.props.route}/${id}`)
.done(d => {
if (Array.isArray(d)) cb(d);
})
.fail(xhr => {
console.error(xhr);
});
}
render() {
return (
<div>
{
levels.map((levelName, index) => (
<span className="select-wrapper" key={index}>
<select key={index} onChange={this.select} data-level={index}
name={levels.length - 1 === index ? this.props.resultName : ''}
ref={`select${index}`}
>
<option value="0">{levelName}</option>
{(this.state.list[index] || []).map((x, i) => <option key={i} value={x[1]}>{x[0]}</option>)}
</select>
</span>
))
}
</div>
);
}
}
ChinaCity.defaultProps = {
resultName: 'user[city]',
route: '/china_city',
};
ChinaCity.propTypes = {
list: PropTypes.array.isRequired,
selected: PropTypes.number,
resultName: PropTypes.string,
route: PropTypes.string,
};
export default ChinaCity;