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
maskitoChangeEventPlugin
(#1338)
- Loading branch information
1 parent
7d7e6f9
commit daa04d2
Showing
12 changed files
with
204 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type {MaskitoPlugin} from '../types'; | ||
|
||
export function maskitoChangeEventPlugin(): MaskitoPlugin { | ||
return element => { | ||
if (element.isContentEditable) { | ||
return; | ||
} | ||
|
||
let value = element.value; | ||
|
||
const valueListener = (): void => { | ||
value = element.value; | ||
}; | ||
const blurListener = (): void => { | ||
if (element.value !== value) { | ||
element.dispatchEvent(new Event('change', {bubbles: true})); | ||
} | ||
}; | ||
|
||
element.addEventListener('focus', valueListener); | ||
element.addEventListener('change', valueListener); | ||
element.addEventListener('blur', blurListener); | ||
|
||
return () => { | ||
element.removeEventListener('focus', valueListener); | ||
element.removeEventListener('change', valueListener); | ||
element.removeEventListener('blur', blurListener); | ||
}; | ||
}; | ||
} |
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,3 @@ | ||
export * from './change-event-plugin'; | ||
export * from './initial-calibration-plugin'; | ||
export * from './strict-composition-plugin'; |
3 changes: 1 addition & 2 deletions
3
...c/lib/utils/initial-calibration-plugin.ts → ...lib/plugins/initial-calibration-plugin.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
4 changes: 1 addition & 3 deletions
4
...rc/lib/utils/strict-composition-plugin.ts → .../lib/plugins/strict-composition-plugin.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
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
62 changes: 62 additions & 0 deletions
62
...mo-integrations/src/tests/component-testing/change-event-plugin/change-event-plugin.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,62 @@ | ||
import type {MaskitoOptions} from '@maskito/core'; | ||
import {maskitoChangeEventPlugin} from '@maskito/core'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
import {createOutputSpy} from 'cypress/angular'; | ||
|
||
import {TestInput} from '../utils'; | ||
|
||
describe('maskitoChangeEventPlugin', () => { | ||
const numberMask = maskitoNumberOptionsGenerator({ | ||
thousandSeparator: ' ', | ||
decimalSeparator: '.', | ||
precision: 2, | ||
}); | ||
const maskitoOptions: MaskitoOptions = { | ||
...numberMask, | ||
plugins: [...numberMask.plugins, maskitoChangeEventPlugin()], | ||
}; | ||
|
||
beforeEach(() => { | ||
cy.mount(TestInput, { | ||
componentProperties: { | ||
maskitoOptions, | ||
change: createOutputSpy('changeEvent'), | ||
}, | ||
}); | ||
}); | ||
|
||
it('Enter only valid value (Maskito does not prevent any typed character) => only 1 change event on blur', () => { | ||
cy.get('input').type('123').should('have.value', '123'); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
cy.get('input').blur(); | ||
cy.get('@changeEvent').should('have.callCount', 1); | ||
}); | ||
|
||
it('Enter valid value + pseudo decimal separator (Maskito replaces pseudo separator with valid one) => only 1 change event on blur', () => { | ||
cy.get('input').type('123,').should('have.value', '123.'); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
cy.get('input').blur(); | ||
cy.get('@changeEvent').should('have.callCount', 1); | ||
}); | ||
|
||
it('Enter only decimal separator (Maskito pads it with zero) => only 1 change event on blur', () => { | ||
cy.get('input').type('.').should('have.value', '0.'); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
cy.get('input').blur(); | ||
cy.get('@changeEvent').should('have.callCount', 1); | ||
}); | ||
|
||
it('Enter only invalid value (Maskito rejects all typed characters) => no change event', () => { | ||
cy.get('input').type('abc').should('have.value', ''); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
cy.get('input').blur(); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
}); | ||
|
||
it('Enter any value value and then erase it again => no change event', () => { | ||
cy.get('input').type('123').should('have.value', '123'); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
cy.get('input').clear().blur(); | ||
cy.get('@changeEvent').should('not.be.called'); | ||
}); | ||
}); |
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
36 changes: 36 additions & 0 deletions
36
projects/demo/src/pages/documentation/plugins/examples/4-change-event/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,36 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {FormsModule} from '@angular/forms'; | ||
import {MaskitoDirective} from '@maskito/angular'; | ||
import {TuiInputModule} from '@taiga-ui/kit'; | ||
|
||
import mask from './mask'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'plugins-change-event-doc-example-4', | ||
imports: [FormsModule, MaskitoDirective, TuiInputModule], | ||
template: ` | ||
<tui-input | ||
[maskito]="maskitoOptions" | ||
[style.max-width.rem]="20" | ||
[(ngModel)]="value" | ||
> | ||
Enter number | ||
<input | ||
tuiTextfield | ||
[maskito]="maskitoOptions" | ||
(change)="log($event)" | ||
/> | ||
</tui-input> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PluginsDocExample4 { | ||
protected readonly maskitoOptions = mask; | ||
protected value = ''; | ||
|
||
protected log(anything: any): void { | ||
console.info(anything); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
projects/demo/src/pages/documentation/plugins/examples/4-change-event/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,15 @@ | ||
import type {MaskitoOptions} from '@maskito/core'; | ||
import {maskitoChangeEventPlugin} from '@maskito/core'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
const numberOptions = maskitoNumberOptionsGenerator({ | ||
precision: 2, | ||
}); | ||
|
||
export default { | ||
...numberOptions, | ||
plugins: [ | ||
...numberOptions.plugins, | ||
maskitoChangeEventPlugin(), // <--- Enable it | ||
], | ||
} 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