Skip to content

Commit

Permalink
refactor: use signals in chipped content component (#875)
Browse files Browse the repository at this point in the history
* refactor: use signals in chipped content component

* refactor: use signals for internal state
  • Loading branch information
davidlj95 authored Nov 27, 2024
1 parent aa587f6 commit babd22b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<div class="chips">
@for (content of contents; track content; let i = $index) {
@for (content of contents(); track content) {
<app-chip
[selected]="_isActive && _activeIndex === i"
(selectedChange)="onSelect(i)"
[selected]="isActive() && content === activeContent()"
(selectedChange)="select(content)"
>
{{ content.displayName }}
</app-chip>
}
</div>
<div
class="content"
[@contentDisplayed]="_isActive"
[@contentDisplayed]="isActive()"
(@contentDisplayed.done)="_scrollIntoView(contentElement)"
#contentElement
>
<ng-container
[ngComponentOutlet]="contents[_activeIndex].component"
[ngComponentOutletInputs]="contents[_activeIndex].inputs"
[ngComponentOutlet]="activeContent().component"
[ngComponentOutletInputs]="activeContent().inputs"
></ng-container>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ describe('ChippedContentComponent', () => {

beforeEach(() => {
;[fixture, component] = makeSut()
component.contents = CONTENTS
fixture.detectChanges()
})

it('should create', () => {
Expand Down Expand Up @@ -210,6 +208,7 @@ function makeSut() {
MockProvider(SCROLL_INTO_VIEW, jasmine.createSpy()),
],
})
component.contents = CONTENTS
fixture.componentRef.setInput('contents', CONTENTS)
fixture.detectChanges()
return [fixture, component] as const
}
21 changes: 11 additions & 10 deletions src/app/resume-page/chipped-content/chipped-content.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Inject, Input } from '@angular/core'
import { Component, Inject, input, linkedSignal, signal } from '@angular/core'
import { ChipComponent } from '../chip/chip.component'
import { NgComponentOutlet } from '@angular/common'
import { ChippedContent } from './chipped-content'
Expand Down Expand Up @@ -41,21 +41,22 @@ import { SCROLL_INTO_VIEW, ScrollIntoView } from '@/common/scroll-into-view'
],
})
export class ChippedContentComponent {
@Input() contents!: readonly ChippedContent[]
readonly contents = input.required<readonly ChippedContent[]>()
readonly activeContent = linkedSignal<ChippedContent>(
() => this.contents()[0],
)
readonly isActive = signal<boolean>(false)

constructor(
@Inject(SCROLL_INTO_VIEW) protected _scrollIntoView: ScrollIntoView,
) {}

protected _isActive = false
protected _activeIndex = 0

onSelect(index: number) {
if (this._activeIndex == index) {
this._isActive = !this._isActive
select(content: ChippedContent) {
if (this.activeContent() === content) {
this.isActive.update((isActive) => !isActive)
return
}
this._isActive = true
this._activeIndex = index
this.isActive.set(true)
this.activeContent.set(content)
}
}

0 comments on commit babd22b

Please sign in to comment.