Skip to content

Commit ed5d7ac

Browse files
author
xuqingkai
committed
fix: 🐛 修复picker选择器在APP端包装二维数组逻辑错误导致无法实现多列选择器的问题
1 parent 2102f38 commit ed5d7ac

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/uni_modules/wot-design-uni/components/wd-picker-view/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ComponentPublicInstance, ExtractPropTypes, PropType, Ref } from 'vue'
22
import { baseProps, makeArrayProp, makeBooleanProp, makeNumberProp, makeStringProp } from '../common/props'
3-
import { getType } from '../common/util'
3+
import { getType, isArray, isObj } from '../common/util'
44

55
export type ColumnItem = {
66
[key: string]: any
@@ -80,7 +80,7 @@ export type PickerViewInstance = ComponentPublicInstance<PickerViewProps, Picker
8080
* @param {string} labelKey labelKey
8181
*/
8282
export function formatArray(array: Array<string | number | ColumnItem | Array<string | number | ColumnItem>>, valueKey: string, labelKey: string) {
83-
let tempArray: Array<string | number | ColumnItem | Array<string | number | ColumnItem>> = array instanceof Array ? array : [array]
83+
let tempArray: Array<string | number | ColumnItem | Array<string | number | ColumnItem>> = isArray(array) ? array : [array]
8484
// 检测第一层的type
8585
const firstLevelTypeList = new Set(array.map(getType))
8686
/**
@@ -97,15 +97,14 @@ export function formatArray(array: Array<string | number | ColumnItem | Array<st
9797
* 数组的所有一维子元素都不是array,说明是它是一个一维数组
9898
* 所以需要把一维的转成二维,这样方便统一处理
9999
*/
100-
if (!(array[0] instanceof Array)) {
100+
if (!isArray(array[0])) {
101101
tempArray = [tempArray as Array<string | number | ColumnItem>]
102102
}
103103
// 经过上述处理,都已经变成了二维数组,再把每一项二维元素包装成object
104104
const result: Array<Array<ColumnItem>> = (tempArray as Array<Array<string | number | ColumnItem>>).map((col) => {
105105
return col.map((row) => {
106-
const isObj = getType(row)
107106
/* 针对原始值,包装成{valueKey,labelKey} */
108-
if (isObj !== 'object') {
107+
if (!isObj(row)) {
109108
return {
110109
[valueKey]: row,
111110
[labelKey]: row

src/uni_modules/wot-design-uni/components/wd-picker/wd-picker.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default {
8686

8787
<script lang="ts" setup>
8888
import { getCurrentInstance, onBeforeMount, ref, watch, computed, onMounted, nextTick } from 'vue'
89-
import { deepClone, defaultDisplayFormat, getType, isArray, isDef } from '../common/util'
89+
import { deepClone, defaultDisplayFormat, getType, isArray, isDef, isFunction } from '../common/util'
9090
import { useCell } from '../composables/useCell'
9191
import { type ColumnItem, formatArray } from '../wd-picker-view/types'
9292
import { FORM_KEY, type FormItemRule } from '../wd-form/types'
@@ -112,14 +112,16 @@ const resetColumns = ref<Array<string | number | ColumnItem | Array<string | num
112112
const isPicking = ref<boolean>(false) // 判断pickview是否还在滑动中
113113
const hasConfirmed = ref<boolean>(false) // 判断用户是否点击了确认按钮
114114
115+
const emit = defineEmits(['confirm', 'open', 'cancel', 'update:modelValue'])
116+
115117
const isLoading = computed(() => {
116118
return props.loading || innerLoading.value
117119
})
118120
119121
watch(
120122
() => props.displayFormat,
121123
(fn) => {
122-
if (fn && getType(fn) !== 'function') {
124+
if (fn && !isFunction(fn)) {
123125
console.error('The type of displayFormat must be Function')
124126
}
125127
if (pickerViewWd.value && pickerViewWd.value.selectedIndex && pickerViewWd.value.selectedIndex.length !== 0) {
@@ -186,7 +188,7 @@ watch(
186188
watch(
187189
() => props.columnChange,
188190
(newValue) => {
189-
if (newValue && getType(newValue) !== 'function') {
191+
if (newValue && !isFunction(newValue)) {
190192
console.error('The type of columnChange must be Function')
191193
}
192194
},
@@ -223,8 +225,6 @@ const isRequired = computed(() => {
223225
224226
const { proxy } = getCurrentInstance() as any
225227
226-
const emit = defineEmits(['confirm', 'open', 'cancel', 'update:modelValue'])
227-
228228
onMounted(() => {
229229
isDef(props.modelValue) && props.modelValue !== '' && setShowValue(getSelects(props.modelValue)!)
230230
if (isDef(props.modelValue) && props.modelValue !== '' && pickerViewWd.value && pickerViewWd.value.getSelects) {
@@ -257,7 +257,7 @@ function getSelects(value: string | number | Array<string | number | Array<any>>
257257
* 2.根据formatColumns的长度截取Array<String>,保证下面的遍历不溢出
258258
* 3.根据每列的key值找到选项中value为此key的下标并记录
259259
*/
260-
value = value instanceof Array ? value : [value]
260+
value = isArray(value) ? value : [value]
261261
value = value.slice(0, formatColumns.length)
262262
263263
if (value.length === 0) {
@@ -319,7 +319,7 @@ function onConfirm() {
319319
}
320320
321321
const { beforeConfirm } = props
322-
if (beforeConfirm && getType(beforeConfirm) === 'function') {
322+
if (beforeConfirm && isFunction(beforeConfirm)) {
323323
beforeConfirm(
324324
pickerValue.value,
325325
(isPass: boolean) => {
@@ -363,7 +363,7 @@ function pickerViewChange({ value }: any) {
363363
*/
364364
function setShowValue(items: ColumnItem | ColumnItem[]) {
365365
// 避免值为空时调用自定义展示函数
366-
if ((items instanceof Array && !items.length) || !items) return
366+
if ((isArray(items) && !items.length) || !items) return
367367
368368
const { valueKey, labelKey } = props
369369
showValue.value = (props.displayFormat || defaultDisplayFormat)(items, { valueKey, labelKey })

0 commit comments

Comments
 (0)