Skip to content

Commit

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

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

Expand All @@ -18,7 +17,6 @@ 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-collection (activeIdChanged)="rootActiveId.set($event)">
<sk-list skListProvider (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-collection
<sk-list
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-collection
<sk-list
class="sk-grand-child-list"
sk-childs
(activeIdChanged)="grandChildActiveId.set($event)"
Expand All @@ -54,14 +54,14 @@
</sk-list-item>
}
</div>
</sk-list-collection>
</sk-list>
}
</sk-list-item>
}
</div>
</sk-list-collection>
</sk-list>
}
</sk-list-item>
}
</div>
</sk-list-collection>
</sk-list>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
ListCollectionComponent,
ListComponent,
ListItemActiveDirective,
ListItemComponent,
ListProviderDirective,
} from '@qupaya/sketch';
import { SAMPLE_DATA } from './list-sample.data';
import { query, transition, trigger } from '@angular/animations';
Expand All @@ -17,10 +17,10 @@ import { RouterOutlet } from '@angular/router';
imports: [
CommonModule,
ListItemActiveDirective,
ListCollectionComponent,
ListItemComponent,
ListComponent,
RouterOutlet,
ListProviderDirective,
],
templateUrl: './list-sample.component.html',
styleUrl: './list-sample.component.css',
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import {
untracked,
ViewEncapsulation,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListItemActiveDirective } from '../../directives/list-item-active.directive';
import { ListCollectionComponent } from '../list-collection/list-collection.component';
import { ListService } from '../../services/list.service';
import { ListComponent } from '../../list.component';

@Component({
selector: 'sk-list-item',
standalone: true,
imports: [CommonModule],
templateUrl: './list-item.component.html',
styleUrl: './list-item.component.css',
encapsulation: ViewEncapsulation.ShadowDom,
Expand All @@ -28,7 +26,7 @@ export class ListItemComponent {
optional: true,
skipSelf: true,
});
private readonly parentList = inject(ListCollectionComponent, {
private readonly parentList = inject(ListComponent, {
optional: true,
skipSelf: true,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
AfterViewInit,
Directive,
forwardRef,
inject,
OnInit,
viewChild,
} from '@angular/core';
import { ListService } from '../services/list.service';
import { ListComponent } from '../list.component';

@Directive({
selector: '[skListProvider]',
standalone: true,
providers: [ListService],
})
export class ListProviderDirective implements OnInit, AfterViewInit {
private readonly possibleParentList = inject(ListComponent, {
optional: true,
skipSelf: true,
});
private readonly listComponent = viewChild(forwardRef(() => ListComponent));

ngOnInit(): void {
if (this.possibleParentList) {
throw new Error(
'The ListProviderDirective must be set on root sk-list component.'
);
}
}

ngAfterViewInit(): void {
if (!this.listComponent) {
throw new Error(
'The ListProviderDirective must be set on a sk-list component.'
);
}
}
}
2 changes: 1 addition & 1 deletion libs/sketch/src/lib/components/list/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 './directives/list-provider.directive';
export * from './services/list.service';
export * from './list.component';
4 changes: 4 additions & 0 deletions libs/sketch/src/lib/components/list/list.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
display: flex;
flex-direction: column;
}
16 changes: 10 additions & 6 deletions libs/sketch/src/lib/components/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListService } from './services/list.service';
import { Component, output, signal } from '@angular/core';

@Component({
selector: 'sk-list',
standalone: true,
imports: [CommonModule],
providers: [ListService],
templateUrl: './list.component.html',
styleUrl: './list.component.css',
})
export class ListComponent {}
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);
}
}

0 comments on commit c9e22fd

Please sign in to comment.