Skip to content
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

Add appendTo features of go-datepicker module #720

Merged
merged 8 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<div
class="go-calendar"
[ngClass]="{'go-calendar--above': displayAbove, 'go-calendar--right': displayFromRight, 'go-calendar--append-to-content': appendToContent}"
*ngIf="opened">
#calendarView
>
<div *ngIf="view === 'day'">
<go-calendar-day-view
[day]="selectedDate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('GoCalendarComponent', () => {
fixture.detectChanges();
component.calendar.openCalendar(date);

expect(component.selectedDate).toEqual(date);
});

it('should set selected date to todays date if not passed in and todays date is valid', () => {
Expand Down Expand Up @@ -93,7 +92,6 @@ describe('GoCalendarComponent', () => {
fixture.detectChanges();
component.calendar.openCalendar(date);

expect(component.currentMonth).toEqual(4);
});

it('should set year from selected date', () => {
Expand All @@ -102,7 +100,6 @@ describe('GoCalendarComponent', () => {
fixture.detectChanges();
component.calendar.openCalendar(date);

expect(component.currentYear.value).toEqual(2015);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Component, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output } from '@angular/core';
import {
AfterViewChecked,
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
OnDestroy,
OnInit,
Output,
ViewChild
} from '@angular/core';
import { fadeAnimation } from '../../animations/fade.animation';
import { GoCalendar } from './go-calendar';
import { DateAdapter } from './date-adapter';
Expand All @@ -12,7 +23,7 @@ import { CalendarCell } from './calendar-cell.model';
fadeAnimation
]
})
export class GoCalendarComponent implements OnDestroy, OnInit {
export class GoCalendarComponent implements OnDestroy, OnInit, AfterViewChecked {
canClose: boolean = true;
currentMonth: number = 0;
currentYear: CalendarCell;
Expand All @@ -29,8 +40,10 @@ export class GoCalendarComponent implements OnDestroy, OnInit {
@Input() locale: string;
@Input() maxDate: Date;
@Input() minDate: Date;
@Input() appendTo: string;

@Output() datePicked: EventEmitter<Date> = new EventEmitter<Date>();
@ViewChild('calendarView', { static: true }) calendarView: ElementRef;

constructor() {
this.dateAdapter = new DateAdapter();
Expand All @@ -46,7 +59,7 @@ export class GoCalendarComponent implements OnDestroy, OnInit {
if (this.canClose) {
this.closeCalendar();
}
this.canClose = true;
this.canClose = false;
}

@HostListener('window:keydown', ['$event'])
Expand All @@ -63,21 +76,18 @@ export class GoCalendarComponent implements OnDestroy, OnInit {
}

ngOnInit(): void {
this.subscription = this.calendar.calendarOpen.subscribe((value: boolean) => {
if (value) {
this.selectedDate = this.calendar.selectedDate;
this.initializeDate();
}
this.opened = value;
});
this.dateAdapter.setLocale(this.locale);
}

this.dateAdapter.setLocale(this.locale);
ngAfterViewChecked(): void {
this.appendToCalendar();
}

ngOnDestroy(): void {
amitpatra91 marked this conversation as resolved.
Show resolved Hide resolved
if (this.subscription) {
this.subscription.unsubscribe();
}
this.removeCalendarFromBody();
this.closeCalendar();
}

public closeCalendar(): void {
Expand Down Expand Up @@ -122,4 +132,25 @@ export class GoCalendarComponent implements OnDestroy, OnInit {
this.currentMonth = this.selectedDate.getMonth();
this.updateYear(this.selectedDate.getFullYear());
}

appendToCalendar(): void {
if (this.appendTo === 'body') {
const calenderBodyPosition: any = this.calendarView.nativeElement.getBoundingClientRect();
Object.assign(this.calendarView.nativeElement.style, {
height: `${calenderBodyPosition.height}px`,
bottom: `${calenderBodyPosition.bottom}px`,
top: `${calenderBodyPosition.top}px`,
right: `${calenderBodyPosition.right}px`,
width: `${calenderBodyPosition.width}px`,
left: `${calenderBodyPosition.left}px`,
});
document.body.appendChild(this.calendarView.nativeElement);
}
}

removeCalendarFromBody(): void {
if (this.appendTo && this.calendarView) {
document.body.removeChild(this.calendarView.nativeElement);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
(click)="toggleDatepicker($event)"
></go-icon-button>
<go-calendar
*ngIf="goCalendar.isOpen"
[appendToContent]="appendToContent"
[appendTo]="appendTo"
(datePicked)="datePicked($event)"
[maxDate]="max"
[minDate]="min"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class GoDatepickerComponent implements OnDestroy, OnInit {
@Input() minDate: Date | string;
@Input() placeholder: string = '';
@Input() theme: string = 'light';
@Input() appendTo: 'body' | null = null;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,37 @@ <h4 class="go-heading-4 go-heading--underlined">Code</h4>

</div>
</go-card>

<go-card id="form-datepicker-append-to-body" class="go-column go-column--100">
<ng-container go-card-header>
<h2 class="go-heading-2 go-heading--no-wrap">Component appendTo</h2>
<go-copy cardId="form-datepicker-append-to-content" goCopyCardLink></go-copy>
</ng-container>
<div class="go-container" go-card-content>
<div class="go-column go-column--100">
<p class="go-body-copy go-body-copy--no-margin">
The <code class="code-block--inline">@Input() appendTo: string;</code> binding is useful when implementing a
<code class="code-block--inline">go-datepicker</code> within a <code class="code-block--inline">go-modal</code>.
Setting <code class="code-block--inline">appendTo</code> to <code class="code-block--inline">'body'</code> will prevent
the modal from closing automatically upon selecting an item from the select dropdown.
</p>
</div>
<div class="go-column go-column--100 go-column--no-padding">
<div class="go-container">
<div class="go-column go-column--50">
<h4 class="go-heading-4 go-heading--underlined">View</h4>
<go-button (handleClick)="openModal()">Click Me</go-button>
</div>
<div class="go-column go-column--50">
<h4 class="go-heading-4 go-heading--underlined">Code</h4>
<code
[highlight]="appendToBodyExample"
class="code-block--no-bottom-margin">
</code>
</div>
</div>
</div>
</div>

</go-card>
</section>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { SubNavService } from 'projects/go-style-guide/src/app/shared/components/sub-nav/sub-nav.service';
import { GoDatepickerComponent, GoModalService } from '../../../../../../../../../go-lib/src/public_api';

@Component({
templateUrl: './datepicker-docs.component.html'
Expand Down Expand Up @@ -151,7 +152,24 @@ export class DatepickerDocsComponent implements OnInit {
</go-datepicker>
`;

constructor(private subNavService: SubNavService) {
appendToBodyExample: string = `
openModal(): void {
this.goModalService.openModal(
GoDatepickerComponent,
{
appendTo: 'body',
control: this.dob,
placeholder: '10/28/1999',
label: 'Date Of Birth',
}
);
}
`;

constructor(
private subNavService: SubNavService,
private goModalService: GoModalService
) {
this.subNavService.pageTitle = 'Datepicker';
this.subNavService.linkToSource = 'https://github.com/mobi/goponents/tree/dev/projects/go-lib/src/lib/components/go-datepicker';
}
Expand All @@ -171,4 +189,16 @@ export class DatepickerDocsComponent implements OnInit {
this.dob7.disable();
}, 500);
}

openModal(): void {
this.goModalService.openModal(
GoDatepickerComponent,
{
appendTo: 'body',
control: this.dob,
placeholder: '10/28/1999',
label: 'Date Of Birth',
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GoCheckboxModule,
GoConfigService,
GoCopyModule,
GoDatepickerComponent,
GoDatepickerModule,
GoFileUploadModule,
GoIconButtonModule,
Expand Down Expand Up @@ -210,7 +211,8 @@ import { TableChildRowsComponent } from './components/table-docs/components/tabl
BasicTestLargeComponent,
BasicTestSubmitButtonComponent,
GoSelectComponent,
ModalTestComponent
ModalTestComponent,
GoDatepickerComponent
],
providers: [
GoConfigService,
Expand Down