Skip to content

Commit

Permalink
feat(date-picker): define formatDate as global config
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjulian committed Jan 29, 2025
1 parent 72e3bf9 commit 18735a7
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 16 deletions.
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);
}
`;
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HlmDatePickerComponent } from '@spartan-ng/ui-date-picker-helm';
import { HlmDatePickerComponent, provideHlmDatePickerConfig } from '@spartan-ng/ui-date-picker-helm';
import { DateTime } from 'luxon';

@Component({
selector: 'spartan-date-picker-format',
standalone: true,
imports: [HlmDatePickerComponent, FormsModule],
template: `
<hlm-date-picker [min]="minDate" [max]="maxDate" [dateFormat]="dateFormat">
<hlm-date-picker [min]="minDate" [max]="maxDate" [formatDate]="formatDate">
<span>Pick a date</span>
</hlm-date-picker>
`,
providers: [
// Global formatDate config
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',
},
Expand All @@ -23,24 +27,29 @@ export class DatePickerFormatExampleComponent {
/** The maximum date */
public maxDate = new Date(2030, 11, 31);

public dateFormat = (date: Date) => DateTime.fromJSDate(date).toFormat('dd.MM.yyyy');
/** Overrides global formatDate */
public formatDate = (date: Date) => DateTime.fromJSDate(date).toFormat('MMMM dd, yyyy');
}

export const datePickerFormatCode = `
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HlmDatePickerComponent } from '@spartan-ng/ui-date-picker-helm';
import { HlmDatePickerComponent, provideHlmDatePickerConfig } from '@spartan-ng/ui-date-picker-helm';
import { DateTime } from 'luxon';
@Component({
selector: 'spartan-date-picker-format',
standalone: true,
imports: [HlmDatePickerComponent, FormsModule],
template: \`
<hlm-date-picker [min]="minDate" [max]="maxDate" [dateFormat]="dateFormat">
<hlm-date-picker [min]="minDate" [max]="maxDate" [formatDate]="formatDate">
<span>Pick a date</span>
</hlm-date-picker>
\`,
providers: [
// Global formatDate config
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',
},
Expand All @@ -52,6 +61,7 @@ export class DatePickerFormatExampleComponent {
/** The maximum date */
public maxDate = new Date(2030, 11, 31);
public dateFormat = (date: Date) => DateTime.fromJSDate(date).toFormat('dd.MM.yyyy');
/** Overrides global formatDate */
public formatDate = (date: Date) => DateTime.fromJSDate(date).toFormat('MMMM dd, yyyy');
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RouteMeta } from '@analogjs/router';
import { Component } from '@angular/core';
import { hlmH4 } from '@spartan-ng/ui-typography-helm';
import { hlmCode, hlmH4, hlmP } from '@spartan-ng/ui-typography-helm';
import { CodeComponent } from '../../../../shared/code/code.component';
import { PageBottomNavLinkComponent } from '../../../../shared/layout/page-bottom-nav/page-bottom-nav-link.component';
import { PageBottomNavComponent } from '../../../../shared/layout/page-bottom-nav/page-bottom-nav.component';
Expand All @@ -10,6 +10,7 @@ import { SectionSubHeadingComponent } from '../../../../shared/layout/section-su
import { TabsCliComponent } from '../../../../shared/layout/tabs-cli.component';
import { TabsComponent } from '../../../../shared/layout/tabs.component';
import { metaWith } from '../../../../shared/meta/meta.util';
import { datePickerConfigCode, DatePickerConfigExampleComponent } from './date-picker--config.example';
import { datePickerFormCode, DatePickerFormExampleComponent } from './date-picker--form.example';
import { datePickerFormatCode, DatePickerFormatExampleComponent } from './date-picker--format.example';
import { codeSkeleton, DatePickerPreviewComponent, defaultCode, defaultImports } from './date-picker.preview';
Expand All @@ -33,6 +34,7 @@ export const routeMeta: RouteMeta = {
PageBottomNavComponent,
PageBottomNavLinkComponent,
PageNavComponent,
DatePickerConfigExampleComponent,
DatePickerFormatExampleComponent,
DatePickerFormExampleComponent,
],
Expand Down Expand Up @@ -61,14 +63,48 @@ export const routeMeta: RouteMeta = {
</div>
<spartan-section-sub-heading id="examples">Examples</spartan-section-sub-heading>
<h3 id="examples__default" class="${hlmH4} mb-2 mt-6">Custom Configs</h3>
<p class="${hlmP} mb-6">
Use
<code class="${hlmCode}">provideHlmDatePickerConfig</code>
to provide custom configs for the date picker component throughout the application.
</p>
<p class="${hlmP} mb-6">
<code class="${hlmCode}">formatDate: (date: T) => string;</code>
defines the default format how the date should be displayed in the UI.
</p>
<spartan-tabs firstTab="Preview" secondTab="Code">
<div spartanCodePreview firstTab>
<spartan-date-picker-config />
</div>
<spartan-code secondTab [code]="datePickerConfigCode" />
</spartan-tabs>
<h3 id="examples__default" class="${hlmH4} mb-2 mt-6">Format Date</h3>
<p class="${hlmP} mb-6">
Use
<code class="${hlmCode}">formatDate</code>
input to override the global date format for the date picker component.
</p>
<spartan-tabs firstTab="Preview" secondTab="Code">
<div spartanCodePreview firstTab>
<spartan-date-picker-format />
</div>
<spartan-code secondTab [code]="datePickerFormatCode" />
</spartan-tabs>
<h3 id="examples__default" class="${hlmH4} mb-2 mt-6">Form</h3>
<p class="${hlmP} mb-6">
Sync the date to a form by adding
<code class="${hlmCode}">formControlName</code>
to
<code class="${hlmCode}">hlm-date-picker</code>
.
</p>
<spartan-tabs firstTab="Preview" secondTab="Code">
<div spartanCodePreview firstTab>
<spartan-date-picker-form />
Expand All @@ -88,6 +124,7 @@ export default class CardPageComponent {
protected readonly defaultCode = defaultCode;
protected readonly defaultImports = defaultImports;
protected readonly codeSkeleton = codeSkeleton;
protected readonly datePickerConfigCode = datePickerConfigCode;
protected readonly datePickerFormCode = datePickerFormCode;
protected readonly datePickerFormatCode = datePickerFormatCode;
}
2 changes: 2 additions & 0 deletions libs/ui/date-picker/helm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NgModule } from '@angular/core';
import { HlmDatePickerComponent } from './lib/hlm-date-picker.component';

export * from './lib/hlm-date-format.token';

export * from './lib/hlm-date-picker.component';

@NgModule({
Expand Down
25 changes: 25 additions & 0 deletions libs/ui/date-picker/helm/src/lib/hlm-date-format.token.ts
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>);
}
15 changes: 6 additions & 9 deletions libs/ui/date-picker/helm/src/lib/hlm-date-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HlmCalendarComponent } from '@spartan-ng/ui-calendar-helm';
import { HlmIconDirective } from '@spartan-ng/ui-icon-helm';
import { HlmPopoverContentDirective } from '@spartan-ng/ui-popover-helm';
import type { ClassValue } from 'clsx';
import { injectHlmDatePickerConfig } from './hlm-date-format.token';

export const HLM_DATE_PICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
Expand Down Expand Up @@ -58,6 +59,8 @@ export const HLM_DATE_PICKER_VALUE_ACCESSOR = {
},
})
export class HlmDatePickerComponent<T> {
private readonly _config = injectHlmDatePickerConfig<T>();

public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
Expand Down Expand Up @@ -88,18 +91,12 @@ export class HlmDatePickerComponent<T> {
disabled: signal(this.disabled()),
}));

public readonly dateFormat = input<(date: T) => string>((date) =>
date instanceof Date ? date.toDateString() : `${date}`,
);
/** Defines how the date should be displayed in the UI. */
public readonly formatDate = input<(date: T) => string>(this._config.formatDate);

protected readonly formattedDate = computed(() => {
const date = this.date();

if (!date) {
return undefined;
}

return this.dateFormat()(date);
return date ? this.formatDate()(date) : undefined;
});

public readonly changed = output<T>();
Expand Down

0 comments on commit 18735a7

Please sign in to comment.