Merge pull request #7357 from Winter979/fix-select-button-allow-empty-with-multiple

make allowEmpty compatible with multiple on SelectButton
pull/7365/head^2^2
Tuğçe Küçükoğlu 2025-03-04 14:41:41 +03:00 committed by GitHub
commit 06812556e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 8 deletions

View File

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