From d6efc85b8ed88bd7b99025662b5220567d9551cc Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Oct 2019 14:18:07 +0300 Subject: [PATCH] feat(theme-shared): add new inputs to button component --- .../lib/components/button/button.component.ts | 41 +++++++++++++++++-- .../src/lib/tests/button.component.spec.ts | 39 +++++++++++------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/button/button.component.ts b/npm/ng-packs/packages/theme-shared/src/lib/components/button/button.component.ts index 59c6b3c13d..99d58d3124 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/button/button.component.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/button/button.component.ts @@ -1,14 +1,24 @@ -import { Component, Input } from '@angular/core'; +import { Component, EventEmitter, Input, Output, ViewChild, ElementRef, Renderer2, OnInit } from '@angular/core'; +import { ABP } from '@abp/ng.core'; @Component({ selector: 'abp-button', + // tslint:disable-next-line: component-max-inline-declarations template: ` - `, }) -export class ButtonComponent { +export class ButtonComponent implements OnInit { @Input() buttonClass = 'btn btn-primary'; @@ -24,6 +34,21 @@ export class ButtonComponent { @Input() disabled = false; + @Input() + attributes: ABP.Dictionary; + + // tslint:disable-next-line: no-output-native + @Output() readonly click = new EventEmitter(); + + // tslint:disable-next-line: no-output-native + @Output() readonly focus = new EventEmitter(); + + // tslint:disable-next-line: no-output-native + @Output() readonly blur = new EventEmitter(); + + @ViewChild('button', { static: true }) + buttonRef: ElementRef; + /** * @deprecated Use buttonType instead. To be deleted in v1 */ @@ -32,4 +57,14 @@ export class ButtonComponent { get icon(): string { return `${this.loading ? 'fa fa-pulse fa-spinner' : this.iconClass || 'd-none'}`; } + + constructor(private renderer: Renderer2) {} + + ngOnInit() { + if (this.attributes) { + Object.keys(this.attributes).forEach(key => { + this.renderer.setAttribute(this.buttonRef.nativeElement, key, this.attributes[key]); + }); + } + } } diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/button.component.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/button.component.spec.ts index 59d49e68c4..c272b665e7 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/button.component.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/button.component.spec.ts @@ -2,46 +2,55 @@ import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest'; import { ButtonComponent } from '../components'; describe('ButtonComponent', () => { - let host: SpectatorHost; + let spectator: SpectatorHost; const createHost = createHostFactory(ButtonComponent); - beforeEach(() => (host = createHost('Button'))); + beforeEach( + () => + (spectator = createHost('Button', { + hostProps: { attributes: { autofocus: '', name: 'abp-button' } }, + })), + ); it('should display the button', () => { - expect(host.query('button')).toBeTruthy(); + expect(spectator.query('button')).toBeTruthy(); }); it('should equal the default classes to btn btn-primary', () => { - expect(host.query('button')).toHaveClass('btn btn-primary'); + expect(spectator.query('button')).toHaveClass('btn btn-primary'); }); it('should equal the default type to button', () => { - expect(host.query('button')).toHaveAttribute('type', 'button'); + expect(spectator.query('button')).toHaveAttribute('type', 'button'); }); it('should enabled', () => { - expect(host.query('[disabled]')).toBeFalsy(); + expect(spectator.query('[disabled]')).toBeFalsy(); }); it('should have the text content', () => { - expect(host.query('button')).toHaveText('Button'); + expect(spectator.query('button')).toHaveText('Button'); }); it('should display the icon', () => { - expect(host.query('i.d-none')).toBeFalsy(); - expect(host.query('i')).toHaveClass('fa'); + expect(spectator.query('i.d-none')).toBeFalsy(); + expect(spectator.query('i')).toHaveClass('fa'); }); it('should display the spinner icon', () => { - host.component.loading = true; - host.detectComponentChanges(); - expect(host.query('i')).toHaveClass('fa-spinner'); + spectator.component.loading = true; + spectator.detectComponentChanges(); + expect(spectator.query('i')).toHaveClass('fa-spinner'); }); it('should disabled when the loading input is true', () => { - host.component.loading = true; - host.detectComponentChanges(); - expect(host.query('[disabled]')).toBeDefined(); + spectator.component.loading = true; + spectator.detectComponentChanges(); + expect(spectator.query('[disabled]')).toBeTruthy(); + }); + + it('should disabled when the loading input is true', () => { + expect(spectator.query('[autofocus][name="abp-button"]')).toBeTruthy(); }); });