make allowEmpty compatible with multiple on SelectButton

pull/7357/head
Jonathon 2025-03-03 15:38:01 +08:00
parent e53d48ed62
commit b88f80b0f5
1 changed files with 18 additions and 8 deletions

View File

@ -8,7 +8,7 @@
:disabled="disabled || isOptionDisabled(option)" :disabled="disabled || isOptionDisabled(option)"
:unstyled="unstyled" :unstyled="unstyled"
:size="size" :size="size"
:readonly="!allowEmpty && isSelected(option)" :readonly="isOptionReadonly(option)"
@change="onOptionSelect($event, option, index)" @change="onOptionSelect($event, option, index)"
:pt="ptm('pcToggleButton')" :pt="ptm('pcToggleButton')"
> >
@ -46,24 +46,34 @@ export default {
isOptionDisabled(option) { isOptionDisabled(option) {
return this.optionDisabled ? resolveFieldData(option, this.optionDisabled) : false; return this.optionDisabled ? resolveFieldData(option, this.optionDisabled) : false;
}, },
isOptionReadonly(option) {
if (this.allowEmpty) return false;
let selected = this.isSelected(option);
if (this.multiple) {
return selected && this.d_value.length === 1;
} else {
return selected;
}
},
onOptionSelect(event, option, index) { onOptionSelect(event, option, index) {
if (this.disabled || this.isOptionDisabled(option)) { if (this.disabled || this.isOptionDisabled(option) || this.isOptionReadonly(option)) {
return; return;
} }
let selected = this.isSelected(option); let selected = this.isSelected(option);
if (selected && !this.allowEmpty) {
return;
}
let optionValue = this.getOptionValue(option); let optionValue = this.getOptionValue(option);
let newValue; let newValue;
if (this.multiple) { if (this.multiple) {
if (selected) newValue = this.d_value.filter((val) => !equals(val, optionValue, this.equalityKey)); if (selected) {
else newValue = this.d_value ? [...this.d_value, optionValue] : [optionValue]; newValue = this.d_value.filter((val) => !equals(val, optionValue, this.equalityKey));
if (!this.allowEmpty && newValue.length === 0) return;
} else newValue = this.d_value ? [...this.d_value, optionValue] : [optionValue];
} else { } else {
if (selected && !this.allowEmpty) return;
newValue = selected ? null : optionValue; newValue = selected ? null : optionValue;
} }