Skip to content

Commit

Permalink
Merge pull request #13854 from primefaces/issue-13853
Browse files Browse the repository at this point in the history
Fix #13853 - SelectButton: Add allowEmpty property
  • Loading branch information
gucal authored Oct 11, 2023
2 parents 0baaacc + ef1622e commit a45bc1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 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
10 changes: 9 additions & 1 deletion src/app/showcase/doc/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@
"optional": true,
"readonly": false,
"type": "\"focus\" | \"hover\"",
"description": "Event to show tooltip."
"description": "Event to show the tooltip."
},
{
"name": "appendTo",
Expand Down Expand Up @@ -18592,6 +18592,14 @@
"default": "false",
"description": "When specified, allows selecting multiple values."
},
{
"name": "allowEmpty",
"optional": false,
"readonly": false,
"type": "boolean",
"default": "true",
"description": "Whether selection can not be cleared."
},
{
"name": "style",
"optional": false,
Expand Down

0 comments on commit a45bc1c

Please sign in to comment.