-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kit): allow dynamic changes to TUI_NUMBER_FORMAT (#6856)
Co-authored-by: taiga-family-bot <[email protected]>
- Loading branch information
1 parent
203b473
commit 3b3feb1
Showing
27 changed files
with
140 additions
and
62 deletions.
There are no files selected for viewing
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
26 changes: 15 additions & 11 deletions
26
projects/core/directives/number-format/number-format.directive.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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
/* eslint-disable no-restricted-syntax,no-restricted-imports */ | ||
import {Directive, forwardRef, Inject, Input} from '@angular/core'; | ||
import {Directive, forwardRef, inject, Input} from '@angular/core'; | ||
import {TuiNumberFormatSettings} from '@taiga-ui/core/interfaces'; | ||
import {TUI_NUMBER_FORMAT, TUI_NUMBER_FORMAT_OBSERVABLE} from '@taiga-ui/core/tokens'; | ||
import {BehaviorSubject} from 'rxjs'; | ||
import {TUI_NUMBER_FORMAT} from '@taiga-ui/core/tokens'; | ||
import {combineLatest, map, Observable, ReplaySubject} from 'rxjs'; | ||
|
||
@Directive({ | ||
selector: '[tuiNumberFormat]', | ||
providers: [ | ||
{ | ||
provide: TUI_NUMBER_FORMAT_OBSERVABLE, | ||
provide: TUI_NUMBER_FORMAT, | ||
useExisting: forwardRef(() => TuiNumberFormatDirective), | ||
}, | ||
], | ||
}) | ||
export class TuiNumberFormatDirective extends BehaviorSubject<TuiNumberFormatSettings> { | ||
export class TuiNumberFormatDirective extends Observable<TuiNumberFormatSettings> { | ||
private readonly settings = new ReplaySubject<Partial<TuiNumberFormatSettings>>(1); | ||
private readonly parent = inject(TUI_NUMBER_FORMAT, {skipSelf: true}); | ||
|
||
@Input() | ||
public set tuiNumberFormat(format: Partial<TuiNumberFormatSettings>) { | ||
this.next({...this.settings, decimalLimit: NaN, ...format}); | ||
this.settings.next(format); | ||
} | ||
|
||
constructor( | ||
@Inject(TUI_NUMBER_FORMAT) private readonly settings: TuiNumberFormatSettings, | ||
) { | ||
super({...settings, decimalLimit: NaN}); | ||
constructor() { | ||
super(subscriber => | ||
combineLatest([this.parent, this.settings]) | ||
.pipe(map(([parent, settings]) => ({...parent, ...settings}))) | ||
.subscribe(subscriber), | ||
); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,26 @@ | ||
import {Provider} from '@angular/core'; | ||
import {tuiCreateToken, tuiProvideOptions} from '@taiga-ui/cdk'; | ||
import {InjectionToken, Optional, Provider, SkipSelf} from '@angular/core'; | ||
import {tuiCreateToken} from '@taiga-ui/cdk'; | ||
import {TUI_DEFAULT_NUMBER_FORMAT} from '@taiga-ui/core/constants'; | ||
import {TuiNumberFormatSettings} from '@taiga-ui/core/interfaces'; | ||
import {map, Observable, of} from 'rxjs'; | ||
|
||
/** | ||
* Formatting configuration for displayed numbers | ||
*/ | ||
export const TUI_NUMBER_FORMAT = tuiCreateToken(TUI_DEFAULT_NUMBER_FORMAT); | ||
export const TUI_NUMBER_FORMAT: InjectionToken<Observable<TuiNumberFormatSettings>> = | ||
tuiCreateToken(of(TUI_DEFAULT_NUMBER_FORMAT)); | ||
|
||
export function tuiNumberFormatProvider( | ||
options: Partial<TuiNumberFormatSettings>, | ||
): Provider { | ||
return tuiProvideOptions(TUI_NUMBER_FORMAT, options, TUI_DEFAULT_NUMBER_FORMAT); | ||
return { | ||
provide: TUI_NUMBER_FORMAT, | ||
deps: [[new Optional(), new SkipSelf(), TUI_NUMBER_FORMAT]], | ||
useFactory: ( | ||
parent: Observable<TuiNumberFormatSettings> | null, | ||
): Observable<TuiNumberFormatSettings> => | ||
(parent || of(TUI_DEFAULT_NUMBER_FORMAT)).pipe( | ||
map(format => ({...format, ...options})), | ||
), | ||
}; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
projects/demo/src/modules/components/input-number/examples/7/index.html
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,7 @@ | ||
<tui-input-number | ||
tuiTextfieldPrefix="$" | ||
class="input" | ||
[(ngModel)]="value" | ||
> | ||
Type a sum | ||
</tui-input-number> |
31 changes: 31 additions & 0 deletions
31
projects/demo/src/modules/components/input-number/examples/7/index.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,31 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
import {TUI_DEFAULT_NUMBER_FORMAT, TUI_NUMBER_FORMAT} from '@taiga-ui/core'; | ||
import {TUI_LANGUAGE, TuiLanguage} from '@taiga-ui/i18n'; | ||
import {BehaviorSubject, map, Observable, switchMap} from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'tui-input-number-example-7', | ||
templateUrl: './index.html', | ||
encapsulation, | ||
changeDetection, | ||
providers: [ | ||
{ | ||
provide: TUI_NUMBER_FORMAT, | ||
deps: [TUI_LANGUAGE], | ||
useFactory: (lang: BehaviorSubject<Observable<TuiLanguage>>) => | ||
lang.pipe( | ||
switchMap(lang => lang), | ||
map(({name}) => ({ | ||
...TUI_DEFAULT_NUMBER_FORMAT, | ||
thousandSeparator: name === 'english' ? ',' : ' ', | ||
decimalSeparator: name === 'english' ? '.' : ',', | ||
})), | ||
), | ||
}, | ||
], | ||
}) | ||
export class TuiInputNumberExample7 { | ||
protected value = 1234.56; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
projects/demo/src/modules/directives/copy-processor/examples/1/index.html
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
9 changes: 6 additions & 3 deletions
9
projects/demo/src/modules/pipes/format-number/examples/1/index.html
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
<p>Formatted number by default: {{ 10500.33 | tuiFormatNumber }}</p> | ||
<p>Formatted number by default: {{ 10500.33 | tuiFormatNumber | async }}</p> | ||
|
||
<p>Formatted number with custom params: {{ 10500.33 | tuiFormatNumber: {decimalLimit: 4, decimalSeparator: '.'} }}</p> | ||
<p> | ||
Formatted number with custom params: | ||
{{ 10500.33 | tuiFormatNumber: {decimalLimit: 4, decimalSeparator: '.'} | async }} | ||
</p> | ||
|
||
<p> | ||
Formatted number with rounding: | ||
{{ 10500.334 | tuiFormatNumber: {decimalLimit: 2, decimalSeparator: '.', rounding: 'ceil'} }} | ||
{{ 10500.334 | tuiFormatNumber: {decimalLimit: 2, decimalSeparator: '.', rounding: 'ceil'} | async }} | ||
</p> |
2 changes: 1 addition & 1 deletion
2
projects/demo/src/modules/pipes/format-number/examples/import/insert-template.md
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
```html | ||
Formatted number: {{index | tuiFormatNumber}} | ||
Formatted number: {{index | tuiFormatNumber | async}} | ||
``` |
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
Oops, something went wrong.