forked from TWA-AFS-202310/ng-counter-group
-
Notifications
You must be signed in to change notification settings - Fork 5
homework-pengyu #6
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
Open
mechcodeman
wants to merge
10
commits into
TWA-AFS-202310-GROUP-3:main
Choose a base branch
from
mechcodeman:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
487cbe4
feat: create counter component and implement ++ and --
c54ed6b
test: add test for counter increase and decrease method
07feb87
feat: add counter-group component and cycle render counter
88ddddc
feat: implement feature can send counter number change from child to …
799ec35
feat: implement feature add button can add new counter component
f3fab1d
feat: implement button can reset all counter number
739e6b3
feat: implement reset button for each single counter
aeb6ff2
feat: implement add a button for each counter to remove it from page
45514de
test: add test for counter and counter-group components
6a3aaaa
fix: add css for the project
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| .home-page { | ||
| width: 600px; | ||
| height: 800px; | ||
| margin: auto; | ||
| border: 3px solid rgb(181, 175, 175); | ||
| border-radius: 10px; | ||
| } | ||
|
|
||
| .add-counter-button { | ||
| width: 580px; | ||
| height: 40px; | ||
| margin: 10px 10px 20px 10px; | ||
| background-color: #07bdb7; | ||
| border: transparent; | ||
| border-radius: 5px; | ||
| font: 16px bold; | ||
| } | ||
|
|
||
| .counter-group-region { | ||
| width: 580px; | ||
| margin: 0 10px 20px 10px; | ||
| } | ||
|
|
||
| .counter-box { | ||
| margin: 15px 0; | ||
| display: flex; | ||
| } | ||
|
|
||
| .counter-item { | ||
| margin-right: 40px; | ||
| } | ||
|
|
||
| .remove-button { | ||
| display: flex; | ||
| } | ||
|
|
||
| .remove-button:hover { | ||
| background-color: #db342b; | ||
| border: solid #db342b; | ||
| } | ||
|
|
||
| .sum-text { | ||
| margin: 0 10px 20px 10px; | ||
| text-align: center; | ||
| font-size: 20px; | ||
| } | ||
| .remove-counters-button { | ||
| width: 580px; | ||
| height: 40px; | ||
| margin: 10px 10px 20px 10px; | ||
| background-color: #db342b; | ||
| border: transparent; | ||
| border-radius: 5px; | ||
| font: 16px bold; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <div class="home-page"> | ||
| <button (click)="onAddCounter()" class="add-counter-button"> | ||
| Add counter | ||
| </button> | ||
| <div class="counter-group-region"> | ||
| <div *ngFor="let counter of counters; index as i" class="counter-box"> | ||
| <app-counter | ||
| [number]="counter.num" | ||
| (change)="counter.num = $event" | ||
| class="counter-item" | ||
| ></app-counter> | ||
| <button (click)="onRemoveCounter(i)" class="remove-button">Remove</button> | ||
| </div> | ||
| </div> | ||
| <div class="sum-text">Sum: {{ sum }}</div> | ||
| <button (click)="onResetNumber()" class="remove-counters-button">ResetAllNumber</button> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CounterGroupComponent } from './counter-group.component'; | ||
| import { CounterComponent } from '../counter/counter.component'; | ||
|
|
||
|
|
||
| describe('CounterGroupComponent', () => { | ||
| let component: CounterGroupComponent; | ||
| let fixture: ComponentFixture<CounterGroupComponent>; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CounterGroupComponent, CounterComponent], | ||
| }); | ||
| fixture = TestBed.createComponent(CounterGroupComponent); | ||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should reset all number when call OnResetNumber', () => { | ||
| component.counters = [{num: 1}, {num: 2}]; | ||
| component.onResetNumber(); | ||
| expect(component.counters).toEqual([{num: 0}, {num: 0}]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well: nice to verify counters data when test OnReset |
||
| }); | ||
|
|
||
| it('should increase sum when num from counter increase', () => { | ||
| component.counters = [{num: 1}, {num: 2}]; | ||
| component.counters[0].num++; | ||
| expect(component.sum).toEqual(4); | ||
| }) | ||
|
|
||
| it('should remove counter object from counters when call onRemoveCounter', () => { | ||
| component.counters = [{num: 1}, {num: 2}]; | ||
| component.onRemoveCounter(0); | ||
| expect(component.counters).toEqual([{num: 2}]); | ||
| }) | ||
|
|
||
| it('should add counter object to counters when call onAddCounter', () => { | ||
| component.counters = [{num: 1}, {num: 2}]; | ||
| component.onAddCounter(); | ||
| expect(component.counters).toEqual([{num: 1}, {num: 2}, {num: 0}]); | ||
| }) | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Component } from '@angular/core'; | ||
| import { CounterComponent } from '../counter/counter.component'; | ||
| @Component({ | ||
| selector: 'app-counter-group', | ||
| templateUrl: './counter-group.component.html', | ||
| styleUrls: ['./counter-group.component.css'] | ||
| }) | ||
| export class CounterGroupComponent { | ||
| counters: {num: number}[] = [ | ||
| {num: 1}, | ||
| {num: 2}, | ||
| {num: 3} | ||
| ]; | ||
|
|
||
| get sum() { | ||
| let sum_c = 0; | ||
| this.counters.forEach(counter => { | ||
| sum_c += counter.num | ||
| }) | ||
| return sum_c; | ||
| // return this.counters.reduce((result, current) => result + current.num, 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: remove dead code |
||
| } | ||
|
|
||
| onAddCounter() { | ||
| this.counters.push({num: 0}); | ||
| } | ||
|
|
||
| onResetNumber() { | ||
| this.counters.forEach(counter => { | ||
| counter.num = 0; | ||
| }) | ||
| } | ||
|
|
||
| onRemoveCounter(index: number) { | ||
| this.counters.splice(index, 1) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .add-button { | ||
| margin-right: 10px; | ||
| margin-left: 70px; | ||
| } | ||
|
|
||
| .decrease-button { | ||
| margin-left: 10px; | ||
| } | ||
|
|
||
| .button:hover { | ||
| background-color: rgb(151, 185, 255); | ||
| border: solid rgb(151, 185, 255); | ||
| } | ||
|
|
||
| .reset-button { | ||
| margin-left: 40px; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <div> | ||
| <button (click)="onIncreaseNumber()" class="button add-button">+</button> | ||
| <span>number: {{ number }}</span> | ||
| <button (click)="onDecreaseNumber()" class="button decrease-button">-</button> | ||
| <button (click)="onResetCurrentNumber()" class="button reset-button">ResetCurrentNumber</button> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CounterComponent } from './counter.component'; | ||
|
|
||
| describe('CounterComponent', () => { | ||
| let component: CounterComponent; | ||
| let fixture: ComponentFixture<CounterComponent>; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CounterComponent] | ||
| }); | ||
| fixture = TestBed.createComponent(CounterComponent); | ||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should increase number when call onIncreaseNumber', () => { | ||
| component.number = 2; | ||
| component.onIncreaseNumber(); | ||
| expect(component.number).toEqual(3); | ||
| }) | ||
|
|
||
| it('should decrease number when call onDecreaseNumber', () => { | ||
| component.number = 2; | ||
| component.onDecreaseNumber(); | ||
| expect(component.number).toEqual(1); | ||
| }) | ||
|
|
||
| it('should reset number when call onResetCurrentNumber', () => { | ||
| component.number = 2; | ||
| component.onResetCurrentNumber(); | ||
| expect(component.number).toEqual(0); | ||
| }) | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { Component, EventEmitter, Input, Output } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-counter', | ||
| templateUrl: './counter.component.html', | ||
| styleUrls: ['./counter.component.css'] | ||
| }) | ||
| export class CounterComponent { | ||
| @Input() number = 0; | ||
| @Output() change = new EventEmitter(); | ||
| onIncreaseNumber() { | ||
| this.number++; | ||
| this.change.emit(this.number); | ||
| } | ||
|
|
||
| onDecreaseNumber() { | ||
| this.number--; | ||
| this.change.emit(this.number); | ||
| } | ||
|
|
||
| onResetCurrentNumber() { | ||
| this.number = 0; | ||
| this.change.emit(this.number); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: this file can be removed