-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(date-picker): define formatDate as global config
- Loading branch information
1 parent
72e3bf9
commit 18735a7
Showing
6 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
apps/app/src/app/pages/(components)/components/(date-picker)/date-picker--config.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Component } from '@angular/core'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { HlmDatePickerComponent, provideHlmDatePickerConfig } from '@spartan-ng/ui-date-picker-helm'; | ||
import { DateTime } from 'luxon'; | ||
|
||
@Component({ | ||
selector: 'spartan-date-picker-config', | ||
standalone: true, | ||
imports: [HlmDatePickerComponent, FormsModule], | ||
template: ` | ||
<hlm-date-picker [min]="minDate" [max]="maxDate"> | ||
<span>Pick a date</span> | ||
</hlm-date-picker> | ||
`, | ||
providers: [ | ||
provideHlmDatePickerConfig({ formatDate: (date: Date) => DateTime.fromJSDate(date).toFormat('dd.MM.yyyy') }), | ||
], | ||
host: { | ||
class: 'preview flex min-h-[350px] w-full justify-center p-10 items-center', | ||
}, | ||
}) | ||
export class DatePickerConfigExampleComponent { | ||
/** The minimum date */ | ||
public minDate = new Date(2023, 0, 1); | ||
|
||
/** The maximum date */ | ||
public maxDate = new Date(2030, 11, 31); | ||
} | ||
|
||
export const datePickerConfigCode = ` | ||
import { Component } from '@angular/core'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { HlmDatePickerComponent, provideHlmDatePickerConfig } from '@spartan-ng/ui-date-picker-helm'; | ||
import { DateTime } from 'luxon'; | ||
@Component({ | ||
selector: 'spartan-date-picker-config', | ||
standalone: true, | ||
imports: [HlmDatePickerComponent, FormsModule], | ||
template: \` | ||
<hlm-date-picker [min]="minDate" [max]="maxDate"> | ||
<span>Pick a date</span> | ||
</hlm-date-picker> | ||
\`, | ||
providers: [ | ||
provideHlmDatePickerConfig({ formatDate: (date: Date) => DateTime.fromJSDate(date).toFormat('dd.MM.yyyy') }), | ||
], | ||
host: { | ||
class: 'preview flex min-h-[350px] w-full justify-center p-10 items-center', | ||
}, | ||
}) | ||
export class DatePickerConfigExampleComponent { | ||
/** The minimum date */ | ||
public minDate = new Date(2023, 0, 1); | ||
/** The maximum date */ | ||
public maxDate = new Date(2030, 11, 31); | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { inject, InjectionToken, ValueProvider } from '@angular/core'; | ||
|
||
export interface HlmDatePickerConfig<T> { | ||
/** | ||
* Defines how the date should be displayed in the UI. | ||
* | ||
* @param date | ||
* @returns formatted date | ||
*/ | ||
formatDate: (date: T) => string; | ||
} | ||
|
||
const defaultConfig: HlmDatePickerConfig<Date> = { | ||
formatDate: (date) => (date instanceof Date ? date.toDateString() : `${date}`), | ||
}; | ||
|
||
const HlmDatePickerConfigToken = new InjectionToken<HlmDatePickerConfig<unknown>>('HlmDatePickerConfig'); | ||
|
||
export function provideHlmDatePickerConfig<T>(config?: HlmDatePickerConfig<T>): ValueProvider { | ||
return { provide: HlmDatePickerConfigToken, useValue: { ...defaultConfig, ...config } }; | ||
} | ||
|
||
export function injectHlmDatePickerConfig<T>(): HlmDatePickerConfig<T> { | ||
return inject(HlmDatePickerConfigToken, { optional: true }) ?? (defaultConfig as HlmDatePickerConfig<T>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters