Skip to content

Commit e5944c5

Browse files
[UIK-5647][input-mask] rewrite component to NS (#3093)
## Changelog ### @semcore/input-mask #### Changed - Refactor component types. Deprecated atomic types. Atomic types are part of `NSInputMask` namespace. <!--- Provide a general summary of your changes in the Title above --> ## Motivation and Context Refactor component types. Deprecated atomic types in favor namespaces. Nothing changed in how component works. ## How has this been tested? <!--- Please describe in detail how you tested your changes. --> <!--- For example: --> <!--- I have added unit tests --> <!--- I have added Voice Over tests --> <!--- Code cannot be tested automatically so I have tested it only manually --> ## Screenshots (if appropriate): ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue). - [ ] New feature (non-breaking change which adds functionality). - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected). - [x] Nice improve. ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I have updated the documentation accordingly. - [ ] I have added new tests on added of fixed functionality. Co-authored-by: Valeria-Zimnitskaya <valeryia.zimnitskaya@semrush.com>
1 parent f064c6e commit e5944c5

6 files changed

Lines changed: 142 additions & 102 deletions

File tree

playground/entries/InputMask.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { NSInput } from '@semcore/ui/input';
2-
import type { InputMaskValueProps } from '@semcore/ui/input-mask';
2+
import type { NSInputMask } from '@semcore/ui/input-mask';
33
import InputMask from '@semcore/ui/input-mask';
44
import React from 'react';
55

66
import type { JSXProps } from '../types/JSXProps';
77
import type { PlaygroundEntry } from '../types/Playground';
88
import createGithubLink from '../utils/createGHLink';
99

10-
export type InputMaskJSXProps = JSXProps<NSInput.Props & InputMaskValueProps>;
10+
export type InputMaskJSXProps = JSXProps<NSInput.Props & NSInputMask.Value.Props>;
1111

1212
function getJSX(props: InputMaskJSXProps) {
1313
return (

semcore/date-picker/src/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type Divider from '@semcore/divider';
1010
import type { DropdownProps, DropdownTriggerProps } from '@semcore/dropdown';
1111
import type { NSInput } from '@semcore/input';
1212
import type Input from '@semcore/input';
13-
import type { InputMaskValueProps } from '@semcore/input-mask';
13+
import type { NSInputMask } from '@semcore/input-mask';
1414
import type { TooltipProps } from '@semcore/tooltip';
1515
import type dayjs from 'dayjs';
1616
import type { ChangeEvent } from 'react';
@@ -314,7 +314,7 @@ declare const InputTrigger: Intergalactic.Component<
314314
Value: typeof Input.Value;
315315
SingleDateInput: Intergalactic.Component<'div', NSInput.Props & SingleDateInputProps> & {
316316
Indicator: typeof Input.Addon;
317-
MaskedInput: Intergalactic.Component<'input', InputMaskValueProps & DatePickerMaskedInputProps>;
317+
MaskedInput: Intergalactic.Component<'input', NSInputMask.Value.Props & DatePickerMaskedInputProps>;
318318
};
319319
};
320320

@@ -329,11 +329,11 @@ declare const RangeInputTrigger: Intergalactic.Component<
329329
RangeSep: typeof Input.Addon;
330330
FromMaskedInput: Intergalactic.Component<
331331
'input',
332-
InputMaskValueProps & DatePickerMaskedInputProps
332+
NSInputMask.Value.Props & DatePickerMaskedInputProps
333333
>;
334334
ToMaskedInput: Intergalactic.Component<
335335
'input',
336-
InputMaskValueProps & DatePickerMaskedInputProps
336+
NSInputMask.Value.Props & DatePickerMaskedInputProps
337337
>;
338338
};
339339
};

semcore/input-mask/src/InputMask.tsx

Lines changed: 15 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { NeighborLocation, Box, Flex, ScreenReaderOnly } from '@semcore/base-components';
2-
import {
3-
createComponent,
4-
Component,
5-
sstyled,
6-
Root,
7-
type PropGetterFn,
8-
type Intergalactic,
9-
} from '@semcore/core';
2+
import type { Intergalactic } from '@semcore/core';
3+
import { createComponent, Component, sstyled, Root } from '@semcore/core';
104
import fire from '@semcore/core/lib/utils/fire';
115
import getInputProps, { inputProps } from '@semcore/core/lib/utils/inputProps';
126
import logger from '@semcore/core/lib/utils/logger';
@@ -16,80 +10,9 @@ import Input, { type NSInput } from '@semcore/input';
1610
import React from 'react';
1711
import * as mask from 'text-mask-core';
1812

