af-mobile-client-vue3 1.3.99 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,238 +1,211 @@
1
- <script setup lang="ts">
2
- import {
3
- Field as VanField,
4
- Picker as VanPicker,
5
- Popup as VanPopup,
6
- Search as VanSearch,
7
- } from 'vant'
8
- import { computed, defineEmits, defineModel, defineProps, onBeforeMount, ref, watch } from 'vue'
9
-
10
- const props = defineProps({
11
- columns: {
12
- type: Array,
13
- default() {
14
- return []
15
- },
16
- },
17
- option: {
18
- type: Object,
19
- default() {
20
- return { text: 'label', value: 'value', children: 'children' }
21
- },
22
- },
23
- offOption: { // 关闭option配置key-value;当数据是非集合的数组的时候,开启
24
- type: Boolean,
25
- default: false,
26
- },
27
- border: { // 是否展示边框
28
- type: Boolean,
29
- default: false,
30
- },
31
- lazyLoad: { // 是否启用懒加载
32
- type: String,
33
- default: 'false',
34
- },
35
- onSearch: { // 懒加载时的搜索函数
36
- type: Function,
37
- default: null,
38
- },
39
- })
40
- const emits = defineEmits(['confirm', 'change', 'cancel', 'input'])
41
- const show = ref(false)
42
- const resultValue = defineModel()
43
- const columnsData = ref([])
44
- const selectedOption = ref([])
45
- const searchValue = ref('')
46
- const filteredColumns = ref([])
47
- const isLoading = ref(false)
48
-
49
- // 转换空children为空字符串
50
- function transformColumns(data) {
51
- return data.map((item) => {
52
- if (item.children && item.children.length === 0) {
53
- return {
54
- ...item,
55
- children: '',
56
- }
57
- }
58
- else if (item.children && item.children.length !== 0) {
59
- return { ...item, children: transformColumns(item.children) }
60
- }
61
- else {
62
- return { ...item }
63
- }
64
- })
65
- }
66
-
67
- // 搜索过滤函数
68
- function handleSearch(value) {
69
- // 确保 value 是字符串
70
- const searchText = typeof value === 'string' ? value : value?.target?.value || ''
71
- searchValue.value = searchText
72
-
73
- // 如果是懒加载模式
74
- if (props.lazyLoad && props.lazyLoad === 'true' && props.onSearch) {
75
- if (!searchText) {
76
- filteredColumns.value = []
77
- return
78
- }
79
-
80
- isLoading.value = true
81
- // 调用父组件传递的搜索函数
82
- props.onSearch(searchText).then((results) => {
83
- filteredColumns.value = transformColumns(results || [])
84
- isLoading.value = false
85
- }).catch((error) => {
86
- console.error('懒加载搜索失败:', error)
87
- filteredColumns.value = []
88
- isLoading.value = false
89
- })
90
- return
91
- }
92
-
93
- // 普通搜索模式
94
- if (!searchText) {
95
- filteredColumns.value = columnsData.value
96
- return
97
- }
98
-
99
- const searchTextLower = searchText.toLowerCase()
100
-
101
- filteredColumns.value = columnsData.value.filter((item) => {
102
- const text = props.offOption ? item : item[props.option.text]
103
- return text?.toString().toLowerCase().includes(searchTextLower)
104
- })
105
- }
106
-
107
- // 清空搜索
108
- function handleSearchClear() {
109
- searchValue.value = ''
110
- if (props.lazyLoad) {
111
- // 懒加载模式:清空搜索时重新获取初始数据
112
- if (props.onSearch) {
113
- props.onSearch('').then((results) => {
114
- filteredColumns.value = transformColumns(results || [])
115
- }).catch((error) => {
116
- console.error('清空搜索后数据加载失败:', error)
117
- filteredColumns.value = []
118
- })
119
- }
120
- }
121
- else {
122
- filteredColumns.value = columnsData.value
123
- }
124
- }
125
-
126
- onBeforeMount(() => {
127
- columnsData.value = transformColumns(props.columns)
128
- filteredColumns.value = columnsData.value
129
- })
130
-
131
- const resultLabel = computed({
132
- get() {
133
- if (!resultValue.value)
134
- return ''
135
- const res = props.columns.filter((item) => {
136
- const data = props.offOption ? item : item[props.option.value]
137
- return data === resultValue.value
138
- })
139
- return res.length ? (props.offOption ? res[0] : res[0][props.option.text]) : ''
140
- },
141
- set() {
142
-
143
- },
144
- })
145
-
146
- function onConfirm(value, _index) {
147
- resultValue.value = props.offOption ? value.selectedValues : value.selectedValues[0]
148
- // resultValue.value = value.selectedValues
149
- selectedOption.value = value.selectedOptions
150
- show.value = !show.value
151
- emits('confirm', value.selectedValues[0], value.selectedOptions)
152
- }
153
-
154
- function change(val, index) {
155
- emits('change', val, index, resultValue.value)
156
- }
157
-
158
- function cancel(val, index) {
159
- show.value = !show.value
160
- emits('cancel', val, index, resultValue.value)
161
- }
162
-
163
- function showPopu(disabled) {
164
- if (disabled !== undefined && disabled !== false)
165
- return false
166
- columnsData.value = transformColumns(props.columns)
167
- filteredColumns.value = columnsData.value
168
- searchValue.value = '' // 重置搜索
169
- show.value = !show.value
170
- }
171
-
172
- watch(() => resultValue, (newVal, _oldVal) => {
173
- columnsData.value = transformColumns(props.columns)
174
- filteredColumns.value = columnsData.value
175
- emits('input', newVal)
176
- })
177
-
178
- // 监听 columns 变化
179
- watch(() => props.columns, () => {
180
- columnsData.value = transformColumns(props.columns)
181
- filteredColumns.value = columnsData.value
182
- searchValue.value = ''
183
- }, { deep: true })
184
- </script>
185
-
186
- <template>
187
- <VanField
188
- v-model="resultLabel"
189
- v-bind="$attrs"
190
- readonly
191
- :is-link="true"
192
- :border="props.border"
193
- @click="showPopu($attrs.readonly)"
194
- />
195
- <VanPopup v-model:show="show" position="bottom">
196
- <div class="x-select-popup">
197
- <!-- 搜索框 -->
198
- <VanSearch
199
- v-model="searchValue"
200
- placeholder="搜索"
201
- @clear="handleSearchClear"
202
- @input="handleSearch"
203
- />
204
-
205
- <!-- 选择器 -->
206
- <div v-if="isLoading" class="loading-container">
207
- <div class="loading-text">
208
- 搜索中...
209
- </div>
210
- </div>
211
- <VanPicker
212
- v-else
213
- v-bind="$attrs"
214
- :columns="filteredColumns"
215
- :columns-field-names="props.option"
216
- show-toolbar
217
- :value-key="props.option.text"
218
- @cancel="cancel"
219
- @confirm="onConfirm"
220
- @change="change"
221
- />
222
- </div>
223
- </VanPopup>
224
- </template>
225
-
226
- <style scoped>
227
- .loading-container {
228
- display: flex;
229
- justify-content: center;
230
- align-items: center;
231
- height: 200px;
232
- }
233
-
234
- .loading-text {
235
- color: #969799;
236
- font-size: 14px;
237
- }
238
- </style>
1
+ <script setup lang="ts">
2
+ import {
3
+ Field as VanField,
4
+ Picker as VanPicker,
5
+ Popup as VanPopup,
6
+ Search as VanSearch,
7
+ } from 'vant'
8
+ import { computed, defineEmits, defineModel, defineProps, onBeforeMount, ref, watch } from 'vue'
9
+
10
+ const props = defineProps({
11
+ columns: {
12
+ type: Array,
13
+ default() {
14
+ return []
15
+ },
16
+ },
17
+ option: {
18
+ type: Object,
19
+ default() {
20
+ return { text: 'label', value: 'value', children: 'children' }
21
+ },
22
+ },
23
+ offOption: { // 关闭option配置key-value;当数据是非集合的数组的时候,开启
24
+ type: Boolean,
25
+ default: false,
26
+ },
27
+ border: { // 是否展示边框
28
+ type: Boolean,
29
+ default: false,
30
+ },
31
+ lazyLoad: { // 是否启用懒加载
32
+ type: String,
33
+ default: 'false',
34
+ },
35
+ onSearch: { // 懒加载时的搜索函数
36
+ type: Function,
37
+ default: null,
38
+ },
39
+ })
40
+ const emits = defineEmits(['confirm', 'change', 'cancel', 'input'])
41
+ const show = ref(false)
42
+ const resultValue = defineModel()
43
+ const columnsData = ref([])
44
+ const selectedOption = ref([])
45
+ const searchValue = ref('')
46
+ const isLoading = ref(false)
47
+
48
+ // 转换空children为空字符串
49
+ function transformColumns(data) {
50
+ return data.map((item) => {
51
+ if (item.children && item.children.length === 0) {
52
+ return {
53
+ ...item,
54
+ children: '',
55
+ }
56
+ }
57
+ else if (item.children && item.children.length !== 0) {
58
+ return { ...item, children: transformColumns(item.children) }
59
+ }
60
+ else {
61
+ return { ...item }
62
+ }
63
+ })
64
+ }
65
+ const filteredColumns = ref([])
66
+
67
+ watch(
68
+ () => searchValue.value,
69
+ async (newValue) => {
70
+ if (props.lazyLoad && props.lazyLoad === 'true' && props.onSearch) {
71
+ if (!newValue) {
72
+ filteredColumns.value = []
73
+ return
74
+ }
75
+
76
+ isLoading.value = true
77
+ try {
78
+ const results = await props.onSearch(newValue)
79
+ filteredColumns.value = transformColumns(results || [])
80
+ }
81
+ catch (error) {
82
+ console.error('懒加载搜索失败:', error)
83
+ filteredColumns.value = []
84
+ }
85
+ finally {
86
+ isLoading.value = false
87
+ }
88
+ }
89
+ else {
90
+ // 普通搜索模式
91
+ if (!newValue) {
92
+ filteredColumns.value = columnsData.value
93
+ return
94
+ }
95
+
96
+ const searchTextLower = newValue.toLowerCase()
97
+ filteredColumns.value = columnsData.value.filter((item) => {
98
+ const text = props.offOption ? item : item[props.option.text]
99
+ return text?.toString().toLowerCase().includes(searchTextLower)
100
+ })
101
+ }
102
+ },
103
+ { immediate: true },
104
+ )
105
+
106
+ onBeforeMount(() => {
107
+ columnsData.value = transformColumns(props.columns)
108
+ filteredColumns.value = columnsData.value
109
+ })
110
+
111
+ const resultLabel = computed({
112
+ get() {
113
+ if (!resultValue.value)
114
+ return ''
115
+ const res = props.columns.filter((item) => {
116
+ const data = props.offOption ? item : item[props.option.value]
117
+ return data === resultValue.value
118
+ })
119
+ return res.length ? (props.offOption ? res[0] : res[0][props.option.text]) : ''
120
+ },
121
+ set() {
122
+
123
+ },
124
+ })
125
+
126
+ function onConfirm(value, _index) {
127
+ resultValue.value = props.offOption ? value.selectedValues : value.selectedValues[0]
128
+ // resultValue.value = value.selectedValues
129
+ selectedOption.value = value.selectedOptions
130
+ show.value = !show.value
131
+ emits('confirm', value.selectedValues[0], value.selectedOptions)
132
+ }
133
+
134
+ function change(val, index) {
135
+ emits('change', val, index, resultValue.value)
136
+ }
137
+
138
+ function cancel(val, index) {
139
+ show.value = !show.value
140
+ emits('cancel', val, index, resultValue.value)
141
+ }
142
+
143
+ function showPopu(disabled) {
144
+ if (disabled !== undefined && disabled !== false)
145
+ return false
146
+ columnsData.value = transformColumns(props.columns)
147
+ filteredColumns.value = columnsData.value
148
+ searchValue.value = '' // 重置搜索
149
+ show.value = !show.value
150
+ }
151
+
152
+ watch(() => resultValue, (newVal, _oldVal) => {
153
+ columnsData.value = transformColumns(props.columns)
154
+ filteredColumns.value = columnsData.value
155
+ emits('input', newVal)
156
+ })
157
+
158
+ // 监听 columns 变化
159
+ watch(() => props.columns, () => {
160
+ columnsData.value = transformColumns(props.columns)
161
+ filteredColumns.value = columnsData.value
162
+ searchValue.value = ''
163
+ }, { deep: true })
164
+ </script>
165
+
166
+ <template>
167
+ <VanField
168
+ v-model="resultLabel"
169
+ v-bind="$attrs"
170
+ readonly
171
+ :is-link="true"
172
+ :border="props.border"
173
+ @click="showPopu($attrs.readonly)"
174
+ />
175
+ <VanPopup v-model:show="show" position="bottom">
176
+ <div class="x-select-popup">
177
+ <!-- 搜索框 -->
178
+ <VanSearch
179
+ v-model="searchValue"
180
+ placeholder="搜索"
181
+ />
182
+
183
+ <!-- 选择器 -->
184
+ <VanPicker
185
+ :loading="isLoading"
186
+ v-bind="$attrs"
187
+ :columns="filteredColumns"
188
+ :columns-field-names="props.option"
189
+ show-toolbar
190
+ :value-key="props.option.text"
191
+ @cancel="cancel"
192
+ @confirm="onConfirm"
193
+ @change="change"
194
+ />
195
+ </div>
196
+ </VanPopup>
197
+ </template>
198
+
199
+ <style scoped>
200
+ .loading-container {
201
+ display: flex;
202
+ justify-content: center;
203
+ align-items: center;
204
+ height: 200px;
205
+ }
206
+
207
+ .loading-text {
208
+ color: #969799;
209
+ font-size: 14px;
210
+ }
211
+ </style>
@@ -188,4 +188,4 @@ export interface PolygonLayerConfig {
188
188
  onClick?: (polygon: PolygonData, event: any) => void
189
189
  /** 多边形数据提供者 */
190
190
  dataProvider?: () => PolygonData[] | Promise<PolygonData[]>
191
- }
191
+ }