Skip to content

Commit ddfeeb9

Browse files
committed
expose event/eventSources/resources as top-level component inputs
1 parent 1eee159 commit ddfeeb9

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
v6.0.0-beta.3
33
-------------
4+
- FEATURE: expose event/eventSources/resources as top-level component inputs (#303)
45
- FIX: remove fast-deep-equals because Angular prefers ESM for tree shaking (#421)
56

67

lib/src/full-calendar.component.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { FullCalendarComponent } from './full-calendar.component';
4+
import { CalendarOptions } from '@fullcalendar/core';
45
import dayGridPlugin from '@fullcalendar/daygrid';
56

67
const DEFAULT_OPTIONS = {
@@ -147,6 +148,57 @@ describe('HostComponent', () => {
147148
});
148149

149150

151+
// uses the separate `events` input
152+
153+
@Component({
154+
selector: 'full-calendar-test',
155+
template: `
156+
<full-calendar
157+
[options]="calendarOptions"
158+
[events]="events"
159+
></full-calendar>
160+
`
161+
})
162+
class HostComponent2 {
163+
calendarOptions: CalendarOptions = {
164+
...DEFAULT_OPTIONS,
165+
eventDidMount: this.handleEventDidMount.bind(this)
166+
};
167+
events = [buildEvent()];
168+
eventRenderCnt = 0;
169+
170+
handleEventDidMount() {
171+
this.eventRenderCnt++;
172+
}
173+
174+
addEventReset() {
175+
this.events = this.events.concat([buildEvent()]);
176+
}
177+
}
178+
179+
describe('HostComponent2', () => {
180+
let component: HostComponent2;
181+
let fixture: ComponentFixture<HostComponent2>;
182+
183+
beforeEach(() => {
184+
TestBed.configureTestingModule({
185+
declarations: [FullCalendarComponent, HostComponent2]
186+
}).compileComponents();
187+
188+
fixture = TestBed.createComponent(HostComponent2);
189+
component = fixture.componentInstance;
190+
fixture.detectChanges(); // necessary for initializing change detection system
191+
});
192+
193+
it('should render events', () => {
194+
expect(component.eventRenderCnt).toBe(1);
195+
component.addEventReset();
196+
fixture.detectChanges();
197+
expect(component.eventRenderCnt).toBe(3); // +2 (the two events were freshly rendered)
198+
});
199+
})
200+
201+
150202
// some tests need a wrapper component with DEEP COMPARISON
151203

152204
@Component({

lib/src/full-calendar.component.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import { deepEqual } from './fast-deep-equal';
1313
import { deepCopy, shallowCopy, mapHash } from './utils';
1414
import { OPTION_IS_DEEP } from './options';
1515

16+
type CalendarOptionsLookup<OptionName> = OptionName extends keyof CalendarOptions
17+
? CalendarOptions[OptionName]
18+
: unknown
19+
1620
@Component({
1721
selector: 'full-calendar',
1822
template: '',
@@ -21,6 +25,9 @@ import { OPTION_IS_DEEP } from './options';
2125
export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterContentChecked, OnDestroy {
2226

2327
@Input() options?: CalendarOptions;
28+
@Input() events?: CalendarOptionsLookup<'events'>;
29+
@Input() eventSources?: CalendarOptionsLookup<'eventSources'>;
30+
@Input() resources?: CalendarOptionsLookup<'resources'>;
2431
@Input() deepChangeDetection?: boolean;
2532

2633
private calendar: Calendar | null = null;
@@ -31,7 +38,7 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
3138

3239
ngAfterViewInit() {
3340
const { deepChangeDetection } = this;
34-
const options = this.options || {};
41+
const options = this.buildOptions();
3542

3643
// initialize snapshot
3744
this.optionSnapshot = mapHash(options, (optionVal: any, optionName: string) => (
@@ -51,7 +58,7 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
5158
ngDoCheck() {
5259
if (this.calendar) { // not the initial render
5360
const { deepChangeDetection, optionSnapshot } = this;
54-
const newOptions = this.options || {};
61+
const newOptions = this.buildOptions();
5562
const newProcessedOptions: Record<string, any> = {};
5663
let anyChanges = false;
5764

@@ -116,4 +123,20 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
116123
return this.calendar!;
117124
}
118125

126+
private buildOptions(): CalendarOptions {
127+
const options = { ...this.options };
128+
129+
if (this.events !== undefined) {
130+
(options as any).events = this.events;
131+
}
132+
if (this.eventSources !== undefined) {
133+
(options as any).eventSources = this.eventSources;
134+
}
135+
if (this.resources !== undefined) {
136+
(options as any).resources = this.resources;
137+
}
138+
139+
return options;
140+
}
141+
119142
}

0 commit comments

Comments
 (0)