Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core)!: replace TUI_DATE_FORMAT, TUI_DATE_SEPARATOR with single observable token #7106

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion projects/cdk/abstract/control.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/// <reference types="@taiga-ui/tsconfig/ng-dev-mode" />
import type {OnDestroy, OnInit, Provider, Type} from '@angular/core';
import {ChangeDetectorRef, Directive, HostBinding, inject, Input} from '@angular/core';
import {
ChangeDetectorRef,
DestroyRef,
Directive,
HostBinding,
inject,
Input,
} from '@angular/core';
import type {AbstractControl, ControlValueAccessor} from '@angular/forms';
import {NgControl, NgModel} from '@angular/forms';
import {EMPTY_FUNCTION} from '@taiga-ui/cdk/constants';
Expand Down Expand Up @@ -43,7 +50,9 @@ export abstract class AbstractTuiControl<T>
protected onTouched = EMPTY_FUNCTION;
protected onChange = EMPTY_FUNCTION;
protected readonly fallbackValue = this.getFallbackValue();
// TODO: remove after migrating to takeUntilDestroyed()
protected readonly destroy$ = new Subject<void>();
protected destroyRef = inject(DestroyRef);
protected readonly cdr = inject(ChangeDetectorRef);
protected readonly valueTransformer = inject<TuiControlValueTransformer<T>>(
AbstractTuiValueTransformer,
Expand Down
7 changes: 0 additions & 7 deletions projects/cdk/date-time/date-format.ts

This file was deleted.

2 changes: 0 additions & 2 deletions projects/cdk/date-time/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from './date-clamp';
export * from './date-fillers';
export * from './date-format';
export * from './date-separator';
export * from './date-time';
export * from './day';
export * from './day-range';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {tuiCreateToken} from '@taiga-ui/cdk/utils';

/**
* Date separator for Taiga UI components
*/
export const TUI_DATE_SEPARATOR = tuiCreateToken('.');

/* internal */
export const changeDateSeparator = (
dateString: string,
newDateSeparator: string,
Expand Down
1 change: 1 addition & 0 deletions projects/cdk/utils/miscellaneous/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './array-remove';
export * from './array-shallow-equals';
export * from './array-toggle';
export * from './change-date-separator';
export * from './clean-object';
export * from './create-options';
export * from './create-token';
Expand Down
6 changes: 6 additions & 0 deletions projects/core/constants/default-date-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type {TuiDateFormatSettings} from '@taiga-ui/core/interfaces';

export const TUI_DEFAULT_DATE_FORMAT: TuiDateFormatSettings = {
mode: 'DMY',
separator: '.',
};
1 change: 1 addition & 0 deletions projects/core/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './cache-basting-payload';
export * from './decimal-symbols';
export * from './default-date-format';
export * from './default-icons-path';
export * from './default-marker-handler';
export * from './default-number-format';
Expand Down
32 changes: 32 additions & 0 deletions projects/core/directives/date-format/date-format.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Directive, forwardRef, inject, Input} from '@angular/core';
import type {TuiDateFormatSettings} from '@taiga-ui/core/interfaces';
import {TUI_DATE_FORMAT} from '@taiga-ui/core/tokens';
import {combineLatest, map, Observable, ReplaySubject} from 'rxjs';

@Directive({
standalone: true,
selector: '[tuiDateFormat]',
providers: [
{
provide: TUI_DATE_FORMAT,
useExisting: forwardRef(() => TuiDateFormatDirective),
},
],
})
export class TuiDateFormatDirective extends Observable<TuiDateFormatSettings> {
private readonly settings = new ReplaySubject<Partial<TuiDateFormatSettings>>(1);
private readonly parent = inject(TUI_DATE_FORMAT, {skipSelf: true});

constructor() {
super(subscriber =>
combineLatest([this.parent, this.settings])
.pipe(map(([parent, settings]) => ({...parent, ...settings})))
.subscribe(subscriber),
);
}

@Input()
public set tuiDateFormat(format: Partial<TuiDateFormatSettings>) {
this.settings.next(format);
}
}
1 change: 1 addition & 0 deletions projects/core/directives/date-format/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './date-format.directive';
5 changes: 5 additions & 0 deletions projects/core/directives/date-format/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}
1 change: 1 addition & 0 deletions projects/core/directives/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from '@taiga-ui/core/directives/appearance';
export * from '@taiga-ui/core/directives/date-format';
export * from '@taiga-ui/core/directives/dropdown';
export * from '@taiga-ui/core/directives/hint';
export * from '@taiga-ui/core/directives/icons';
Expand Down
1 change: 0 additions & 1 deletion projects/core/directives/number-format/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './number-format.directive';
export * from './number-format.module';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TUI_NUMBER_FORMAT} from '@taiga-ui/core/tokens';
import {combineLatest, map, Observable, ReplaySubject} from 'rxjs';

