Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for setting selectedIndex in segmentSelect call-back #2407

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class SegmentedControlShowcaseComponent {
{
name: 'selectedIndex',
description:
'Gets/sets the index of the selected segment within the `segmentItems` array. The value -1 indicates no element is selected.',
defaultValue: 'undefined',
'The index of the segment selected within the provided `Items` array. The value -1 means no element selected. Define this parameter as two-way-binding when used for controlling the selection',
defaultValue: '-1',
type: ['number'],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator';
import { createHostFactory, Spectator, SpectatorHost } from '@ngneat/spectator';
import { MockComponents, MockDirective } from 'ng-mocks';

import { DesignTokenHelper } from '@kirbydesign/core';
Expand All @@ -17,7 +18,6 @@ const fatFingerSize = DesignTokenHelper.fatFingerSize;

describe('SegmentedControlComponent', () => {
let component: SegmentedControlComponent;
let onSegmentSelectSpy: jasmine.Spy;
let items: SegmentItem[] = [
{
text: 'First item',
Expand Down Expand Up @@ -59,7 +59,6 @@ describe('SegmentedControlComponent', () => {
}
);
component = spectator.component;
onSegmentSelectSpy = spyOn(component.segmentSelect, 'emit');
});

it('should create', () => {
Expand Down Expand Up @@ -114,6 +113,7 @@ describe('SegmentedControlComponent', () => {

describe('when updating items', () => {
it('should not emit segmentSelect event', async () => {
const onSegmentSelectSpy = spyOn(component.segmentSelect, 'emit');
const ionSegmentElement = spectator.queryHost<HTMLIonSegmentElement>('ion-segment');
await TestHelper.whenReady(ionSegmentElement);

Expand Down Expand Up @@ -164,6 +164,32 @@ describe('SegmentedControlComponent', () => {

expect(component.value).toBe(items[2]);
});

it('should set the correct value when changing the selected-index', () => {
spectator.setInput('selectedIndex', 2);

expect(component.value).toBe(items[2]);
});

it('should invoke the selected-index-change when changing the selected-index', () => {
const subscriber = jasmine.createSpy('subcriber');
spectator.output('selectedIndexChange').subscribe(subscriber);

spectator.setInput('selectedIndex', 2);

expect(subscriber).toHaveBeenCalledTimes(1);
});

it('should set the correct value when changing the selected-index in segment-select call-back', fakeAsync(() => {
spectator
.output('segmentSelect')
.subscribe((value) => spectator.setInput('selectedIndex', 2));

spectator.dispatchMouseEvent('kirby-chip:first-of-type', 'click');
tick();

expect(component.value).toBe(items[2]);
}));
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ export class SegmentedControlComponent {
return this._selectedIndex;
}

@Input() set selectedIndex(value: number) {
if (value !== this._selectedIndex) {
this._selectedIndex = value;
@Input() set selectedIndex(index: number) {
if (index !== this._selectedIndex) {
this._selectedIndex = index;
this._value = this.items[this.selectedIndex];
this.selectedIndexChange.emit(this.selectedIndex);
}
}

@Output() selectedIndexChange = new EventEmitter<number>();
Copy link
Collaborator

@RasmusKjeldgaard RasmusKjeldgaard Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you briefly explain why this new output is needed? And it should probably be documented in the API table of the cookbook page too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah wow, I completely forgot about the fact that this is the standard pattern for creating two way bindings, I interpreted it as a completely new output on its own. Angular magic ✨


private _value: SegmentItem;
get value(): SegmentItem {
return this._value;
Expand Down Expand Up @@ -102,9 +105,12 @@ export class SegmentedControlComponent {

onSegmentSelect(selectedId: string) {
const selectedItemIndex = this.items.findIndex((item) => selectedId === item.id);

if (selectedItemIndex !== this.selectedIndex) {
this.selectedIndex = selectedItemIndex;
this.segmentSelect.emit(this.value);
setTimeout(() => {
this.segmentSelect.emit(this.value);
});
}
}
}