Skip to content

Commit

Permalink
feat(list): change the provider for list service
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjahr committed Apr 16, 2024
1 parent 63fc4da commit 54b129b
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 30 deletions.
2 changes: 2 additions & 0 deletions apps/demo-app/src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Route } from '@angular/router';
import { ListService } from '@qupaya/sketch';

const UUID_REGEX = /^[a-z,0-9,-]{36,36}$/;

Expand All @@ -17,6 +18,7 @@ export const appRoutes: Route[] = [
},
{
path: 'list-sample',
providers: [ListService],
children: [
{
path: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<sk-list (activeIdChanged)="rootActiveId.set($event)">
<sk-list-collection (activeIdChanged)="rootActiveId.set($event)">
<div class="sk-list" @skListAnimation>
@for (rootItem of items(); track rootItem.link) {
<sk-list-item [skListItemId]="rootItem.link" [skEnableRouting]="true">
Expand All @@ -10,7 +10,7 @@
<span> {{ rootItem.label }} </span>
</div>
@if (rootItem.children?.length && rootActiveId() === rootItem.link) {
<sk-list
<sk-list-collection
class="sk-child-list"
sk-childs
(activeIdChanged)="childActiveId.set($event)"
Expand All @@ -30,7 +30,7 @@
</div>
@if (childItem.children?.length && childActiveId() ===
childItem.link) {
<sk-list
<sk-list-collection
class="sk-grand-child-list"
sk-childs
(activeIdChanged)="grandChildActiveId.set($event)"
Expand All @@ -54,14 +54,14 @@
</sk-list-item>
}
</div>
</sk-list>
</sk-list-collection>
}
</sk-list-item>
}
</div>
</sk-list>
</sk-list-collection>
}
</sk-list-item>
}
</div>
</sk-list>
</sk-list-collection>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ListSampleComponent } from './list-sample.component';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
import { ListService } from '@qupaya/sketch';

describe('ListSampleComponent', () => {
let component: ListSampleComponent;
Expand All @@ -14,6 +15,7 @@ describe('ListSampleComponent', () => {
NoopAnimationsModule,
RouterModule.forRoot([]),
],
providers: [ListService],
}).compileComponents();

fixture = TestBed.createComponent(ListSampleComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
ListCollectionComponent,
ListComponent,
ListItemActiveDirective,
ListItemComponent,
Expand All @@ -16,8 +17,9 @@ import { RouterOutlet } from '@angular/router';
imports: [
CommonModule,
ListItemActiveDirective,
ListComponent,
ListCollectionComponent,
ListItemComponent,
ListComponent,
RouterOutlet,
],
templateUrl: './list-sample.component.html',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
display: flex;
flex-direction: column;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ng-content></ng-content>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ListCollectionComponent } from './list-collection.component';

describe('ListComponent', () => {
let component: ListCollectionComponent;
let fixture: ComponentFixture<ListCollectionComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ListCollectionComponent],
}).compileComponents();

fixture = TestBed.createComponent(ListCollectionComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component, output, signal, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
selector: 'sk-list-collection',
standalone: true,
imports: [CommonModule, ListItemComponent],
templateUrl: './list-collection.component.html',
styleUrl: './list-collection.component.css',
encapsulation: ViewEncapsulation.ShadowDom,
})
export class ListCollectionComponent {
private readonly activeItemId = signal<string | undefined>(undefined);
activeIdChanged = output<string>();

activateItem(id: string): void {
this.activeItemId.update(() => id);
this.activeIdChanged.emit(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ListItemComponent } from './list-item.component';
import { ListService } from '@qupaya/sketch';

describe('ListItemComponent', () => {
let component: ListItemComponent;
Expand All @@ -8,6 +9,7 @@ describe('ListItemComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ListItemComponent],
providers: [ListService],
}).compileComponents();

fixture = TestBed.createComponent(ListItemComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListItemActiveDirective } from '../../directives/list-item-active.directive';
import { ListComponent } from '../../list.component';
import { ListCollectionComponent } from '../list-collection/list-collection.component';
import { ListService } from '../../services/list.service';

@Component({
Expand All @@ -28,7 +28,7 @@ export class ListItemComponent {
optional: true,
skipSelf: true,
});
private readonly parentList = inject(ListComponent, {
private readonly parentList = inject(ListCollectionComponent, {
optional: true,
skipSelf: true,
});
Expand Down
3 changes: 2 additions & 1 deletion libs/sketch/src/lib/components/list/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './list.component';
export * from './components/list-collection/list-collection.component';
export * from './components/list-item/list-item.component';
export * from './directives/list-item-active.directive';
export * from './services/list.service';
export * from './list.component';
4 changes: 0 additions & 4 deletions libs/sketch/src/lib/components/list/list.component.css
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
:host {
display: flex;
flex-direction: column;
}
18 changes: 5 additions & 13 deletions libs/sketch/src/lib/components/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { Component, output, signal, ViewEncapsulation } from '@angular/core';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListItemComponent } from './components/list-item/list-item.component';
import { ListService } from './services/list.service';

@Component({
selector: 'sk-list',
standalone: true,
imports: [CommonModule, ListItemComponent],
imports: [CommonModule],
providers: [ListService],
templateUrl: './list.component.html',
styleUrl: './list.component.css',
encapsulation: ViewEncapsulation.ShadowDom,
})
export class ListComponent {
private readonly activeItemId = signal<string | undefined>(undefined);
activeIdChanged = output<string>();

activateItem(id: string): void {
this.activeItemId.update(() => id);
this.activeIdChanged.emit(id);
}
}
export class ListComponent {}
4 changes: 1 addition & 3 deletions libs/sketch/src/lib/components/list/services/list.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Injectable, signal } from '@angular/core';

@Injectable({
providedIn: 'root',
})
@Injectable()
export class ListService {
readonly items = signal<{ id: string; parentId?: string; active: boolean }[]>(
[]
Expand Down

0 comments on commit 54b129b

Please sign in to comment.