Skip to content

Commit

Permalink
feat: convert to standalone
Browse files Browse the repository at this point in the history
Fixes #57

Fixes #56
  • Loading branch information
mattlewis92 committed Nov 28, 2024
1 parent 07aa4a6 commit 73d51bc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 50 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,18 @@ npm install flatpickr angularx-flatpickr

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

Then include in your apps module:

```typescript
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FlatpickrModule } from 'angularx-flatpickr';

@NgModule({
imports: [FormsModule, FlatpickrModule.forRoot()],
})
export class MyModule {}
```

Finally use in one of your apps components:
Then use in one of your apps components:

```typescript
import { Component } from '@angular/core';
import { FlatpickrDirective, provideFlatpickrDefaults } from 'angularx-flatpickr';

@Component({
template: ` <input type="text" mwlFlatpickr [(ngModel)]="selectedDate" [altInput]="true" [convertModelValue]="true" /> `,
imports: [FlatpickrDirective],
providers: [provideFlatpickrDefaults()],
template: `<input type="text" mwlFlatpickr [(ngModel)]="selectedDate" [altInput]="true" [convertModelValue]="true" />`,
standalone: true,
selector: 'my-component',
})
export class MyComponent {}
```
Expand Down
1 change: 1 addition & 0 deletions projects/angularx-flatpickr/src/lib/flatpickr.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const FLATPICKR_CONTROL_VALUE_ACCESSOR: any = {
selector: '[mwlFlatpickr]',
providers: [FLATPICKR_CONTROL_VALUE_ACCESSOR],
exportAs: 'mwlFlatpickr',
standalone: true,
})
export class FlatpickrDirective
implements AfterViewInit, OnChanges, OnDestroy, ControlValueAccessor
Expand Down
41 changes: 23 additions & 18 deletions projects/angularx-flatpickr/src/lib/flatpickr.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,31 @@ import {

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

export function defaultsFactory(
userDefaults: FlatpickrDefaultsInterface,
): FlatpickrDefaults {
const defaults: FlatpickrDefaults = new FlatpickrDefaults();
Object.assign(defaults, userDefaults);
return defaults;
export function provideFlatpickrDefaults(
userDefaults: FlatpickrDefaultsInterface = {},
): Provider[] {
return [
{
provide: USER_DEFAULTS,
useValue: userDefaults,
},
{
provide: FlatpickrDefaults,
useFactory() {
const defaults: FlatpickrDefaults = new FlatpickrDefaults();
Object.assign(defaults, userDefaults);
return defaults;
},
deps: [USER_DEFAULTS],
},
];
}

/**
* @deprecated Will be removed in the next major version. Please use the standalone `FlatpickrDirective` and `provideFlatpickrDefaults()` instead.
*/
@NgModule({
declarations: [FlatpickrDirective],
imports: [FlatpickrDirective],
exports: [FlatpickrDirective],
})
export class FlatpickrModule {
Expand All @@ -30,17 +45,7 @@ export class FlatpickrModule {
): ModuleWithProviders<FlatpickrModule> {
return {
ngModule: FlatpickrModule,
providers: [
{
provide: USER_DEFAULTS,
useValue: userDefaults,
},
{
provide: FlatpickrDefaults,
useFactory: defaultsFactory,
deps: [USER_DEFAULTS],
},
],
providers: provideFlatpickrDefaults(userDefaults),
};
}
}
9 changes: 9 additions & 0 deletions projects/demo/app/demo.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { Component, ViewEncapsulation } from '@angular/core';
import {
FlatpickrDirective,
provideFlatpickrDefaults,
} from 'angularx-flatpickr';
import { FormsModule } from '@angular/forms';
import { JsonPipe } from '@angular/common';

@Component({
selector: 'mwl-demo-app',
standalone: true,
imports: [FlatpickrDirective, FormsModule, JsonPipe],
providers: [provideFlatpickrDefaults()],
template: `
<div class="container-fluid">
<div class="row">
Expand Down
12 changes: 0 additions & 12 deletions projects/demo/app/demo.module.ts

This file was deleted.

8 changes: 3 additions & 5 deletions projects/demo/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { bootstrapApplication } from '@angular/platform-browser';

import { DemoModule } from './app/demo.module';
import { environment } from './environments/environment';
import { DemoComponent } from './app/demo.component';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic()
.bootstrapModule(DemoModule)
.catch((err) => console.error(err));
bootstrapApplication(DemoComponent).catch((err) => console.error(err));

0 comments on commit 73d51bc

Please sign in to comment.