af-mobile-client-vue3 1.4.15 → 1.4.16

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,211 +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 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>
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>