Skip to content

Commit

Permalink
feat(core)!: merge MaskitoElementPredicate & `MaskitoElementPredica…
Browse files Browse the repository at this point in the history
…teAsync` into single type (#757)
  • Loading branch information
nsbarsukov authored Dec 12, 2023
1 parent 4495942 commit 640a631
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 39 deletions.
4 changes: 1 addition & 3 deletions projects/angular/src/lib/maskito.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MASKITO_DEFAULT_ELEMENT_PREDICATE,
MASKITO_DEFAULT_OPTIONS,
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoOptions,
} from '@maskito/core';

Expand All @@ -24,8 +23,7 @@ export class MaskitoDirective implements OnDestroy, OnChanges {
maskito: MaskitoOptions | null = MASKITO_DEFAULT_OPTIONS;

@Input()
maskitoElement: MaskitoElementPredicate | MaskitoElementPredicateAsync =
MASKITO_DEFAULT_ELEMENT_PREDICATE;
maskitoElement: MaskitoElementPredicate = MASKITO_DEFAULT_ELEMENT_PREDICATE;

constructor(
@Inject(NgZone) private readonly ngZone: NgZone,
Expand Down
1 change: 0 additions & 1 deletion projects/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
export {Maskito} from './lib/mask';
export {
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoMask,
MaskitoMaskExpression,
MaskitoOptions,
Expand Down
3 changes: 2 additions & 1 deletion projects/core/src/lib/constants/default-element-predicate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {MaskitoElementPredicate} from '../types';

export const MASKITO_DEFAULT_ELEMENT_PREDICATE: MaskitoElementPredicate = e =>
e.querySelector('input,textarea') || (e as HTMLInputElement | HTMLTextAreaElement);
e.querySelector<HTMLInputElement | HTMLTextAreaElement>('input,textarea') ||
(e as HTMLInputElement | HTMLTextAreaElement);
10 changes: 4 additions & 6 deletions projects/core/src/lib/types/element-predicate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export type MaskitoElementPredicate = (
element: HTMLElement,
) => HTMLInputElement | HTMLTextAreaElement; // TODO: add `Promise<HTMLInputElement | HTMLTextAreaElement>`

// TODO: delete in v2.0
export type MaskitoElementPredicateAsync = (
element: HTMLElement,
) => Promise<HTMLInputElement | HTMLTextAreaElement>;
) =>
| HTMLInputElement
| HTMLTextAreaElement
| Promise<HTMLInputElement | HTMLTextAreaElement>;
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoOptions,
} from '@maskito/core';
import {MaskitoElementPredicate, MaskitoOptions} from '@maskito/core';

@Component({
selector: 'test-doc-example-1',
Expand Down Expand Up @@ -41,6 +37,6 @@ export class TestDocExample1 {
readonly namePredicate: MaskitoElementPredicate = element =>
element.querySelectorAll('input')[1]!;

readonly asyncPredicate: MaskitoElementPredicateAsync = async element =>
readonly asyncPredicate: MaskitoElementPredicate = async element =>
Promise.resolve(element.querySelectorAll('input')[0]!);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-nocheck React & Vue Global JSX Types Conflicts
// TODO: Check if it still required after upgrade Vue to 3.4 (https://github.com/vuejs/core/pull/7958)
import type {MaskitoElementPredicateAsync} from '@maskito/core';
import {MaskitoElementPredicate} from '@maskito/core';
import {maskitoTimeOptionsGenerator} from '@maskito/kit';
import {useMaskito} from '@maskito/react';
Expand All @@ -13,19 +12,19 @@ const options = maskitoTimeOptionsGenerator({
const correctPredicate: MaskitoElementPredicate = host => host.querySelector('.real-input')!;
const wrongPredicate: MaskitoElementPredicate = host => host.querySelector('input')!;

const longCorrectPredicate: MaskitoElementPredicateAsync = host =>
const longCorrectPredicate: MaskitoElementPredicate = host =>
new Promise(resolve => {
setTimeout(() => {
resolve(correctPredicate(host));
}, 2_000);
});

const longInvalidPredicate: MaskitoElementPredicateAsync = host =>
const longInvalidPredicate: MaskitoElementPredicate = host =>
new Promise(resolve => {
setTimeout(() => resolve(wrongPredicate(host)), 7_000);
});

const fastValidPredicate: MaskitoElementPredicateAsync = host =>
const fastValidPredicate: MaskitoElementPredicate = host =>
new Promise(resolve => {
setTimeout(() => resolve(correctPredicate(host)), 500);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export class NestedDocExample1 {
};

readonly predicate: MaskitoElementPredicate = element =>
element.querySelector('tui-input input')!;
element.querySelector<HTMLInputElement>('tui-input input')!;
}
11 changes: 3 additions & 8 deletions projects/react/src/lib/tests/elementPredicate.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
MASKITO_DEFAULT_ELEMENT_PREDICATE,
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoOptions,
} from '@maskito/core';
import {MASKITO_DEFAULT_ELEMENT_PREDICATE, MaskitoElementPredicate, MaskitoOptions} from '@maskito/core';
import {render, RenderResult, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

Expand All @@ -13,9 +8,9 @@ describe('@maskito/react | `elementPredicate` property', () => {
const options: MaskitoOptions = {
mask: /^\d+$/,
};
let predicate: MaskitoElementPredicate | MaskitoElementPredicateAsync = MASKITO_DEFAULT_ELEMENT_PREDICATE;
let predicate: MaskitoElementPredicate = MASKITO_DEFAULT_ELEMENT_PREDICATE;

const correctPredicate: MaskitoElementPredicate = host => host.querySelector('.real-input')!;
const correctPredicate: MaskitoElementPredicate = host => host.querySelector<HTMLInputElement>('.real-input')!;
const wrongPredicate: MaskitoElementPredicate = host => host.querySelector('input')!;

function TestComponent({elementPredicate = predicate}) {
Expand Down
3 changes: 1 addition & 2 deletions projects/react/src/lib/useMaskito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
MASKITO_DEFAULT_ELEMENT_PREDICATE,
MASKITO_DEFAULT_OPTIONS,
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoOptions,
} from '@maskito/core';
import {RefCallback, useCallback, useRef, useState} from 'react';
Expand Down Expand Up @@ -33,7 +32,7 @@ export const useMaskito = ({
elementPredicate = MASKITO_DEFAULT_ELEMENT_PREDICATE,
}: {
options?: MaskitoOptions;
elementPredicate?: MaskitoElementPredicate | MaskitoElementPredicateAsync;
elementPredicate?: MaskitoElementPredicate;
} = {}): RefCallback<HTMLElement> => {
const [hostElement, setHostElement] = useState<HTMLElement | null>(null);
const [element, setElement] = useState<HTMLInputElement | HTMLTextAreaElement | null>(
Expand Down
10 changes: 3 additions & 7 deletions projects/vue/src/lib/maskito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import {
Maskito,
MASKITO_DEFAULT_ELEMENT_PREDICATE,
MaskitoElementPredicate,
MaskitoElementPredicateAsync,
MaskitoOptions,
} from '@maskito/core';
import {ObjectDirective} from 'vue';

const teardown = new Map<HTMLElement, Maskito>();
const predicates = new Map<
HTMLElement,
MaskitoElementPredicate | MaskitoElementPredicateAsync
>();
const predicates = new Map<HTMLElement, MaskitoElementPredicate>();

async function update(
element: HTMLElement,
options: MaskitoOptions & {
elementPredicate?: MaskitoElementPredicate | MaskitoElementPredicateAsync;
elementPredicate?: MaskitoElementPredicate;
},
): Promise<void> {
const predicate = options.elementPredicate ?? MASKITO_DEFAULT_ELEMENT_PREDICATE;
Expand All @@ -36,7 +32,7 @@ async function update(
export const maskito: ObjectDirective<
HTMLElement,
MaskitoOptions & {
elementPredicate?: MaskitoElementPredicate | MaskitoElementPredicateAsync;
elementPredicate?: MaskitoElementPredicate;
}
> = {
unmounted: element => {
Expand Down

0 comments on commit 640a631

Please sign in to comment.