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(core): add
.select()
-method support for MaskitoElement
- Loading branch information
1 parent
50c58ab
commit 497ad5e
Showing
3 changed files
with
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
export type TextfieldLike = Pick< | ||
HTMLInputElement, | ||
'maxLength' | 'selectionEnd' | 'selectionStart' | 'setSelectionRange' | 'value' | ||
| 'maxLength' | ||
| 'select' | ||
| 'selectionEnd' | ||
| 'selectionStart' | ||
| 'setSelectionRange' | ||
| 'value' | ||
>; | ||
export type MaskitoElement = HTMLElement & TextfieldLike; |
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
58 changes: 58 additions & 0 deletions
58
...-integrations/src/tests/component-testing/native-select-method/native-select-method.cy.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,58 @@ | ||
import {Component} from '@angular/core'; | ||
import {MaskitoDirective} from '@maskito/angular'; | ||
import type {MaskitoOptions} from '@maskito/core'; | ||
import {maskitoEventHandler} from '@maskito/kit'; | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [MaskitoDirective], | ||
template: ` | ||
<input | ||
value="123" | ||
[maskito]="maskitoOptions" | ||
/> | ||
<textarea [maskito]="maskitoOptions">123</textarea> | ||
<div | ||
contenteditable="true" | ||
[maskito]="maskitoOptions" | ||
[textContent]="'123'" | ||
></div> | ||
`, | ||
}) | ||
export class TestComponent { | ||
protected readonly maskitoOptions: MaskitoOptions = { | ||
mask: /^\d+$/, | ||
plugins: [ | ||
maskitoEventHandler('focus', element => element.select(), {once: true}), | ||
], | ||
}; | ||
} | ||
|
||
describe('Native method `.select()` works', () => { | ||
['input', 'textarea'].forEach(selector => { | ||
it(`for <${selector} />`, () => { | ||
cy.mount(TestComponent); | ||
cy.get(selector) | ||
.should('have.value', '123') | ||
.should('have.prop', 'selectionStart', 0) | ||
.should('have.prop', 'selectionEnd', 0) | ||
.should('not.be.focused') | ||
.focus() | ||
.should('have.prop', 'selectionStart', 0) | ||
.should('have.prop', 'selectionEnd', 3); | ||
}); | ||
}); | ||
|
||
it('for [contenteditable]', () => { | ||
cy.mount(TestComponent); | ||
cy.get('[contenteditable]') | ||
.should('have.text', '123') | ||
.should('not.be.focused') | ||
.focus() | ||
.type('0') // all selected value will be overwritten | ||
.should('have.text', '0') | ||
.focus() | ||
.type('2') // no selection (plugin works only for the first focus), just append value | ||
.should('have.text', '02'); | ||
}); | ||
}); |