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.
feat(core): new built-in
maskitoStrictCompositionPlugin
(#881)
- Loading branch information
1 parent
c56b4ea
commit 74dddd5
Showing
8 changed files
with
112 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
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,33 @@ | ||
import {ElementState, MaskitoPlugin, TypedInputEvent} from '../types'; | ||
import {maskitoUpdateElement} from './dom/update-element'; | ||
import {areElementStatesEqual} from './element-states-equality'; | ||
import {maskitoTransform} from './transform'; | ||
|
||
export function maskitoStrictCompositionPlugin(): MaskitoPlugin { | ||
return (element, maskitoOptions) => { | ||
const listener = (event: TypedInputEvent): void => { | ||
if (event.inputType !== 'insertCompositionText') { | ||
return; | ||
} | ||
|
||
const selection = [ | ||
element.selectionStart || 0, | ||
element.selectionEnd || 0, | ||
] as const; | ||
const elementState: ElementState = { | ||
selection, | ||
value: element.value, | ||
}; | ||
const validatedState = maskitoTransform(elementState, maskitoOptions); | ||
|
||
if (!areElementStatesEqual(elementState, validatedState)) { | ||
event.preventDefault(); | ||
maskitoUpdateElement(element, validatedState); | ||
} | ||
}; | ||
|
||
element.addEventListener('input', listener as EventListener); | ||
|
||
return () => element.removeEventListener('input', listener as EventListener); | ||
}; | ||
} |
21 changes: 21 additions & 0 deletions
21
projects/demo/src/pages/documentation/plugins/examples/3-strict-composition/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,21 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
|
||
import mask from './mask'; | ||
|
||
@Component({ | ||
selector: 'plugins-strict-composition-doc-example-3', | ||
template: ` | ||
<tui-input | ||
[maskito]="maskitoOptions" | ||
[style.max-width.rem]="20" | ||
[(ngModel)]="value" | ||
> | ||
Enter number | ||
</tui-input> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PluginsDocExample3 { | ||
readonly maskitoOptions = mask; | ||
value = ''; | ||
} |
6 changes: 6 additions & 0 deletions
6
projects/demo/src/pages/documentation/plugins/examples/3-strict-composition/mask.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,6 @@ | ||
import {MaskitoOptions, maskitoStrictCompositionPlugin} from '@maskito/core'; | ||
|
||
export default { | ||
mask: /^[0-90-9]*$/, | ||
plugins: [maskitoStrictCompositionPlugin()], | ||
} as 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
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