Skip to content

Commit

Permalink
Fix #13853 - SelectButton: Add allowEmpty property
Browse files Browse the repository at this point in the history
  • Loading branch information
gucal committed Oct 11, 2023
1 parent 0baaacc commit c9869e3
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/app/components/selectbutton/selectbutton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class SelectButton implements ControlValueAccessor {
* @group Props
*/
@Input() multiple: boolean | undefined;
/**
* Whether selection can not be cleared.
* @group Props
*/
@Input() allowEmpty: boolean = true;
/**
* Inline style of the component.
* @group Props
Expand Down Expand Up @@ -174,26 +179,24 @@ export class SelectButton implements ControlValueAccessor {

const optionValue = this.getOptionValue(option);

let selected = this.isSelected(option);

if (selected && !this.allowEmpty) {
return;
}

if (this.multiple) {
if (this.isSelected(option)) this.removeOption(option);
else this.value = [...(this.value || []), optionValue];

this.onModelChange(this.value);

this.onChange.emit({
originalEvent: event,
value: this.value
});
} else if (this.value !== optionValue) {
this.value = optionValue;
this.onModelChange(this.value);

this.onChange.emit({
originalEvent: event,
value: this.value
});
} else {
this.value = selected ? null : optionValue;
}

this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
this.onOptionClick.emit({
originalEvent: event,
option: option,
Expand Down

0 comments on commit c9869e3

Please sign in to comment.