generated from idea2app/Taro-Vant-MobX-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaSelect.tsx
87 lines (75 loc) · 2.01 KB
/
AreaSelect.tsx
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
import { Area, FormItem, Popup } from '@antmjs/vantui';
import { AreaProps } from '@antmjs/vantui/types/area';
import { FormItemProps } from '@antmjs/vantui/types/form';
import { areaList } from '@vant/area-data';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import { PureComponent } from 'react';
export type Area = Record<'name' | 'code', string>;
export interface AreaSelectProps
extends Pick<FormItemProps, 'name' | 'required'>,
Omit<AreaProps, 'onChange'> {
onChange: (value: string) => any;
}
@observer
export class AreaSelect extends PureComponent<AreaSelectProps> {
@observable
show = false;
AreaMap = {
...areaList.province_list,
...areaList.city_list,
...areaList.county_list
};
get nameValue() {
const { value } = this.props;
return (
value &&
[...value]
.map((_, index, code) =>
index % 2
? this.AreaMap[
code
.slice(0, index + 1)
.join('')
.padEnd(6, '0')
]
: ''
)
.filter(Boolean)
.join(' ')
);
}
close = () => (this.show = false);
change: AreaProps['onConfirm'] = ({ detail: { value } }) => {
this.close();
this.props.onChange?.((value as Area[]).slice(-1)[0].code);
};
render() {
const { title, name, value, columnsNum } = this.props,
{ show, nameValue } = this;
return (
<>
<FormItem label={title} name={name}>
<span onClick={() => (this.show = true)}>
{nameValue || '请选择地区'}
</span>
</FormItem>
<Popup
show={show}
position='bottom'
style={{ height: '50vh' }}
onClose={this.close}
>
<Area
title={title}
areaList={areaList}
columnsNum={columnsNum}
value={value}
onCancel={this.close}
onConfirm={this.change}
/>
</Popup>
</>
);
}
}