Skip to content

Commit 344d5b6

Browse files
authored
fix: support disabling options in multi-select (#979)
* fix: multi select - support disabled options
1 parent 4fbcc37 commit 344d5b6

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

projects/components/src/multi-select/multi-select.component.scss

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
flex-direction: column;
9393

9494
.divider {
95-
padding: 0px 16px 4px 16px;
95+
padding: 0 16px 4px 16px;
9696
}
9797

9898
.multi-select-options {
@@ -107,8 +107,13 @@
107107
font-size: 15px;
108108
align-items: center;
109109

110+
&.disabled {
111+
color: $gray-5;
112+
cursor: not-allowed;
113+
}
114+
110115
.checkbox {
111-
margin: 0px;
116+
margin: 0;
112117
color: $gray-3;
113118

114119
::ng-deep .mat-ripple {

projects/components/src/multi-select/multi-select.component.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// tslint:disable: max-file-line-count
2+
13
import { CommonModule } from '@angular/common';
24
import { fakeAsync, flush } from '@angular/core/testing';
35
import { IconLibraryTestingModule, IconType } from '@hypertrace/assets-library';
@@ -476,4 +478,44 @@ describe('Multi Select Component', () => {
476478
expect(spectator.query('.select-all', { root: true })).toExist();
477479
flush();
478480
}));
481+
482+
test('should disable select options as expected', fakeAsync(() => {
483+
const onChange = jest.fn();
484+
485+
spectator = hostFactory(
486+
`
487+
<ht-multi-select (selectedChange)="onChange($event)">
488+
<ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value" [disabled]="option.disabled"></ht-select-option>
489+
</ht-multi-select>`,
490+
{
491+
hostProps: {
492+
options: [
493+
{ label: 'first', value: 'first-value' },
494+
{ label: 'second', value: 'second-value', disabled: true },
495+
{
496+
label: 'third',
497+
value: 'third-value',
498+
selectedLabel: 'Third Value!!!',
499+
icon: 'test-icon',
500+
iconColor: 'red'
501+
}
502+
],
503+
onChange: onChange
504+
}
505+
}
506+
);
507+
508+
spectator.tick();
509+
spectator.click('.trigger-content');
510+
511+
const optionElements = spectator.queryAll('.multi-select-option', { root: true });
512+
expect(optionElements.length).toBe(3);
513+
expect(optionElements[0]).not.toHaveClass('disabled');
514+
expect(optionElements[1]).toHaveClass('disabled');
515+
expect(optionElements[2]).not.toHaveClass('disabled');
516+
spectator.click(optionElements[1]);
517+
518+
expect(onChange).not.toHaveBeenCalled();
519+
flush();
520+
}));
479521
});

projects/components/src/multi-select/multi-select.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ import { MultiSelectJustify } from './multi-select-justify';
9494
*ngFor="let item of filteredOptions"
9595
(click)="this.onSelectionChange(item)"
9696
class="multi-select-option"
97+
[ngClass]="{ disabled: item.disabled }"
9798
>
9899
<ht-checkbox
99100
class="checkbox"
100101
(click)="this.preventClickDefault($event)"
101102
[checked]="this.isSelectedItem(item)"
103+
[disabled]="item.disabled"
102104
></ht-checkbox>
103105
<ht-icon
104106
class="icon"
@@ -207,6 +209,10 @@ export class MultiSelectComponent<V> implements AfterContentInit, OnChanges {
207209
}
208210

209211
public onSelectionChange(item: SelectOptionComponent<V>): void {
212+
if (item.disabled) {
213+
return;
214+
}
215+
210216
const selected = this.isSelectedItem(item)
211217
? this.selected?.filter(value => value !== item.value)
212218
: (this.selected ?? []).concat(item.value);

0 commit comments

Comments
 (0)