Skip to content

Commit 8a9881a

Browse files
palbizuPatricio Albizu
andauthored
fix: clicking on checkbox does not select the option (#769)
* fix: clicking on checkbox does not select the option * fix: linterning code * fix: changing function name * fix: adding UT * fix: fixing UT * fix: fixing ut * fix: adding details Co-authored-by: Patricio Albizu <[email protected]>
1 parent dcb23ca commit 8a9881a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ describe('Multi Select Component', () => {
138138
});
139139
}));
140140

141+
test('should block prevent default when checkbox is clicked', fakeAsync(() => {
142+
const onChange = jest.fn();
143+
spectator = hostFactory(
144+
`
145+
<ht-multi-select (selectedChange)="onChange($event)" [selected]="selected">
146+
<ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value">
147+
</ht-select-option>
148+
</ht-multi-select>`,
149+
{
150+
hostProps: {
151+
options: selectionOptions,
152+
selected: [],
153+
onChange: onChange
154+
}
155+
}
156+
);
157+
158+
spectator.tick();
159+
spectator.click('.trigger-content');
160+
const selectedCheckboxElement = spectator.queryAll('ht-checkbox', { root: true })[0];
161+
expect(spectator.dispatchFakeEvent(selectedCheckboxElement, 'click', true).defaultPrevented).toBe(true);
162+
163+
expect(onChange).toHaveBeenCalledTimes(1);
164+
expect(onChange).toHaveBeenCalledWith([selectionOptions[0].value]);
165+
expect(spectator.query(LabelComponent)?.label).toEqual('first');
166+
flush();
167+
}));
168+
141169
test('should notify and update selection when selection is changed', fakeAsync(() => {
142170
const onChange = jest.fn();
143171

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ import { MultiSelectJustify } from './multi-select-justify';
9292
(click)="this.onSelectionChange(item)"
9393
class="multi-select-option"
9494
>
95-
<ht-checkbox class="checkbox" [checked]="this.isSelectedItem(item)"></ht-checkbox>
95+
<ht-checkbox
96+
class="checkbox"
97+
(click)="this.preventClickDefault($event)"
98+
[checked]="this.isSelectedItem(item)"
99+
></ht-checkbox>
96100
<ht-icon
97101
class="icon"
98102
*ngIf="item.icon"
@@ -210,6 +214,10 @@ export class MultiSelectComponent<V> implements AfterContentInit, OnChanges {
210214
return this.selected !== undefined && this.selected.filter(value => value === item.value).length > 0;
211215
}
212216

217+
public preventClickDefault(event: Event): void {
218+
event.preventDefault();
219+
}
220+
213221
private setSelection(selected: V[]): void {
214222
this.selected = selected;
215223
this.setTriggerLabel();

0 commit comments

Comments
 (0)