13+
import type { NSInputMask } from './InputMask.type';
1914
import style from './style/input-mask.shadow.css';
2015

21-
export type IInputMaskAsFn = (rawValue?: string) => string | RegExp[];
22-
23-
export type InputMaskAliases = {
24-
[s: string]: RegExp;
25-
};
26-
27-
export type InputMaskValueProps = NSInput.Value.Props & {
28-
/**
29-
* Mask for entering text
30-
*/
31-
mask?: string | boolean | IInputMaskAsFn;
32-
/**
33-
* The property for visibility of the mask
34-
* @default false
35-
*/
36-
hideMask?: boolean;
37-
/**
38-
* This function allows you to change the input value before it is displayed on the screen.
39-
*/
40-
pipe?: (
41-
conformedValue: string,
42-
config: {
43-
rawValue: string;
44-
},
45-
) => string | false | { value: string; indexesOfPipedChars: number[] };
46-
/** Internal */
47-
keepCharPositions?: boolean;
48-
/**
49-
* The aliases object for the mask values. The key is the symbol used in the mask,
50-
* and the value is the regular expression that this symbol must match
51-
* @default {'9': /\d/, 'a': /[a-zA-Zа-яА-Я]/, '*': /[\da-zA-Zа-яА-Я]/}
52-
*/
53-
aliases?: InputMaskAliases;
54-
/**
55-
* Event that is called when the input value fully matches the mask
56-
*/
57-
onSuccess?: (value: string) => void;
58-
/**
59-
* A field that explains the mask for blind users
60-
* */
61-
title?: string;
62-
63-
/** Specifies which props to pass to input element */
64-
includeInputProps?: string[];
65-
66-
/**
67-
* Field for describe which symbols will use as mask
68-
* @default `{_: true}`
69-
*/
70-
maskOnlySymbols?: Record<string, boolean>;
71-
72-
/**
73-
* Overrids width of the input field
74-
*/
75-
inputW?: string | number;
76-
77-
/**
78-
* Aria role for input
79-
*/
80-
inputRole?: string;
81-
};
82-
83-
type InputMaskCtx = {
84-
getInputProps: PropGetterFn;
85-
getValueProps: PropGetterFn;
86-
};
87-
88-
type InputMaskComponent = Intergalactic.Component<'div', NSInput.Props, InputMaskCtx> & {
89-
Value: Intergalactic.Component<'input', InputMaskValueProps>;
90-
Addon: typeof Input.Addon;
91-
};
92-
9316
const { createTextMaskInputElement } = mask;
9417