@Directive({
standalone: true,
selector: '[tuiNumberFormat]',
providers: [
{
Expand Down

This file was deleted.

16 changes: 16 additions & 0 deletions projects/core/interfaces/date-format-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {TuiDateMode} from '@taiga-ui/cdk';

/**
* Formatting configuration for displayed dates
*/
export interface TuiDateFormatSettings {
/**
* Date format mode.
*/
readonly mode: TuiDateMode;
/**
* Separator between date segments
* @example 10.02 ('.' by default)
*/
readonly separator: string;
}
1 change: 1 addition & 0 deletions projects/core/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './data-list-accessor';
export * from './data-list-host';
export * from './date-format-settings';
export * from './icon-error';
export * from './media';
export * from './number-format-settings';
Expand Down
26 changes: 26 additions & 0 deletions projects/core/tokens/date-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {InjectionToken, Provider} from '@angular/core';
import {Optional, SkipSelf} from '@angular/core';
import {tuiCreateToken} from '@taiga-ui/cdk';
import {TUI_DEFAULT_DATE_FORMAT} from '@taiga-ui/core/constants';
import type {TuiDateFormatSettings} from '@taiga-ui/core/interfaces';
import type {Observable} from 'rxjs';
import {map, of} from 'rxjs';

/**
* Formatting configuration for displayed dates
*/
export const TUI_DATE_FORMAT: InjectionToken<Observable<TuiDateFormatSettings>> =
tuiCreateToken(of(TUI_DEFAULT_DATE_FORMAT));

export function tuiDateFormatProvider(options: Partial<TuiDateFormatSettings>): Provider {
return {
provide: TUI_DATE_FORMAT,
deps: [[new Optional(), new SkipSelf(), TUI_DATE_FORMAT]],
useFactory: (
parent: Observable<TuiDateFormatSettings> | null,
): Observable<TuiDateFormatSettings> =>
(parent || of(TUI_DEFAULT_DATE_FORMAT)).pipe(
map(format => ({...format, ...options})),
),
};
}
1 change: 1 addition & 0 deletions projects/core/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './assert-enabled';
export * from './common-icon';
export * from './data-list-accessor';
export * from './data-list-host';
export * from './date-format';
export * from './day-type-handler';
export * from './document-or-shadow-root';
export * from './element-ref';
Expand Down
20 changes: 10 additions & 10 deletions projects/demo/src/modules/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,15 +618,11 @@ export const ROUTES: Routes = [
title: 'Input',
},
},
{
route({
path: 'components/input-date',
loadChildren: async () =>
(await import('../components/input-date/input-date.module'))
.ExampleTuiInputDateModule,
data: {
title: 'InputDate',
},
},
title: 'InputDate',
loadComponent: async () => import('../components/input-date'),
}),
{
path: 'components/input-date-multi',
loadChildren: async () =>
Expand Down Expand Up @@ -1781,8 +1777,12 @@ export const ROUTES: Routes = [
route({
path: 'directives/number-format',
title: 'NumberFormat',
loadComponent: async () =>
import('../directives/number-format/number-format.component'),
loadComponent: async () => import('../directives/number-format'),
}),
route({
path: 'directives/date-format',
title: 'NumberFormat',
loadComponent: async () => import('../directives/date-format'),
}),

// UTILS
Expand Down
7 changes: 7 additions & 0 deletions projects/demo/src/modules/app/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,13 @@ export const pages: TuiDocPages = [
keywords: 'number, format, число, separator, precision, rounding, формат',
route: '/directives/number-format',
},
{
section: 'Tools',
title: 'DateFormat',
keywords:
'date, format, дата, separator, год, year, month, месяц, день, day, формат',
route: '/directives/date-format',
},
{
section: 'Tools',
title: 'Touchable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h6 class="tui-text_h6">
</p>
<p>
Requires you to import
<code>TuiNumberFormatModule</code>
<code>TuiNumberFormatDirective</code>
.
</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TuiLinkModule,
TuiLoaderModule,
TuiNotificationModule,
TuiNumberFormatModule,
TuiNumberFormatDirective,
TuiSvgModule,
TuiTextfieldControllerModule,
} from '@taiga-ui/core';
Expand Down Expand Up @@ -73,7 +73,7 @@ import {TuiDialogExampleComponent10} from './examples/10';
PayExampleModalModule,
TuiTextfieldControllerModule,
TuiTextCodeModule,
TuiNumberFormatModule,
TuiNumberFormatDirective,
],
declarations: [
ExampleTuiDialogComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TUI_DATE_FORMAT, TUI_DATE_SEPARATOR, TuiDay, TuiDayRange} from '@taiga-ui/cdk';
import {TuiDay, TuiDayRange} from '@taiga-ui/cdk';
import {tuiDateFormatProvider} from '@taiga-ui/core';

@Component({
selector: 'tui-input-date-range-example-3',
templateUrl: './index.html',
encapsulation,
changeDetection,
providers: [
{provide: TUI_DATE_FORMAT, useValue: 'YMD'},
{provide: TUI_DATE_SEPARATOR, useValue: '/'},
],
providers: [tuiDateFormatProvider({mode: 'YMD', separator: '/'})],
})
export class TuiInputDateRangeExample3 {
protected readonly control = new FormControl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
is a field to input a range of dates.
</p>

<h3>DI-tokens for date localization:</h3>
<dl>
<dt>
<code>TUI_DATE_FORMAT</code>
</dt>
<dd class="tui-space_bottom-5">
active date format (
<code>'DMY' | 'MDY' | 'YMD'</code>
)
</dd>
<dt>
<code>TUI_DATE_SEPARATOR</code>
</dt>
<dd>single-character date's separator (dot, slash etc.)</dd>
</dl>
<p>
Date formatting can be customized with
<a
fragment="date-format"
routerLink="/utils/tokens"
tuiLink
>
TUI_DATE_FORMAT
</a>
token.
</p>
<p class="tui-space_bottom-9">
<a
fragment="date-localization"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TUI_DATE_FORMAT, TUI_DATE_SEPARATOR, TuiDay, TuiTime} from '@taiga-ui/cdk';
import {TuiDay, TuiTime} from '@taiga-ui/cdk';
import {tuiDateFormatProvider} from '@taiga-ui/core';

@Component({
selector: 'tui-input-date-time-example-3',
templateUrl: './index.html',
encapsulation,
changeDetection,
providers: [
{provide: TUI_DATE_FORMAT, useValue: 'YMD'},
{provide: TUI_DATE_SEPARATOR, useValue: '/'},
],
providers: [tuiDateFormatProvider({mode: 'YMD', separator: '/'})],
})
export class TuiInputDateTimeExample3 {
protected readonly control = new FormControl([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
allows to input date and time
</div>

<h3>DI-tokens for date localization:</h3>
<dl>
<dt>
<code>TUI_DATE_FORMAT</code>
</dt>
<dd class="tui-space_bottom-5">
active date format (
<code>'DMY' | 'MDY' | 'YMD'</code>
)
</dd>
<dt>
<code>TUI_DATE_SEPARATOR</code>
</dt>
<dd>single-character date's separator (dot, slash etc.)</dd>
</dl>
<p>
Date formatting can be customized with
<a
fragment="date-format"
routerLink="/utils/tokens"
tuiLink
>
TUI_DATE_FORMAT
</a>
token.
</p>
<p class="tui-space_bottom-9">
<a
fragment="date-localization"
Expand Down
Loading
Loading