generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kit):
Number
has problems with run-time updates of postfix (#380)
- Loading branch information
1 parent
d904842
commit 8210896
Showing
9 changed files
with
179 additions
and
11 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
51 changes: 51 additions & 0 deletions
51
projects/demo/src/pages/cypress/examples/4-runtime-postfix-changes/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,51 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
ElementRef, | ||
Pipe, | ||
PipeTransform, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import {MaskitoOptions} from '@maskito/core'; | ||
import {maskitoNumberOptionsGenerator, maskitoParseNumber} from '@maskito/kit'; | ||
|
||
@Pipe({ | ||
name: 'calculateMask', | ||
}) | ||
export class TestPipe4 implements PipeTransform { | ||
transform(postfix: string): MaskitoOptions { | ||
return maskitoNumberOptionsGenerator({ | ||
postfix, | ||
precision: 2, | ||
thousandSeparator: ' ', | ||
}); | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'test-doc-example-4', | ||
template: ` | ||
<input | ||
#inputRef | ||
placeholder="Enter number" | ||
value="1 year" | ||
[maskito]="parsedValue | i18nPlural : pluralize | calculateMask" | ||
/> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class TestDocExample4 { | ||
@ViewChild('inputRef', {read: ElementRef, static: true}) | ||
readonly inputRef!: ElementRef<HTMLInputElement>; | ||
|
||
get parsedValue(): number { | ||
return maskitoParseNumber(this.inputRef.nativeElement.value); | ||
} | ||
|
||
readonly pluralize = { | ||
one: ` year`, | ||
few: ` years`, | ||
many: ` years`, | ||
other: ` years`, | ||
}; | ||
} |
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
50 changes: 50 additions & 0 deletions
50
projects/kit/src/lib/masks/number/processors/initialization-only-preprocessor.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,50 @@ | ||
import {MaskitoPreprocessor, maskitoTransform} from '@maskito/core'; | ||
|
||
import {generateMaskExpression} from '../utils'; | ||
|
||
/** | ||
* This preprocessor works only once at initialization phase (when `new Maskito(...)` is executed). | ||
* This preprocessor helps to avoid conflicts during transition from one mask to another (for the same input). | ||
* For example, the developer changes postfix (or other mask's props) during run-time. | ||
* ``` | ||
* let maskitoOptions = maskitoNumberOptionsGenerator({postfix: ' year'}); | ||
* // [3 seconds later] | ||
* maskitoOptions = maskitoNumberOptionsGenerator({postfix: ' years'}); | ||
* ``` | ||
*/ | ||
export function createInitializationOnlyPreprocessor({ | ||
decimalSeparator, | ||
decimalPseudoSeparators, | ||
pseudoMinuses, | ||
}: { | ||
decimalSeparator: string; | ||
decimalPseudoSeparators: readonly string[]; | ||
pseudoMinuses: readonly string[]; | ||
}): MaskitoPreprocessor { | ||
let isInitializationPhase = true; | ||
const cleanNumberMask = generateMaskExpression({ | ||
decimalSeparator, | ||
decimalPseudoSeparators, | ||
pseudoMinuses, | ||
prefix: '', | ||
postfix: '', | ||
thousandSeparator: '', | ||
precision: Infinity, | ||
isNegativeAllowed: true, | ||
}); | ||
|
||
return ({elementState, data}) => { | ||
if (!isInitializationPhase) { | ||
return {elementState, data}; | ||
} | ||
|
||
isInitializationPhase = false; | ||
|
||
return { | ||
elementState: maskitoTransform(elementState, { | ||
mask: cleanNumberMask, | ||
}), | ||
data, | ||
}; | ||
}; | ||
} |
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