Skip to content

Commit 73d51bc

Browse files
committed
feat: convert to standalone
Fixes #57 Fixes #56
1 parent 07aa4a6 commit 73d51bc

File tree

6 files changed

+43
-50
lines changed

6 files changed

+43
-50
lines changed

README.md

+7-15
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,18 @@ npm install flatpickr angularx-flatpickr
3232

3333
Next, in your `angular.json` add `"node_modules/flatpickr/dist/flatpickr.css"` to the `styles` array of your application
3434

35-
Then include in your apps module:
36-
37-
```typescript
38-
import { NgModule } from '@angular/core';
39-
import { FormsModule } from '@angular/forms';
40-
import { FlatpickrModule } from 'angularx-flatpickr';
41-
42-
@NgModule({
43-
imports: [FormsModule, FlatpickrModule.forRoot()],
44-
})
45-
export class MyModule {}
46-
```
47-
48-
Finally use in one of your apps components:
35+
Then use in one of your apps components:
4936

5037
```typescript
5138
import { Component } from '@angular/core';
39+
import { FlatpickrDirective, provideFlatpickrDefaults } from 'angularx-flatpickr';
5240

5341
@Component({
54-
template: ` <input type="text" mwlFlatpickr [(ngModel)]="selectedDate" [altInput]="true" [convertModelValue]="true" /> `,
42+
imports: [FlatpickrDirective],
43+
providers: [provideFlatpickrDefaults()],
44+
template: `<input type="text" mwlFlatpickr [(ngModel)]="selectedDate" [altInput]="true" [convertModelValue]="true" />`,
45+
standalone: true,
46+
selector: 'my-component',
5547
})
5648
export class MyComponent {}
5749
```

projects/angularx-flatpickr/src/lib/flatpickr.directive.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const FLATPICKR_CONTROL_VALUE_ACCESSOR: any = {
4444
selector: '[mwlFlatpickr]',
4545
providers: [FLATPICKR_CONTROL_VALUE_ACCESSOR],
4646
exportAs: 'mwlFlatpickr',
47+
standalone: true,
4748
})
4849
export class FlatpickrDirective
4950
implements AfterViewInit, OnChanges, OnDestroy, ControlValueAccessor

projects/angularx-flatpickr/src/lib/flatpickr.module.ts

+23-18
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@ import {
1212

1313
export const USER_DEFAULTS = new InjectionToken('flatpickr defaults');
1414

15-
export function defaultsFactory(
16-
userDefaults: FlatpickrDefaultsInterface,
17-
): FlatpickrDefaults {
18-
const defaults: FlatpickrDefaults = new FlatpickrDefaults();
19-
Object.assign(defaults, userDefaults);
20-
return defaults;
15+
export function provideFlatpickrDefaults(
16+
userDefaults: FlatpickrDefaultsInterface = {},
17+
): Provider[] {
18+
return [
19+
{
20+
provide: USER_DEFAULTS,
21+
useValue: userDefaults,
22+
},
23+
{
24+
provide: FlatpickrDefaults,
25+
useFactory() {
26+
const defaults: FlatpickrDefaults = new FlatpickrDefaults();
27+
Object.assign(defaults, userDefaults);
28+
return defaults;
29+
},
30+
deps: [USER_DEFAULTS],
31+
},
32+
];
2133
}
2234

35+
/**
36+
* @deprecated Will be removed in the next major version. Please use the standalone `FlatpickrDirective` and `provideFlatpickrDefaults()` instead.
37+
*/
2338
@NgModule({
24-
declarations: [FlatpickrDirective],
39+
imports: [FlatpickrDirective],
2540
exports: [FlatpickrDirective],
2641
})
2742
export class FlatpickrModule {
@@ -30,17 +45,7 @@ export class FlatpickrModule {
3045
): ModuleWithProviders<FlatpickrModule> {
3146
return {
3247
ngModule: FlatpickrModule,
33-
providers: [
34-
{
35-
provide: USER_DEFAULTS,
36-
useValue: userDefaults,
37-
},
38-
{
39-
provide: FlatpickrDefaults,
40-
useFactory: defaultsFactory,
41-
deps: [USER_DEFAULTS],
42-
},
43-
],
48+
providers: provideFlatpickrDefaults(userDefaults),
4449
};
4550
}
4651
}

projects/demo/app/demo.component.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { Component, ViewEncapsulation } from '@angular/core';
2+
import {
3+
FlatpickrDirective,
4+
provideFlatpickrDefaults,
5+
} from 'angularx-flatpickr';
6+
import { FormsModule } from '@angular/forms';
7+
import { JsonPipe } from '@angular/common';
28

39
@Component({
410
selector: 'mwl-demo-app',
11+
standalone: true,
12+
imports: [FlatpickrDirective, FormsModule, JsonPipe],
13+
providers: [provideFlatpickrDefaults()],
514
template: `
615
<div class="container-fluid">
716
<div class="row">

projects/demo/app/demo.module.ts

-12
This file was deleted.

projects/demo/main.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { enableProdMode } from '@angular/core';
2-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2+
import { bootstrapApplication } from '@angular/platform-browser';
33

4-
import { DemoModule } from './app/demo.module';
54
import { environment } from './environments/environment';
5+
import { DemoComponent } from './app/demo.component';
66

77
if (environment.production) {
88
enableProdMode();
99
}
1010

11-
platformBrowserDynamic()
12-
.bootstrapModule(DemoModule)
13-
.catch((err) => console.error(err));
11+
bootstrapApplication(DemoComponent).catch((err) => console.error(err));

0 commit comments

Comments
 (0)