9518
export function getAfterPositionValue(value: string, mask: any = ''): number {
@@ -106,7 +29,9 @@ export function getAfterPositionValue(value: string, mask: any = ''): number {
10629
return afterPotionValue;
10730
}
10831

109-
class InputMask extends Component<NSInput.Props> {
32+
class InputMask extends Component<
33+
Intergalactic.InternalTypings.InferComponentProps<NSInputMask.Component>
34+
> {
11035
static displayName = 'InputMask';
11136
static style = style;
11237

@@ -115,8 +40,12 @@ class InputMask extends Component<NSInput.Props> {
11540
}
11641
}
11742

118-
class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value: Array<(value?: string) => string> }> {
119-
static defaultProps = {
43+
class Value extends Component<
44+
Intergalactic.InternalTypings.InferComponentProps<NSInputMask.Value.Component>,
45+
typeof Value.enhance,
46+
NSInputMask.Value.Handlers
47+
> {
48+
static defaultProps: NSInputMask.Value.DefaultProps = {
12049
includeInputProps: inputProps,
12150
defaultValue: '',
12251
hideMask: false,
@@ -138,16 +67,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
13867
textMaskCoreInstance: any = undefined;
13968
usedMask: any = undefined;
14069
prevConfirmedValue: any = undefined;
141-
state: {
142-
lastConformed:
143-
| {
144-
all: string;
145-
userInput: string;
146-
maskOnly: string;
147-
}
148-
| undefined;
149-
maskWidth: number | undefined;
150-
} = {
70+
state: NSInputMask.Value.State = {
15171
lastConformed: undefined,
15272
maskWidth: undefined,
15373
};
@@ -159,7 +79,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
15979
});
16080
}
16181

162-
componentDidUpdate(prevProps: any) {
82+
componentDidUpdate(prevProps: typeof this.asProps) {
16383
const maskConfigProps = ['mask', 'hideMask', 'pipe', 'keepCharPositions'] as const;
16484
const maskConfigChanged = maskConfigProps.some(
16585
(prop) => this.asProps[prop] !== prevProps[prop],
@@ -403,7 +323,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
403323
*
404324
* {@link https://developer.semrush.com/intergalactic/components/input-mask/input-mask-api/|API} | {@link https://developer.semrush.com/intergalactic/components/input-mask/input-mask-code/|Examples}
405325
*/
406-
export default createComponent<InputMaskComponent, typeof InputMask>(InputMask, {
326+
export default createComponent<NSInputMask.Component, typeof InputMask>(InputMask, {
407327
Value,
408328
Addon: Input.Addon,
409329
});
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import type { PropGetterFn, Intergalactic } from '@semcore/core';
2+
import type { NSInput } from '@semcore/input';
3+
4+
declare namespace NSInputMask {
5+
type Props = NSInput.Props;
6+
type Ctx = {
7+
getInputProps: PropGetterFn;
8+
getValueProps: PropGetterFn;
9+
};
10+
11+
namespace Value {
12+
type Props = NSInput.Value.Props & {
13+
/**
14+
* Mask for entering text
15+
*/
16+
mask?: string | boolean | ((rawValue?: string) => string | RegExp[]);
17+
/**
18+
* The property for visibility of the mask
19+
* @default false
20+
*/
21+
hideMask?: boolean;
22+
/**
23+
* This function allows you to change the input value before it is displayed on the screen.
24+
*/
25+
pipe?: (
26+
conformedValue: string,
27+
config: {
28+
rawValue: string;
29+
},
30+
) => string | false | { value: string; indexesOfPipedChars: number[] };
31+
/** Internal */
32+
keepCharPositions?: boolean;
33+
/**
34+
* The aliases object for the mask values. The key is the symbol used in the mask,
35+
* and the value is the regular expression that this symbol must match
36+
* @default {'9': /\d/, 'a': /[a-zA-Zа-яА-Я]/, '*': /[\da-zA-Zа-яА-Я]/}
37+
*/
38+
aliases?: {
39+
[s: string]: RegExp;
40+
};
41+
/**
42+
* Event that is called when the input value fully matches the mask
43+
*/
44+
onSuccess?: (value: string) => void;
45+
/**
46+
* A field that explains the mask for blind users
47+
* */
48+
title?: string;
49+
50+
/** Specifies which props to pass to input element */
51+
includeInputProps?: string[];
52+
53+
/**
54+
* Field for describe which symbols will use as mask
55+
* @default `{_: true}`
56+
*/
57+
maskOnlySymbols?: Record<string, boolean>;
58+
59+
/**
60+
* Overrids width of the input field
61+
*/
62+
inputW?: string | number;
63+
64+
/**
65+
* Aria role for input
66+
*/
67+
inputRole?: string;
68+
};
69+
type DefaultProps = {
70+
includeInputProps: Array<string>;
71+
defaultValue: '';
72+
hideMask: false;
73+
keepCharPositions: false;
74+
aliases: {
75+
'9': RegExp;
76+
'a': RegExp;
77+
'*': RegExp;
78+
};
79+
maskOnlySymbols: {
80+
_: boolean;
81+
};
82+
};
83+
type Handlers = {
84+
value: Array<(value?: string) => string>;
85+
};
86+
type State = {
87+
lastConformed:
88+
| {
89+
all: string;
90+
userInput: string;
91+
maskOnly: string;
92+
}
93+
| undefined;
94+
maskWidth: number | undefined;
95+
};
96+
97+
type Component = Intergalactic.Component<'input', Props>;
98+
}
99+
100+
namespace Addon {
101+
type Component = NSInput.Addon.Component;
102+
}
103+
104+
type Component = Intergalactic.Component<'div', Props, Ctx> & {
105+
Value: Value.Component;
106+
Addon: Addon.Component;
107+
};
108+
}
109+
110+
/** @deprecated It will be removed in v19. */
111+
export type InputMaskValueProps = NSInputMask.Value.Props;
112+
/** @deprecated It will be removed in v19. */
113+
export type InputMaskAliases = {
114+
[s: string]: RegExp;
115+
};
116+
/** @deprecated It will be removed in v19. */
117+
export type IInputMaskAsFn = (rawValue?: string) => string | RegExp[];
118+
119+
export type { NSInputMask };

semcore/input-mask/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default } from './InputMask';
22
export * from './InputMask';
3+
export * from './InputMask.type';

website/docs/components/input-mask/input-mask-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import InputMask from '@semcore/ui/input-mask';
2323
<InputMask.Value />;
2424
```
2525

26-
<TypesView type="InputMaskValueProps" :types={...types} />
26+
<TypesView type="NSInputMask.Value.Props" :types={...types} />
2727

2828
## InputMask.Addon
2929

0 commit comments

Comments
 (0)