Skip to content

Commit 0e9a3e7

Browse files
committed
Select: Properly initialise selected options for select with multiple attribute
1 parent b5daca1 commit 0e9a3e7

File tree

6 files changed

+148
-112
lines changed

6 files changed

+148
-112
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.68.6 (2025-04-25)
2+
3+
### Bug fix
4+
5+
- **Select**: Properly initialise selected options for select with `multiple: true`
6+
17
# 2.68.5 (2025-04-22)
28

39
### Improvement

projects/demo/src/app/demo/pages/select-page/select-page.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ <h3>Dynamic options</h3>
102102
multiple
103103
placeholder="Select one or more options"
104104
[options]="multipleOptions"
105-
(valueChange)="multipleSelection = $event"
105+
[formControl]="multipleSelectControl"
106106
label="Multiple selection"></pa-select>
107-
<div style="flex: 1">Value: {{ multipleSelection }}</div>
107+
<div style="flex: 1">Value: {{ multipleSelectControl.getRawValue() }}</div>
108108
</div>
109109
</div>
110110
</div>

projects/demo/src/app/demo/pages/select-page/select-page.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { FormControl } from '@angular/forms';
23
import { OptionHeaderModel, OptionModel, OptionSeparator } from '@guillotinaweb/pastanaga-angular';
34

45
@Component({
@@ -10,6 +11,8 @@ export class SelectPageComponent {
1011
model = '';
1112
selectedTab = 'standalone';
1213

14+
multipleSelectControl = new FormControl<string>('user1,audio3');
15+
1316
optionsWithDescription: (OptionModel | OptionSeparator | OptionHeaderModel)[] = [
1417
new OptionModel({ id: 'desc1', label: 'Option 1', help: 'Description 1', value: 'desc1' }),
1518
new OptionModel({

projects/pastanaga-angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@guillotinaweb/pastanaga-angular",
33
"description": "Provides Pastanaga UI elements as Angular components",
4-
"version": "2.68.5",
4+
"version": "2.68.6",
55
"license": "MIT",
66
"keywords": [
77
"angular",

projects/pastanaga-angular/src/assets/glyphs-sprite.svg

Lines changed: 104 additions & 104 deletions
Loading

projects/pastanaga-angular/src/lib/controls/textfield/select/select.component.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ export class SelectComponent extends TextFieldDirective implements OnChanges, Af
108108
this._handleNgContent();
109109
this._checkDescribedBy();
110110
this.focusInput();
111-
this._updateDisplayedValue(this.control.value);
111+
if (this.multiple) {
112+
this._updateDisplayMultipleValues(this.control.value);
113+
this._initSelectedOptionsForMultipleValue();
114+
} else {
115+
this._updateDisplayedValue(this.control.value);
116+
}
117+
112118
// valueChanges may be triggered by an update value and validity...
113119
// we don't want to recompute the displayed option label in that case
114120
this.control.valueChanges.pipe(distinctUntilChanged(), takeUntil(this.terminator$)).subscribe((val) => {
@@ -234,6 +240,18 @@ export class SelectComponent extends TextFieldDirective implements OnChanges, Af
234240
detectChanges(this.cdr);
235241
}
236242

243+
private _initSelectedOptionsForMultipleValue() {
244+
const selectedValues = this.control.value.split(',');
245+
(this.dropdownOptions || []).forEach((dropdownOption) => {
246+
if (dropdownOption.type === ControlType.option) {
247+
const option = dropdownOption as OptionModel;
248+
if (selectedValues.includes(option.value)) {
249+
this.selectedOptions.push(option);
250+
}
251+
}
252+
});
253+
}
254+
237255
private _findLabelByValue(value?: string): string | undefined {
238256
let label: string | undefined;
239257

@@ -288,10 +306,19 @@ export class SelectComponent extends TextFieldDirective implements OnChanges, Af
288306
}
289307

290308
private _toggleSelectedOption(option: OptionComponent | OptionModel) {
291-
if (option.selected && option.value !== this.control.value) {
292-
option.selected = false;
293-
} else if (!option.selected && option.value === this.control.value) {
294-
option.selected = true;
309+
if (this.multiple) {
310+
const selectedValues = this.control.value.split(',');
311+
if (option.selected && !selectedValues.includes(option.value)) {
312+
option.selected = false;
313+
} else if (!option.selected && selectedValues.includes(option.value)) {
314+
option.selected = true;
315+
}
316+
} else {
317+
if (option.selected && option.value !== this.control.value) {
318+
option.selected = false;
319+
} else if (!option.selected && option.value === this.control.value) {
320+
option.selected = true;
321+
}
295322
}
296323
}
297324

0 commit comments

Comments
 (0)