Skip to content

Commit

Permalink
OnPush change detection for simplest components (#2169)
Browse files Browse the repository at this point in the history
* ⚡️ Use OnPush CD for comps with immutable inputs

* ⚡️ Add strategy to additional dumb components

* ✅  Migrate tests to use spectator

Directly setting the input on the component did not work with OnPush
change detection, so spectator is used in alert test now, while we
were at it. This way we can interact with the component via setInput().

* ✅  Remove unused provider from alert

* ⏪  Revert OnPush for tabs

State can be changed internally because 'tabBarBottomHidden' is public,
but not an input decorator - while still being used in the template.

* ✅  Clean up alert test

* ✅  Migrate toggle tests to use spectator

Directly setting the input on the component did not work with OnPush
change detection, so spectator is used in toggle test now, while we
were at it. This way we can interact with the component via setInput().

* ⏪  Revert to default change detection for card

* ⏪  Remove default strategy - it is the default

* ⏪ Remove unused import
  • Loading branch information
RasmusKjeldgaard authored Apr 26, 2022
1 parent 7d2088d commit 8a4a0a1
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 141 deletions.
12 changes: 10 additions & 2 deletions libs/designsystem/src/lib/components/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Component, ContentChild, ElementRef, AfterContentInit, ViewChild } from '@angular/core';
import {
AfterContentInit,
ChangeDetectionStrategy,
Component,
ContentChild,
ElementRef,
ViewChild,
} from '@angular/core';
import { IonApp } from '@ionic/angular';

import { RouterOutletComponent } from '../router-outlet/router-outlet.component';
import { ModalController } from '../modal/services/modal.controller';
import { RouterOutletComponent } from '../router-outlet/router-outlet.component';

@Component({
selector: 'kirby-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements AfterContentInit {
@ViewChild(IonApp, { static: true, read: ElementRef })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AfterContentInit,
ChangeDetectionStrategy,
Component,
ContentChild,
ElementRef,
Expand All @@ -22,6 +23,7 @@ export enum ButtonSize {
selector: 'button[kirby-button],Button[kirby-button]',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ButtonComponent implements AfterContentInit {
@HostBinding('class.attention-level1')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';

@Component({
selector: 'kirby-card-footer',
templateUrl: './card-footer.component.html',
styleUrls: ['./card-footer.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardFooterComponent implements OnInit {
constructor() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, HostBinding, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, HostBinding, Input } from '@angular/core';

import { NotificationColor } from '@kirbydesign/core';

Expand All @@ -8,6 +8,7 @@ export type CardFlagLevel = NotificationColor | 'info' | null;
selector: 'kirby-card-header',
templateUrl: './card-header.component.html',
styleUrls: ['./card-header.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardHeaderComponent {
@Input() title: string;
Expand Down
3 changes: 2 additions & 1 deletion libs/designsystem/src/lib/components/chip/chip.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, Input, HostBinding } from '@angular/core';
import { ChangeDetectionStrategy, Component, HostBinding, Input } from '@angular/core';

@Component({
selector: 'kirby-chip',
templateUrl: './chip.component.html',
styleUrls: ['./chip.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ChipComponent {
@Input() text: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

@Component({
selector: 'kirby-divider',
templateUrl: './divider.component.html',
styleUrls: ['./divider.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DividerComponent {
@Input()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { AfterContentInit, Component, ContentChildren, Input, QueryList } from '@angular/core';
import {
AfterContentInit,
ChangeDetectionStrategy,
Component,
ContentChildren,
Input,
QueryList,
} from '@angular/core';

import { ButtonComponent } from '../button/button.component';

@Component({
selector: 'kirby-empty-state',
templateUrl: './empty-state.component.html',
styleUrls: ['./empty-state.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EmptyStateComponent implements AfterContentInit {
@Input() iconName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DOCUMENT } from '@angular/common';
import {
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
Component,
ContentChild,
ElementRef,
Expand All @@ -21,6 +22,7 @@ import { ActionSheetComponent } from '../modal/action-sheet/action-sheet.compone
selector: 'kirby-fab-sheet',
templateUrl: './fab-sheet.component.html',
styleUrls: ['./fab-sheet.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FabSheetComponent implements AfterContentInit, AfterViewInit {
@Input() disabled: boolean = false;
Expand Down
10 changes: 9 additions & 1 deletion libs/designsystem/src/lib/components/icon/icon.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Component, HostBinding, Input, OnChanges, SimpleChanges } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
HostBinding,
Input,
OnChanges,
SimpleChanges,
} from '@angular/core';

import { IconRegistryService } from './icon-registry.service';
import { Icon } from './icon-settings';
Expand All @@ -17,6 +24,7 @@ export enum IconSize {
styleUrls: ['./icon.component.scss'],
// tslint:disable-next-line: no-host-metadata-property
host: { '[class.kirby-icon]': 'true' },
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IconComponent implements OnChanges {
defaultIcon: Icon = this.findIcon(kirbyIconSettings.icons, 'cog');
Expand Down
3 changes: 2 additions & 1 deletion libs/designsystem/src/lib/components/item/item.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, HostBinding, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, HostBinding, Input } from '@angular/core';

export enum ItemSize {
XS = 'xs',
Expand All @@ -10,6 +10,7 @@ export enum ItemSize {
selector: 'kirby-item',
templateUrl: './item.component.html',
styleUrls: ['./item.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ItemComponent {
@Input() disabled: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, HostListener, ViewChild } from '@angular/core';
import { ChangeDetectionStrategy, Component, HostListener, ViewChild } from '@angular/core';
import { IonList } from '@ionic/angular';

import { elementHasAncestor } from '../../../helpers/element-has-ancestor';
Expand All @@ -7,6 +7,7 @@ import { elementHasAncestor } from '../../../helpers/element-has-ancestor';
selector: 'kirby-list-experimental',
templateUrl: './list-experimental.component.html',
styleUrls: ['./list-experimental.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListExperimentalComponent {
@ViewChild(IonList, { static: true }) list: IonList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component } from '@angular/core';
import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
selector: 'kirby-list-header',
templateUrl: './list-header.component.html',
styleUrls: ['./list-header.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListHeaderComponent {
constructor() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Component, Input, OnInit } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';

@Component({
selector: 'kirby-list-section-header',
templateUrl: './list-section-header.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListSectionHeaderComponent implements OnInit {
@Input() title: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

@Component({
selector: 'kirby-loading-overlay',
templateUrl: './loading-overlay.component.html',
styleUrls: ['./loading-overlay.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LoadingOverlayComponent {
@Input() public isLoading = true;
Expand Down
Loading

0 comments on commit 8a4a0a1

Please sign in to comment.