-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimental):
TuiAmount
add new pipe
- Loading branch information
1 parent
b668d3a
commit 1060332
Showing
21 changed files
with
351 additions
and
1 deletion.
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
38 changes: 38 additions & 0 deletions
38
projects/demo/src/modules/experimental/amount/amount.component.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,38 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {TuiDocExample, TuiRawLoaderContent} from '@taiga-ui/addon-doc'; | ||
|
||
@Component({ | ||
selector: 'example-amount', | ||
templateUrl: './amount.template.html', | ||
changeDetection, | ||
}) | ||
export class ExampleTuiAmountComponent { | ||
readonly exampleModule: TuiRawLoaderContent = import( | ||
'./examples/import/import-module.md?raw' | ||
); | ||
|
||
readonly exampleHtml: TuiRawLoaderContent = import( | ||
'./examples/import/insert-template.md?raw' | ||
); | ||
|
||
readonly example1: TuiDocExample = { | ||
HTML: import('./examples/1/index.html?raw'), | ||
TypeScript: import('./examples/1/index.ts?raw'), | ||
}; | ||
|
||
readonly example2: TuiDocExample = { | ||
HTML: import('./examples/2/index.html?raw'), | ||
TypeScript: import('./examples/2/index.ts?raw'), | ||
}; | ||
|
||
readonly example3: TuiDocExample = { | ||
HTML: import('./examples/3/index.html?raw'), | ||
TypeScript: import('./examples/3/index.ts?raw'), | ||
}; | ||
|
||
readonly example4: TuiDocExample = { | ||
HTML: import('./examples/4/index.html?raw'), | ||
TypeScript: import('./examples/4/index.ts?raw'), | ||
}; | ||
} |
30 changes: 30 additions & 0 deletions
30
projects/demo/src/modules/experimental/amount/amount.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,30 @@ | ||
import {CommonModule} from '@angular/common'; | ||
import {NgModule} from '@angular/core'; | ||
import {tuiGetDocModules} from '@taiga-ui/addon-doc'; | ||
import {TuiLinkModule, TuiNotificationModule} from '@taiga-ui/core'; | ||
import {TuiAmountPipeModule} from '@taiga-ui/experimental'; | ||
|
||
import {ExampleTuiAmountComponent} from './amount.component'; | ||
import {TuiAmountExample1} from './examples/1'; | ||
import {TuiAmountExample2} from './examples/2'; | ||
import {TuiAmountExample3} from './examples/3'; | ||
import {TuiAmountExample4} from './examples/4'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
TuiAmountPipeModule, | ||
TuiNotificationModule, | ||
tuiGetDocModules(ExampleTuiAmountComponent), | ||
TuiLinkModule, | ||
], | ||
declarations: [ | ||
ExampleTuiAmountComponent, | ||
TuiAmountExample1, | ||
TuiAmountExample2, | ||
TuiAmountExample3, | ||
TuiAmountExample4, | ||
], | ||
exports: [ExampleTuiAmountComponent], | ||
}) | ||
export class ExampleTuiAmountModule {} |
72 changes: 72 additions & 0 deletions
72
projects/demo/src/modules/experimental/amount/amount.template.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,72 @@ | ||
<tui-doc-page | ||
header="AmountPipe" | ||
package="EXPERIMENTAL" | ||
type="pipes" | ||
> | ||
<ng-template pageTab> | ||
<tui-notification status="warning"> | ||
This code is | ||
<strong>experimental</strong> | ||
and is a subject to change. Expect final solution to be shipped in the next major version | ||
</tui-notification> | ||
|
||
<p>Pipe to format number values to show money sums</p> | ||
|
||
<p> | ||
Number formatting can be customized by using | ||
<a | ||
fragment="number-format" | ||
routerLink="/utils/tokens" | ||
tuiLink | ||
> | ||
TUI_NUMBER_FORMAT | ||
</a> | ||
</p> | ||
|
||
<tui-doc-example | ||
id="base" | ||
heading="Basic" | ||
[content]="example1" | ||
> | ||
<tui-amount-example-1></tui-amount-example-1> | ||
</tui-doc-example> | ||
|
||
<tui-doc-example | ||
id="format" | ||
heading="Format" | ||
[content]="example2" | ||
> | ||
<tui-amount-example-2></tui-amount-example-2> | ||
</tui-doc-example> | ||
|
||
<tui-doc-example | ||
id="options" | ||
heading="Options" | ||
[content]="example3" | ||
> | ||
<tui-amount-example-3></tui-amount-example-3> | ||
</tui-doc-example> | ||
</ng-template> | ||
|
||
<ng-template pageTab="Setup"> | ||
<ol class="b-demo-steps"> | ||
<li> | ||
<p>Import into a module</p> | ||
|
||
<tui-doc-code | ||
filename="myComponent.module.ts" | ||
[code]="exampleModule" | ||
></tui-doc-code> | ||
</li> | ||
|
||
<li> | ||
<p>Add to the template:</p> | ||
|
||
<tui-doc-code | ||
filename="myComponent.template.html" | ||
[code]="exampleHtml" | ||
></tui-doc-code> | ||
</li> | ||
</ol> | ||
</ng-template> | ||
</tui-doc-page> |
14 changes: 14 additions & 0 deletions
14
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 |
---|---|---|
@@ -0,0 +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> | ||
</ol> | ||
|
||
<hr class="tui-space_top-1 tui-space_bottom-1" /> | ||
|
||
<span>Remaining {{ 10728.9 | tuiAmount }} of {{ 11000 | tuiAmount }}</span> |
11 changes: 11 additions & 0 deletions
11
projects/demo/src/modules/experimental/amount/examples/1/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,11 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
|
||
@Component({ | ||
selector: 'tui-amount-example-1', | ||
templateUrl: './index.html', | ||
changeDetection, | ||
encapsulation, | ||
}) | ||
export class TuiAmountExample1 {} |
2 changes: 2 additions & 0 deletions
2
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<div>{{ -10000000.536 | tuiAmount: 'USD' : 'left' }}</div> | ||
<div>{{ 200.211 | tuiAmount: 'EUR' }}</div> |
19 changes: 19 additions & 0 deletions
19
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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 {} |
2 changes: 2 additions & 0 deletions
2
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<div>{{ -12.3 | tuiAmount: 'USD' : 'left' }}</div> | ||
<div>{{ 3000 | tuiAmount: 'EUR' }}</div> |
18 changes: 18 additions & 0 deletions
18
projects/demo/src/modules/experimental/amount/examples/3/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,18 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
import {tuiAmountOptionsProvider} from '@taiga-ui/experimental'; | ||
|
||
@Component({ | ||
selector: 'tui-amount-example-3', | ||
templateUrl: './index.html', | ||
changeDetection, | ||
encapsulation, | ||
providers: [ | ||
tuiAmountOptionsProvider({ | ||
sign: 'always', | ||
decimal: 'always', | ||
}), | ||
], | ||
}) | ||
export class TuiAmountExample3 {} |
2 changes: 2 additions & 0 deletions
2
projects/demo/src/modules/experimental/amount/examples/4/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,2 @@ | ||
<div>{{ -12.3 | tuiAmount: 'USD' : 'left' }}</div> | ||
<div>{{ 3000 | tuiAmount: 'EUR' }}</div> |
18 changes: 18 additions & 0 deletions
18
projects/demo/src/modules/experimental/amount/examples/4/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,18 @@ | ||
import {Component} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
import {tuiAmountOptionsProvider} from '@taiga-ui/experimental'; | ||
|
||
@Component({ | ||
selector: 'tui-amount-example-4', | ||
templateUrl: './index.html', | ||
changeDetection, | ||
encapsulation, | ||
providers: [ | ||
tuiAmountOptionsProvider({ | ||
sign: 'always', | ||
decimal: 'always', | ||
}), | ||
], | ||
}) | ||
export class TuiAmountExample4 {} |
13 changes: 13 additions & 0 deletions
13
projects/demo/src/modules/experimental/amount/examples/import/import-module.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```ts | ||
import {TuiAmountPipeModule} from '@taiga-ui/experimental'; | ||
|
||
// ... | ||
|
||
@NgModule({ | ||
imports: [ | ||
// ... | ||
TuiAmountPipeModule, | ||
], | ||
}) | ||
export class MyModule {} | ||
``` |
3 changes: 3 additions & 0 deletions
3
projects/demo/src/modules/experimental/amount/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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```html | ||
{{ 100000 | tuiAmount : currency : align}} | ||
``` |
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,11 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Provider} from '@angular/core'; | ||
import {TuiCurrencyVariants, TuiMoneySign} from '@taiga-ui/addon-commerce'; | ||
import {tuiCreateToken, tuiProvideOptions} from '@taiga-ui/cdk'; | ||
import {TuiDecimal, TuiHorizontalDirection} from '@taiga-ui/core'; | ||
|
||
export interface TuiAmountOptions { | ||
readonly currency: TuiCurrencyVariants; | ||
readonly currencyAlign: TuiHorizontalDirection; | ||
readonly decimal: TuiDecimal; | ||
readonly precision: number; | ||
readonly sign: TuiMoneySign; | ||
} | ||
|
||
export const TUI_AMOUNT_DEFAULT_OPTIONS: TuiAmountOptions = { | ||
decimal: `not-zero`, | ||
currency: null, | ||
currencyAlign: `right`, | ||
sign: `negative-only`, | ||
precision: 2, | ||
}; | ||
|
||
export const TUI_AMOUNT_OPTIONS = tuiCreateToken(TUI_AMOUNT_DEFAULT_OPTIONS); | ||
|
||
export function tuiAmountOptionsProvider(options: Partial<TuiAmountOptions>): Provider { | ||
return tuiProvideOptions(TUI_AMOUNT_OPTIONS, options, TUI_AMOUNT_DEFAULT_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Inject, Pipe, PipeTransform} from '@angular/core'; | ||
import {tuiFormatCurrency, tuiFormatSignSymbol} from '@taiga-ui/addon-commerce'; | ||
import { | ||
TUI_NUMBER_FORMAT, | ||
tuiFormatNumber, | ||
TuiNumberFormatSettings, | ||
} from '@taiga-ui/core'; | ||
|
||
import {TUI_AMOUNT_OPTIONS, TuiAmountOptions} from './amount.options'; | ||
|
||
@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, | ||
) {} | ||
|
||
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` ? ` ` : ``; | ||
|
||
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; | ||
} | ||
} |
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,3 @@ | ||
export * from './amount.module'; | ||
export * from './amount.options'; | ||
export * from './amount.pipe'; |
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 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts", | ||
"styleIncludePaths": [ | ||
"../../../core/styles" | ||
] | ||
} | ||
} |
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,4 @@ | ||
export * from '@taiga-ui/experimental/pipes/amount'; | ||
export * from '@taiga-ui/experimental/pipes/auto-color'; | ||
export * from '@taiga-ui/experimental/pipes/fallback-src'; | ||
export * from '@taiga-ui/experimental/pipes/initials'; |