Skip to content

Commit 496cb73

Browse files
TLMoonofweisheng
authored andcommitted
fix(picker): clear selected value and options when columns is emptied
- Fixed issue where selected value and picker options were not cleared when columns became empty - Updated wd-picker to reset pickerValue and showValue when columns is empty - Updated wd-picker-view to clear formatColumns and selectedIndex when columns is empty - Enhanced selectWithValue to handle empty columns case - Resolves #935 Closes: #935
1 parent 1ac115f commit 496cb73

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ const selectedIndex = ref<Array<number>>([]) // 格式化之后,每列选中
5858
watch(
5959
[() => props.modelValue, () => props.columns],
6060
(newValue, oldValue) => {
61-
if (!isEqual(oldValue[1], newValue[1]) && isArray(newValue[1]) && newValue[1].length > 0) {
62-
formatColumns.value = formatArray(newValue[1], props.valueKey, props.labelKey)
61+
if (!isEqual(oldValue[1], newValue[1])) {
62+
if (isArray(newValue[1]) && newValue[1].length > 0) {
63+
formatColumns.value = formatArray(newValue[1], props.valueKey, props.labelKey)
64+
} else {
65+
// 当 columns 变为空时,清空 formatColumns 和 selectedIndex
66+
formatColumns.value = []
67+
selectedIndex.value = []
68+
}
6369
}
6470
if (isDef(newValue[0])) {
6571
selectWithValue(newValue[0])
@@ -79,7 +85,10 @@ const { proxy } = getCurrentInstance() as any
7985
* @param {String|Number|Boolean|Array<String|Number|Boolean|Array<any>>}value
8086
*/
8187
function selectWithValue(value: string | number | boolean | number[] | string[] | boolean[]) {
82-
if (formatColumns.value.length === 0) return
88+
if (formatColumns.value.length === 0) {
89+
selectedIndex.value = [] // 如果列为空,直接清空选中索引
90+
return
91+
}
8392
// 使其默认选中首项
8493
if (value === '' || !isDef(value) || (isArray(value) && value.length === 0)) {
8594
value = formatColumns.value.map((col) => {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,14 @@ watch(
152152
(newValue) => {
153153
displayColumns.value = deepClone(newValue)
154154
resetColumns.value = deepClone(newValue)
155-
// 获取初始选中项,并展示初始选中文案
156-
handleShowValueUpdate(props.modelValue)
155+
if (newValue.length === 0) {
156+
// 当 columns 变为空时,清空 pickerValue 和 showValue
157+
pickerValue.value = isArray(props.modelValue) ? [] : ''
158+
showValue.value = ''
159+
} else {
160+
// 非空时正常更新显示值
161+
handleShowValueUpdate(props.modelValue)
162+
}
157163
},
158164
{
159165
deep: true,

0 commit comments

Comments
 (0)