-
Notifications
You must be signed in to change notification settings - Fork 1
Add tag component #30
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
base: develop
Are you sure you want to change the base?
Changes from all commits
3a1732f
31fb0df
2822efb
850c68a
3727970
2670725
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { render } from '@testing-library/angular'; | ||
| import { ESTagModule, ESTagComponent } from '..'; | ||
| import { Component } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| template: ` <es-tag>Tag</es-tag> ` | ||
| }) | ||
| class ESTagWrapperdComponent {} | ||
|
|
||
| describe('Tag', () => { | ||
| it('Should show content', async () => { | ||
| const component = await render(ESTagWrapperdComponent, { | ||
| imports: [ESTagModule] | ||
| }); | ||
|
|
||
| expect(component.getByText('Tag')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('Should show with default colors', async () => { | ||
| const component = await render(ESTagComponent, { | ||
| imports: [ESTagModule], | ||
| excludeComponentDeclaration: true | ||
| }); | ||
|
|
||
| expect(component.fixture.nativeElement.querySelector('.es-tag').style.backgroundColor).toBe( | ||
| 'rgb(0, 0, 0)' | ||
| ); | ||
| expect(component.fixture.nativeElement.querySelector('.es-tag__text').style.color).toBe( | ||
|
Contributor
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. У нас стоит расшрение https://github.com/testing-library/jest-dom и в нем помимо прочего есть |
||
| 'rgb(255, 255, 255)' | ||
| ); | ||
| }); | ||
|
|
||
| it('Should change colors', async () => { | ||
| const component = await render(ESTagComponent, { | ||
| imports: [ESTagModule], | ||
| excludeComponentDeclaration: true, | ||
| componentProperties: { | ||
| color: 'rgb(255, 255, 1)', | ||
| textColor: 'rgb(255, 255, 2)' | ||
| } | ||
| }); | ||
|
|
||
| expect(component.fixture.nativeElement.querySelector('.es-tag').style.backgroundColor).toBe( | ||
| 'rgb(255, 255, 1)' | ||
| ); | ||
| expect(component.fixture.nativeElement.querySelector('.es-tag__text').style.color).toBe( | ||
| 'rgb(255, 255, 2)' | ||
| ); | ||
| }); | ||
|
|
||
| it('Should show right icon', async () => { | ||
| const component = await render(ESTagComponent, { | ||
| imports: [ESTagModule], | ||
| excludeComponentDeclaration: true, | ||
| componentProperties: { | ||
| icon: 'info' | ||
| } | ||
| }); | ||
|
|
||
| expect(component.fixture.componentInstance.currentIcon).toEqual({ icon: 'info' }); | ||
|
|
||
| component.fixture.componentInstance.icon = null; | ||
| component.fixture.componentInstance.svgIcon = 'test'; | ||
| expect(component.fixture.componentInstance.currentIcon).toEqual({ svgIcon: 'test' }); | ||
|
|
||
| component.fixture.componentInstance.icon = 'info'; | ||
| component.fixture.componentInstance.svgIcon = 'test'; | ||
| expect(component.fixture.componentInstance.currentIcon).toEqual({ icon: 'info' }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { TAG_STORY_BASIC_SOURCE } from './tag-story-basic.source'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const TAG_STORY_BASIC_SOURCE = { | ||
| html: ` | ||
| <es-tag [color]="color" [textColor]="textColor" [icon]="icon">{{content}}</es-tag> | ||
| ` | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { TAG_STORY_SVG_ICON_SOURCE } from './tag-story-svg-icon.source'; | ||
| export { TagStorySvgIconModule } from './tag-story-svg-icon.module'; | ||
| export { TagStorySvgIconComponent } from './tag-story-svg-icon.component'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Component, ChangeDetectionStrategy, Input } from '@angular/core'; | ||
| import { MatIconRegistry } from '@angular/material/icon'; | ||
| import { DomSanitizer } from '@angular/platform-browser'; | ||
|
|
||
| @Component({ | ||
| selector: 'es-tag-story-svg-icon', | ||
| template: `<es-tag svgIcon="warning">{{ content }}</es-tag>`, | ||
| changeDetection: ChangeDetectionStrategy.OnPush | ||
| }) | ||
| export class TagStorySvgIconComponent { | ||
| @Input() | ||
| public content: string; | ||
|
|
||
| constructor(private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer) { | ||
| this.matIconRegistry.addSvgIcon( | ||
| 'warning', | ||
| this.domSanitizer.bypassSecurityTrustResourceUrl('/icons/alert/warning.svg') | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { NgModule } from '@angular/core'; | ||
| import { CommonModule } from '@angular/common'; | ||
| import { HttpClientModule } from '@angular/common/http'; | ||
| import { TagStorySvgIconComponent } from './tag-story-svg-icon.component'; | ||
| import { ESTagModule } from '../..'; | ||
|
|
||
| @NgModule({ | ||
| declarations: [TagStorySvgIconComponent], | ||
| imports: [CommonModule, HttpClientModule, ESTagModule], | ||
| exports: [TagStorySvgIconComponent] | ||
| }) | ||
| export class TagStorySvgIconModule {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const TAG_STORY_SVG_ICON_SOURCE = { | ||
| html: ` | ||
| <es-tag [color]="color" [textColor]="textColor" [svgIcon]="icon">{{content}}</es-tag> | ||
| ` | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { Meta, Story, ArgsTable } from '@storybook/addon-docs/blocks'; | ||
| import { Canvas } from '~storybook/components'; | ||
|
|
||
| import { action } from '@storybook/addon-actions'; | ||
|
|
||
| import { ESTagModule, ESTagComponent } from '..'; | ||
| import { TAG_STORY_BASIC_SOURCE } from './tag-story-basic'; | ||
| import { | ||
| TagStorySvgIconModule, | ||
| TagStorySvgIconComponent, | ||
| TAG_STORY_SVG_ICON_SOURCE | ||
| } from './tag-story-svg-icon'; | ||
|
|
||
| <Meta | ||
| title='UI/Tag' | ||
| args={{ | ||
| content: '', | ||
| icon: 'mail', | ||
| svgIcon: '', | ||
| color: '#000', | ||
| textColor: '#fff' | ||
| }} | ||
| /> | ||
|
|
||
| # Tag | ||
|
|
||
| A tags primary use is to place content in context. | ||
|
|
||
| ## Demos | ||
|
|
||
| export const getContent = (args, context) => { | ||
| return args.content || (context.globals.locale === 'en' ? 'Message' : 'Сообщение'); | ||
| }; | ||
|
|
||
| <Canvas source={TAG_STORY_BASIC_SOURCE}> | ||
| <Story name='Basic' height='100px'> | ||
| {((args, context) => ({ | ||
| template: `<es-tag [color]="color" [textColor]="textColor" [icon]="icon">{{content}}</es-tag>`, | ||
| moduleMetadata: { | ||
| imports: [ESTagModule] | ||
| }, | ||
| props: { | ||
| ...args, | ||
| content: getContent(args, context) | ||
| } | ||
| })).bind({})} | ||
| </Story> | ||
| </Canvas> | ||
|
|
||
| We can use individual icons. | ||
|
|
||
| <Canvas source={TAG_STORY_SVG_ICON_SOURCE}> | ||
| <Story | ||
| name='Svg icon' | ||
| height='100px' | ||
| argTypes={{ | ||
| color: { table: { disable: true } }, | ||
| icon: { table: { disable: true } }, | ||
| textColor: { table: { disable: true } }, | ||
| svgIcon: { table: { disable: true } } | ||
| }} | ||
| > | ||
| {((args, context) => ({ | ||
| component: TagStorySvgIconComponent, | ||
| moduleMetadata: { | ||
| imports: [TagStorySvgIconModule] | ||
| }, | ||
| props: { | ||
| ...args, | ||
| content: getContent(args, context) | ||
| } | ||
| })).bind({})} | ||
| </Story> | ||
| </Canvas> | ||
|
|
||
|
Contributor
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. Нужен пример с svg иконкой |
||
| ## API | ||
|
|
||
| <ArgsTable of={ESTagComponent} /> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './public-api'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "lib": { | ||
| "entryFile": "public-api.ts" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { ESTagComponent } from './tag.component'; | ||
| export { ESTagModule } from './tag.module'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <div class="es-tag" [ngStyle]="{ backgroundColor: color }"> | ||
| <div class="es-tag__content"> | ||
| <mat-icon | ||
| *ngIf="currentIcon" | ||
| class="es-tag__icon es-tag__icon_display" | ||
| [svgIcon]="currentIcon.svgIcon" | ||
| [ngStyle]="{ color: textColor }" | ||
| > | ||
| {{ currentIcon.icon || '' }} | ||
| </mat-icon> | ||
| <div class="es-tag__text mat-caption" [ngStyle]="{ color: textColor }"> | ||
| <ng-content></ng-content> | ||
| </div> | ||
| </div> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| .es-tag { | ||
| border-radius: 4px; | ||
| padding: 3px 8px; | ||
| width: fit-content; | ||
|
|
||
| &__content { | ||
| align-items: center; | ||
| display: flex; | ||
| } | ||
|
|
||
| &__text { | ||
| font-size: 12px; | ||
| line-height: 16px; | ||
| } | ||
|
|
||
| &__icon { | ||
| font-size: 14px; | ||
|
|
||
| &.mat-icon { | ||
| height: 14px; | ||
| line-height: 14px; | ||
| width: 14px; | ||
| } | ||
|
|
||
| &_display { | ||
|
Contributor
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. Модификатор применяется всегда, можно от него избавиться и просто в элементе стили сделать |
||
| margin-right: 5px; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { Component, ChangeDetectionStrategy, Input, ViewEncapsulation } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'es-tag', | ||
| templateUrl: './tag.component.html', | ||
| styleUrls: ['./tag.component.scss'], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| encapsulation: ViewEncapsulation.None | ||
| }) | ||
| export class ESTagComponent { | ||
| /** | ||
| * The background color of tag. | ||
| */ | ||
| @Input() | ||
| public color = '#000'; | ||
|
|
||
| /** | ||
| * The color of text. | ||
| */ | ||
| @Input() | ||
| public textColor = '#fff'; | ||
|
|
||
| /** | ||
| * The icon displayed before the text. | ||
| */ | ||
| @Input() | ||
| public icon?: string; | ||
|
|
||
| /** | ||
| * | ||
| * The svg icon displayed before the text. | ||
| */ | ||
| @Input() | ||
| public svgIcon?: string; | ||
|
|
||
| /** | ||
| * @internal | ||
| * @ignore | ||
| */ | ||
| public get currentIcon() { | ||
| if (this.icon) { | ||
| return { icon: this.icon }; | ||
| } | ||
| if (this.svgIcon) { | ||
| return { svgIcon: this.svgIcon }; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { NgModule } from '@angular/core'; | ||
| import { CommonModule } from '@angular/common'; | ||
| import { MatIconModule } from '@angular/material/icon'; | ||
| import { ESTagComponent } from './tag.component'; | ||
|
|
||
| @NgModule({ | ||
| imports: [CommonModule, MatIconModule], | ||
| declarations: [ESTagComponent], | ||
| exports: [ESTagComponent] | ||
| }) | ||
| export class ESTagModule {} |
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.
Лучше сделать
getByTestId, это правильнее с точки зрения идеологии этой либы.getByText>getByTestId>querySelector