MyComponent.vue
2.11 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
74
75
76
77
78
79
80
81
<!--
* @Date: 2023-03-29 15:27:02
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-08-07 18:43:14
* @FilePath: /data-table/src/components/AreaPickerField/MyComponent.vue
* @Description: 文件描述
-->
<template>
<div style="width: 100%;">
<van-field
v-model="fieldValue"
is-link
readonly
:required="props.component_props.required"
placeholder="请选择省市区"
@click="onClick"
:border="show_address ? true : false"
/>
<van-field
v-if="show_address"
v-model="address"
placeholder="请填写详细地址"
:border="false"
:readonly="props.component_props.readonly"
/>
<van-divider />
<van-popup v-model:show="showPicker" position="bottom">
<van-area
v-model="city_code"
title=""
:area-list="areaList"
@confirm="onConfirm"
@cancel="showPicker = false"
/>
</van-popup>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { areaList } from "@vant/area-data";
import { useCustomFieldValue } from '@vant/use';
// 获取父组件传值
const props = inject('props');
const show_address = ref(!props.component_props.no_street);
const address = ref(props.component_props.default?.input_value);
const city_code = ref(props.component_props.default?.city_code);
const showPicker = ref(false);
let fieldValue = ref(props.component_props.default?.picker_value);
const onClick = () => {
if (!props.component_props.readonly) { // 非只读状态下可以点击弹出
showPicker.value = true;
}
};
const result_value = computed(() => {
return {
address: fieldValue.value + ' ' + address.value,
city_code: city_code.value,
picker_value: fieldValue.value,
input_value: address.value,
}
})
const onConfirm = ({ selectedOptions }) => {
fieldValue.value = selectedOptions.map((option) => option.text).join(" ");
city_code.value = selectedOptions[2]?.value;
showPicker.value = false;
};
// 此处传入的值会替代 Field 组件内部的 value
useCustomFieldValue(() => result_value.value);
</script>
<style lang="less" scoped>
</style>