Skip to content

Commit

Permalink
feat(lib): add popper API demo
Browse files Browse the repository at this point in the history
  • Loading branch information
geromegrignon committed Aug 27, 2020
1 parent 18a8334 commit 76cbb13
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 8 deletions.
6 changes: 3 additions & 3 deletions projects/ngneat/helipopper/src/lib/helipopper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export class HelipopperService {
directive.helipopperClass = options?.helipopperClass;
directive.helipopperOffset = options?.helipopperOffset;
directive.injector = options?.helipopperInjector;
directive.placement = options?.helipopperPlacement;
directive.variation = options?.helipopperVariation;
directive.disabled = options?.helipopperDisabled;
directive.placement = options?.helipopperPlacement || 'top';
directive.variation = options?.helipopperVariation || 'tooltip';
directive.disabled = options?.helipopperDisabled || false;
directive.sticky = options?.helipopperSticky;

directive.whenStable.subscribe(() => directive.show());
Expand Down
24 changes: 20 additions & 4 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,23 @@ <h2>Custom Component</h2>
<button [helipopper]="comp" helipopperVariation="popper" (helipopperClose)="close()">Open component</button>
</div>

<fieldset>
<legend>Enter your name</legend>
<input #inputName type="text" [formControl]="formControl" (keyup.enter)="submit()" />
</fieldset>
<div class="block">
<h2>Attach the popper manually</h2>
<fieldset>
<legend>Enter your name</legend>
<input #inputName type="text" [formControl]="formControl" (keyup.enter)="submit()" />
</fieldset>
</div>

<div class="block">
<h2>Attach the popper manually (with provided component)</h2>
<fieldset>
<legend>Enter your name</legend>
<input #inputNameComp type="text" [formControl]="formControlWithComp" (keyup.enter)="submitWithComp()" />
</fieldset>
</div>

<div class="block">

This comment has been minimized.

Copy link
@NetanelBasal

NetanelBasal Aug 27, 2020

Member

Sorry for the confusion. I meant, popper that in its template opens another popper - nested poppers.

<h2>Attach the popper manually (with provided component with nested popper)</h2>
<app-nested-popper (submit)="submitWithNestedComp($event)"></app-nested-popper>
</div>
28 changes: 28 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ExampleComponent } from './example/example.component';
import { interval } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { HelipopperDirective, HelipopperService } from '@ngneat/helipopper';
import { NestedPopperComponent } from './nested-popper/nested-popper.component';

@Component({
selector: 'app-root',
Expand Down Expand Up @@ -49,8 +50,12 @@ export class AppComponent implements AfterViewInit {
text = `Long Long All Text`;
isSticky = false;
comp = ExampleComponent;
nestedPopperComp = NestedPopperComponent;
formControl = new FormControl();
formControlWithComp = new FormControl();
popper: HelipopperDirective;
popperWithComp: HelipopperDirective;
popperWithNestedComp: HelipopperDirective;

changeContent() {
this.text = this.text === `Long Long All Text` ? `Short` : `Long Long All Text`;
Expand All @@ -59,6 +64,8 @@ export class AppComponent implements AfterViewInit {
constructor(private fb: FormBuilder, private service: HelipopperService) {}

@ViewChild('inputName', { static: true }) inputName: ElementRef;
@ViewChild('inputNameComp', { static: true }) inputNameComp: ElementRef;
@ViewChild(NestedPopperComponent, { static: true }) nestedComp: NestedPopperComponent;

ngAfterViewInit() {
this.formControl.valueChanges.subscribe(value => {
Expand All @@ -68,6 +75,14 @@ export class AppComponent implements AfterViewInit {
this.popper.show();
}
});

this.formControlWithComp.valueChanges.subscribe(value => {
if (value && this.popperWithComp) {
this.popperWithComp.hide();
} else if (!value && this.popperWithComp) {
this.popperWithComp.show();
}
});
}

toggleSticky() {
Expand All @@ -92,4 +107,17 @@ export class AppComponent implements AfterViewInit {
this.popper = this.service.open(this.inputName, 'this field is required');
}
}

submitWithComp(): void {
if (!this.formControlWithComp.value) {
this.popperWithComp = this.service.open(this.inputNameComp, this.comp);
}
}

submitWithNestedComp(value: string): void {
if (!value) {
console.log(this.inputName);
this.popperWithNestedComp = this.service.open(this.nestedComp.inputName, this.comp);
}
}
}
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { AppComponent } from './app.component';
import { HelipopperModule } from '@ngneat/helipopper';
import { ReactiveFormsModule } from '@angular/forms';
import { ExampleComponent } from './example/example.component';
import { NestedPopperComponent } from './nested-popper/nested-popper.component';

@NgModule({
declarations: [AppComponent, ExampleComponent],
declarations: [AppComponent, ExampleComponent, NestedPopperComponent],
imports: [BrowserModule, AppRoutingModule, ReactiveFormsModule, HelipopperModule.forRoot()],
providers: [],
bootstrap: [AppComponent]
Expand Down
4 changes: 4 additions & 0 deletions src/app/nested-popper/nested-popper.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<fieldset>
<legend>Enter your name</legend>
<input #inputName type="text" [formControl]="formControl" (keyup.enter)="submit.emit(inputName.value)" />
</fieldset>
Empty file.
24 changes: 24 additions & 0 deletions src/app/nested-popper/nested-popper.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { NestedPopperComponent } from './nested-popper.component';

describe('NestedPopperComponent', () => {
let component: NestedPopperComponent;
let fixture: ComponentFixture<NestedPopperComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [NestedPopperComponent]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(NestedPopperComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
14 changes: 14 additions & 0 deletions src/app/nested-popper/nested-popper.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Component, ElementRef, EventEmitter, Output, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
selector: 'app-nested-popper',
templateUrl: './nested-popper.component.html',
styleUrls: ['./nested-popper.component.scss']
})
export class NestedPopperComponent {
formControl = new FormControl();
@Output() submit: EventEmitter<string> = new EventEmitter<string>();

@ViewChild('inputName', { static: true }) public inputName: ElementRef;
}

0 comments on commit 76cbb13

Please sign in to comment.