feat: 优化多选组件可以单选切换的逻辑

master
xjs 2025-04-23 09:49:43 +08:00
parent c1d895582c
commit 5dbdcc52c8
4 changed files with 14 additions and 8 deletions

View File

@ -68,7 +68,7 @@ const isDisabled = computed(() => {
return (
props.disabled ||
checkboxGroup.disabled.value ||
(max > 0 && !isChecked.value && selectedCount >= max)
(max > 1 && !isChecked.value && selectedCount >= max)
)
})

View File

@ -31,17 +31,20 @@ const innerValue = computed(() => props.modelValue)
const toggleOption = (option: { label: string; value: string | number }) => {
const currentValue = innerValue.value
const index = currentValue.indexOf(option.value)
const newValue = [...currentValue]
let newValue = [...currentValue]
if (index === -1) {
if (props.max && currentValue.length >= props.max) {
if (props.max === 1) {
newValue = [option.value]
} else if (props.max && currentValue.length >= props.max) {
uni.showToast({
title: `最多只能选择${props.max}`,
icon: 'none',
})
return
} else {
newValue.push(option.value)
}
newValue.push(option.value)
} else {
newValue.splice(index, 1)
}

View File

@ -68,7 +68,7 @@ const isDisabled = computed(() => {
return (
props.disabled ||
checkboxGroup.disabled.value ||
(max > 0 && !isChecked.value && selectedCount >= max)
(max > 1 && !isChecked.value && selectedCount >= max)
)
})

View File

@ -31,17 +31,20 @@ const innerValue = computed(() => props.modelValue)
const toggleOption = (option: { label: string; value: string | number }) => {
const currentValue = innerValue.value
const index = currentValue.indexOf(option.value)
const newValue = [...currentValue]
let newValue = [...currentValue]
if (index === -1) {
if (props.max && currentValue.length >= props.max) {
if (props.max === 1) {
newValue = [option.value]
} else if (props.max && currentValue.length >= props.max) {
uni.showToast({
title: `最多只能选择${props.max}`,
icon: 'none',
})
return
} else {
newValue.push(option.value)
}
newValue.push(option.value)
} else {
newValue.splice(index, 1)
}