generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add CVA and pipe (#187)
Co-authored-by: splincode <> Co-authored-by: Nikita Barsukov <nikita.s.barsukov@gmail.com>
- Loading branch information
1 parent
7376b2e
commit a099257
Showing
19 changed files
with
204 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './lib/maskito.cva'; | ||
export * from './lib/maskito.directive'; | ||
export * from './lib/maskito.module'; | ||
export * from './lib/maskito.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,28 @@ | ||
import {Directive, Input} from '@angular/core'; | ||
import {DefaultValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; | ||
import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskito/core'; | ||
|
||
@Directive({ | ||
selector: 'input[maskito], textarea[maskito]', | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
multi: true, | ||
useExisting: MaskitoCva, | ||
}, | ||
], | ||
host: { | ||
'(input)': '$any(this)._handleInput($event.target.value)', | ||
'(blur)': 'onTouched()', | ||
'(compositionstart)': '$any(this)._compositionStart()', | ||
'(compositionend)': '$any(this)._compositionEnd($event.target.value)', | ||
}, | ||
}) | ||
export class MaskitoCva extends DefaultValueAccessor { | ||
@Input() | ||
maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; | ||
|
||
override writeValue(value: unknown): void { | ||
super.writeValue(maskitoTransform(String(value ?? ''), this.maskito)); | ||
} | ||
} |
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,9 +1,11 @@ | ||
import {NgModule} from '@angular/core'; | ||
|
||
import {MaskitoCva} from './maskito.cva'; | ||
import {MaskitoDirective} from './maskito.directive'; | ||
import {MaskitoPipe} from './maskito.pipe'; | ||
|
||
@NgModule({ | ||
declarations: [MaskitoDirective], | ||
exports: [MaskitoDirective], | ||
declarations: [MaskitoDirective, MaskitoCva, MaskitoPipe], | ||
exports: [MaskitoDirective, MaskitoCva, MaskitoPipe], | ||
}) | ||
export class MaskitoModule {} |
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 {Pipe, PipeTransform} from '@angular/core'; | ||
import {MaskitoOptions, maskitoTransform} from '@maskito/core'; | ||
|
||
@Pipe({ | ||
name: 'maskito', | ||
}) | ||
export class MaskitoPipe implements PipeTransform { | ||
transform(value: unknown, maskitoOptions: MaskitoOptions): string { | ||
return maskitoTransform(String(value ?? ''), maskitoOptions); | ||
} | ||
} |
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,57 @@ | ||
import {Component} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {FormControl, ReactiveFormsModule} from '@angular/forms'; | ||
import {MaskitoModule} from '@maskito/angular'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
describe(`Maskito Angular package`, () => { | ||
@Component({ | ||
template: ` | ||
<div id="pipe">{{ control.value | maskito: options }}</div> | ||
<input | ||
id="input" | ||
[formControl]="control" | ||
[maskito]="options" | ||
/> | ||
`, | ||
}) | ||
class TestComponent { | ||
readonly control = new FormControl(); | ||
readonly options = maskitoNumberOptionsGenerator({precision: 2}); | ||
} | ||
|
||
let fixture: ComponentFixture<TestComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [MaskitoModule, ReactiveFormsModule], | ||
declarations: [TestComponent], | ||
}); | ||
|
||
fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it(`Null is treated as empty string`, () => { | ||
expect(getText()).toBe(``); | ||
expect(getValue()).toBe(``); | ||
}); | ||
|
||
it(`Formats new control value`, () => { | ||
fixture.componentInstance.control.setValue(12345.67); | ||
fixture.detectChanges(); | ||
|
||
expect(getText()).toBe(`12\u00A0345.67`); | ||
expect(getValue()).toBe(`12\u00A0345.67`); | ||
}); | ||
|
||
function getText(): string { | ||
return fixture.debugElement.nativeElement | ||
.querySelector('#pipe') | ||
.textContent.trim(); | ||
} | ||
|
||
function getValue(): string { | ||
return fixture.debugElement.nativeElement.querySelector('#input').value; | ||
} | ||
}); |
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
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: 18 additions & 0 deletions
18
projects/demo/src/pages/documentation/angular/examples/3-cva/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,18 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {FormControl} from '@angular/forms'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
@Component({ | ||
selector: 'cva-doc-example-3', | ||
templateUrl: './template.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CvaDocExample3 { | ||
readonly control = new FormControl(); | ||
|
||
readonly maskito = maskitoNumberOptionsGenerator({precision: 2}); | ||
|
||
setValue(): void { | ||
this.control.setValue(12345.67); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
projects/demo/src/pages/documentation/angular/examples/3-cva/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,5 @@ | ||
<input | ||
[formControl]="control" | ||
[maskito]="maskito" | ||
/> | ||
<button (click)="setValue()">Set 12345.67</button> |
13 changes: 13 additions & 0 deletions
13
projects/demo/src/pages/documentation/angular/examples/4-pipe/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,13 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
@Component({ | ||
selector: 'pipe-doc-example-4', | ||
templateUrl: './template.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PipeDocExample4 { | ||
value = 12345.67; | ||
|
||
readonly options = maskitoNumberOptionsGenerator({precision: 2}); | ||
} |
1 change: 1 addition & 0 deletions
1
projects/demo/src/pages/documentation/angular/examples/4-pipe/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 @@ | ||
Balance: ${{ value | maskito: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,7 +166,7 @@ | |
|
||
<p> | ||
<strong>Default:</strong> | ||
comma. | ||
dot. | ||
</p> | ||
</ng-template> | ||
<ng-template | ||
|
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
23 changes: 9 additions & 14 deletions
23
projects/kit/src/lib/masks/number/utils/get-default-pseudo-separators.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,15 +1,10 @@ | ||
export function getDefaultPseudoSeparators({ | ||
decimalSeparator, | ||
thousandSeparator, | ||
}: { | ||
decimalSeparator: string; | ||
thousandSeparator: string; | ||
}): string[] { | ||
if (decimalSeparator === ',' || decimalSeparator === '.') { | ||
return ['.', ',', 'б', 'ю'].filter( | ||
char => char !== thousandSeparator && char !== decimalSeparator, | ||
); | ||
} | ||
|
||
return []; | ||
export function getDefaultPseudoSeparators( | ||
decimalSeparator: string, | ||
thousandSeparator: string, | ||
): string[] { | ||
return decimalSeparator === ',' || decimalSeparator === '.' | ||
? ['.', ',', 'б', 'ю'].filter( | ||
char => char !== thousandSeparator && char !== decimalSeparator, | ||
) | ||
: []; | ||
} |