generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 36
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
maskitoScrollPlugin
- Loading branch information
1 parent
1c31456
commit ea7c22a
Showing
7 changed files
with
149 additions
and
0 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,3 +1,4 @@ | ||
export * from './change-event-plugin'; | ||
export * from './initial-calibration-plugin'; | ||
export * from './scroll-plugin'; | ||
export * from './strict-composition-plugin'; |
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,49 @@ | ||
import type {MaskitoPlugin} from '../types'; | ||
|
||
/** | ||
* Helper to scroll narrow textfield after programmatic updates of masked element. | ||
* It is rather hacky solution – that's why it should be enabled manually at your own risk. | ||
* ___ | ||
* There are 2 issues solved by this plugin: | ||
* | ||
* - Case 1. Programmatic update of `element.value` | ||
* DOM contains already focused <input />. | ||
* It is narrow (only 3 characters can be fit inside it without scroll). | ||
* ``` | ||
* const element = document.querySelector('input'); | ||
* | ||
* element.value = '12345'; | ||
* // caret is placed after the last digit, but it is not visible (no scroll happens) | ||
* ``` | ||
* | ||
* - Case 2. Programmatic execution of `element.setSelectionRange` | ||
* DOM contains already focused <input value="12345" />. | ||
* It is narrow (only 3 characters can be fit inside it without scroll). | ||
* Caret is placed at the beginning. | ||
* ``` | ||
* const element = document.querySelector('input'); | ||
* | ||
* input.setSelectionRange(5, 5); | ||
* // caret is placed after the last digit, but it is not visible (no scroll happens) | ||
* ``` | ||
* ___ | ||
* - {@link https://issues.chromium.org/issues/41081857 Open Chromium bug from 2014} | ||
*/ | ||
export function maskitoScrollPlugin(): MaskitoPlugin { | ||
return (element) => { | ||
const document = element.ownerDocument; | ||
|
||
const listener = (): void => { | ||
if (element.matches(':focus') && element.scrollWidth > element.clientWidth) { | ||
element.blur(); | ||
element.focus(); | ||
} | ||
}; | ||
|
||
document.addEventListener('selectionchange', listener, {passive: true}); | ||
|
||
return () => { | ||
document.removeEventListener('selectionchange', listener); | ||
}; | ||
}; | ||
} |
31 changes: 31 additions & 0 deletions
31
projects/demo/src/pages/documentation/plugins/examples/5-scroll/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,31 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {FormsModule} from '@angular/forms'; | ||
import {MaskitoDirective} from '@maskito/angular'; | ||
import {TuiTextfield} from '@taiga-ui/core'; | ||
|
||
import mask from './mask'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'plugins-scroll-doc-example-5', | ||
imports: [FormsModule, MaskitoDirective, TuiTextfield], | ||
template: ` | ||
<tui-textfield | ||
[style.max-width.rem]="5" | ||
[tuiTextfieldCleaner]="false" | ||
> | ||
<label tuiLabel>Price</label> | ||
<input | ||
tuiTextfield | ||
[maskito]="maskitoOptions" | ||
[(ngModel)]="value" | ||
/> | ||
</tui-textfield> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PluginsDocExample5 { | ||
protected readonly maskitoOptions = mask; | ||
protected value = '$1 234'; | ||
} |
16 changes: 16 additions & 0 deletions
16
projects/demo/src/pages/documentation/plugins/examples/5-scroll/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,16 @@ | ||
import type {MaskitoOptions} from '@maskito/core'; | ||
import {maskitoScrollPlugin} from '@maskito/core'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
const numberOptions = maskitoNumberOptionsGenerator({ | ||
precision: 2, | ||
prefix: '$', | ||
}); | ||
|
||
export default { | ||
...numberOptions, | ||
plugins: [ | ||
...numberOptions.plugins, | ||
maskitoScrollPlugin(), // <--- Enable it | ||
], | ||
} satisfies 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