-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0c0993
commit 3daf9a2
Showing
22 changed files
with
120 additions
and
84 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
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,2 @@ | ||
export * from './number-format.directive'; | ||
export * from './number-format.module'; |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
26 changes: 26 additions & 0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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'; | ||
|
||
@Directive({ | ||
selector: '[tuiNumberFormat]', | ||
providers: [ | ||
{ | ||
provide: TUI_NUMBER_FORMAT_OBSERVABLE, | ||
useExisting: forwardRef(() => TuiNumberFormatDirective), | ||
}, | ||
], | ||
}) | ||
export class TuiNumberFormatDirective extends BehaviorSubject<TuiNumberFormatSettings> { | ||
@Input() | ||
set tuiNumberFormat(format: Partial<TuiNumberFormatSettings>) { | ||
this.next({...this.settings, ...format}); | ||
} | ||
|
||
constructor( | ||
@Inject(TUI_NUMBER_FORMAT) private readonly settings: TuiNumberFormatSettings, | ||
) { | ||
super(settings); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
projects/core/directives/number-format/number-format.module.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,9 @@ | ||
import {NgModule} from '@angular/core'; | ||
|
||
import {TuiNumberFormatDirective} from './number-format.directive'; | ||
|
||
@NgModule({ | ||
declarations: [TuiNumberFormatDirective], | ||
exports: [TuiNumberFormatDirective], | ||
}) | ||
export class TuiNumberFormatModule {} |
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,8 @@ | ||
import {tuiCreateToken} from '@taiga-ui/cdk'; | ||
import {TUI_DEFAULT_NUMBER_FORMAT} from '@taiga-ui/core/constants'; | ||
import {of} from 'rxjs'; | ||
|
||
/** | ||
* Formatting configuration for displayed numbers | ||
*/ | ||
export const TUI_NUMBER_FORMAT_OBSERVABLE = tuiCreateToken(of(TUI_DEFAULT_NUMBER_FORMAT)); |
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
18 changes: 9 additions & 9 deletions
18
projects/demo/src/modules/experimental/amount/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,14 +1,14 @@ | ||
<ol> | ||
<li>{{ 10728.9 | tuiAmount }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'RUB' }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'EUR' }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'USD' }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'GBP' }}</li> | ||
<li>{{ -12345.1 | tuiAmount: 'USD' : 'left' }}</li> | ||
<li>{{ 100 | tuiAmount: '£' : 'left' }}</li> | ||
<li>{{ 200 | tuiAmount: 'AED' : 'left' }}</li> | ||
<li>{{ 10728.9 | tuiAmount | async }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'RUB' | async }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'EUR' | async }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'USD' | async }}</li> | ||
<li>{{ 10728.9 | tuiAmount: 'GBP' | async }}</li> | ||
<li>{{ -12345.1 | tuiAmount: 'USD' : 'left' | async }}</li> | ||
<li>{{ 100 | tuiAmount: '£' : 'left' | async }}</li> | ||
<li>{{ 200 | tuiAmount: 'AED' : 'left' | async }}</li> | ||
</ol> | ||
|
||
<hr class="tui-space_top-1 tui-space_bottom-1" /> | ||
|
||
<span>Remaining {{ 10728.9 | tuiAmount }} of {{ 11000 | tuiAmount }}</span> | ||
<span>Remaining {{ 10728.9 | tuiAmount | async }} of {{ 11000 | tuiAmount | async }}</span> |
9 changes: 7 additions & 2 deletions
9
projects/demo/src/modules/experimental/amount/examples/2/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,2 +1,7 @@ | ||
<div>{{ -10000000.536 | tuiAmount: 'USD' : 'left' }}</div> | ||
<div>{{ 200.211 | tuiAmount: 'EUR' }}</div> | ||
<div> | ||
<div [tuiNumberFormat]="{rounding: 'round', thousandSeparator: ',', decimalSeparator: '.'}"> | ||
{{ -10000000.536 | tuiAmount: 'USD' : 'left' | async }} | ||
</div> | ||
|
||
<div>{{ 200.536 | tuiAmount: 'EUR' | async }}</div> | ||
</div> |
8 changes: 0 additions & 8 deletions
8
projects/demo/src/modules/experimental/amount/examples/2/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 |
---|---|---|
@@ -1,19 +1,11 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
import {tuiNumberFormatProvider} from '@taiga-ui/core'; | ||
|
||
@Component({ | ||
selector: 'tui-amount-example-2', | ||
templateUrl: './index.html', | ||
changeDetection, | ||
encapsulation, | ||
providers: [ | ||
tuiNumberFormatProvider({ | ||
decimalSeparator: '.', | ||
thousandSeparator: ',', | ||
rounding: 'round', | ||
}), | ||
], | ||
}) | ||
export class TuiAmountExample2 {} |
4 changes: 2 additions & 2 deletions
4
projects/demo/src/modules/experimental/amount/examples/3/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,2 +1,2 @@ | ||
<div>{{ -12.3 | tuiAmount: 'USD' : 'left' }}</div> | ||
<div>{{ 3000 | tuiAmount: 'EUR' }}</div> | ||
<div>{{ -12.3 | tuiAmount | async }}</div> | ||
<div>{{ 3000 | tuiAmount | async }}</div> |
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: 0 additions & 2 deletions
2
projects/demo/src/modules/experimental/amount/examples/4/index.html
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
projects/demo/src/modules/experimental/amount/examples/4/index.ts
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,11 +1,9 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {TuiFormatNumberPipe} from '@taiga-ui/core'; | ||
|
||
import {TuiAmountPipePipe} from './amount.pipe'; | ||
|
||
@NgModule({ | ||
declarations: [TuiAmountPipePipe], | ||
exports: [TuiAmountPipePipe], | ||
providers: [TuiFormatNumberPipe], | ||
}) | ||
export class TuiAmountPipeModule {} |
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 |
---|---|---|
@@ -1,45 +1,59 @@ | ||
import {Inject, Pipe, PipeTransform} from '@angular/core'; | ||
import {tuiFormatCurrency, tuiFormatSignSymbol} from '@taiga-ui/addon-commerce'; | ||
import {CHAR_NO_BREAK_SPACE} from '@taiga-ui/cdk'; | ||
import { | ||
TUI_NUMBER_FORMAT, | ||
TUI_NUMBER_FORMAT_OBSERVABLE, | ||
TuiDecimal, | ||
tuiFormatNumber, | ||
TuiNumberFormatSettings, | ||
} from '@taiga-ui/core'; | ||
import {Observable} from 'rxjs'; | ||
import {map} from 'rxjs/operators'; | ||
|
||
import {TUI_AMOUNT_OPTIONS, TuiAmountOptions} from './amount.options'; | ||
|
||
const MONEY_DECIMAL_LIMIT = 2; | ||
|
||
@Pipe({ | ||
name: `tuiAmount`, | ||
}) | ||
export class TuiAmountPipePipe implements PipeTransform { | ||
constructor( | ||
@Inject(TUI_AMOUNT_OPTIONS) private readonly options: TuiAmountOptions, | ||
@Inject(TUI_NUMBER_FORMAT) private readonly format: TuiNumberFormatSettings, | ||
@Inject(TUI_NUMBER_FORMAT_OBSERVABLE) | ||
private readonly format: Observable<TuiNumberFormatSettings>, | ||
) {} | ||
|
||
transform( | ||
value: number, | ||
currency = this.options.currency, | ||
currencyAlign = this.options.currencyAlign, | ||
): string { | ||
const sign = tuiFormatSignSymbol(value, this.options.sign); | ||
const currencySymbol = tuiFormatCurrency(currency); | ||
const formatted = tuiFormatNumber(Math.abs(value), { | ||
...this.format, | ||
decimalLimit: this.getDecimalLimit(value), | ||
}); | ||
// eslint-disable-next-line no-irregular-whitespace | ||
const space = currencySymbol?.length > 1 || currencyAlign === `right` ? ` ` : ``; | ||
): Observable<string> { | ||
return this.format.pipe( | ||
map(format => { | ||
const sign = tuiFormatSignSymbol(value, this.options.sign); | ||
const currencySymbol = tuiFormatCurrency(currency); | ||
const formatted = tuiFormatNumber(Math.abs(value), { | ||
...format, | ||
decimalLimit: this.getDecimalLimit( | ||
value, | ||
MONEY_DECIMAL_LIMIT, | ||
format.decimal, | ||
), | ||
}); | ||
const space = | ||
currencySymbol?.length > 1 || currencyAlign === `right` | ||
? CHAR_NO_BREAK_SPACE | ||
: ``; | ||
|
||
return currencyAlign === `right` | ||
? `${sign}${formatted}${space}${currencySymbol}` | ||
: `${sign}${currencySymbol}${space}${formatted}`; | ||
return currencyAlign === `right` | ||
? `${sign}${formatted}${space}${currencySymbol}` | ||
: `${sign}${currencySymbol}${space}${formatted}`; | ||
}), | ||
); | ||
} | ||
|
||
private getDecimalLimit(value: number): number { | ||
return this.options.decimal === `always` || | ||
(this.options.decimal === `not-zero` && value % 1) | ||
? this.options.precision | ||
: 0; | ||
private getDecimalLimit(value: number, limit: number, decimal: TuiDecimal): number { | ||
return decimal === `always` || (decimal === `not-zero` && value % 1) ? limit : 0; | ||
} | ||
} |