Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions playground/entries/InputMask.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { NSInput } from '@semcore/ui/input';
import type { InputMaskValueProps } from '@semcore/ui/input-mask';
import type { NSInputMask } from '@semcore/ui/input-mask';
import InputMask from '@semcore/ui/input-mask';
import React from 'react';

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

export type InputMaskJSXProps = JSXProps<NSInput.Props & InputMaskValueProps>;
export type InputMaskJSXProps = JSXProps<NSInput.Props & NSInputMask.Value.Props>;

function getJSX(props: InputMaskJSXProps) {
return (
Expand Down
8 changes: 4 additions & 4 deletions semcore/date-picker/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type Divider from '@semcore/divider';
import type { DropdownProps, DropdownTriggerProps } from '@semcore/dropdown';
import type { NSInput } from '@semcore/input';
import type Input from '@semcore/input';
import type { InputMaskValueProps } from '@semcore/input-mask';
import type { NSInputMask } from '@semcore/input-mask';
import type { TooltipProps } from '@semcore/tooltip';
import type dayjs from 'dayjs';
import type { ChangeEvent } from 'react';
Expand Down Expand Up @@ -315,7 +315,7 @@ declare const InputTrigger: Intergalactic.Component<
Value: typeof Input.Value;
SingleDateInput: Intergalactic.Component<'div', NSInput.Props & SingleDateInputProps> & {
Indicator: typeof Input.Addon;
MaskedInput: Intergalactic.Component<'input', InputMaskValueProps & DatePickerMaskedInputProps>;
MaskedInput: Intergalactic.Component<'input', NSInputMask.Value.Props & DatePickerMaskedInputProps>;
};
};

Expand All @@ -330,11 +330,11 @@ declare const RangeInputTrigger: Intergalactic.Component<
RangeSep: typeof Input.Addon;
FromMaskedInput: Intergalactic.Component<
'input',
InputMaskValueProps & DatePickerMaskedInputProps
NSInputMask.Value.Props & DatePickerMaskedInputProps
>;
ToMaskedInput: Intergalactic.Component<
'input',
InputMaskValueProps & DatePickerMaskedInputProps
NSInputMask.Value.Props & DatePickerMaskedInputProps
>;
};
};
Expand Down
110 changes: 15 additions & 95 deletions semcore/input-mask/src/InputMask.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { NeighborLocation, Box, Flex, ScreenReaderOnly } from '@semcore/base-components';
import {
createComponent,
Component,
sstyled,
Root,
type PropGetterFn,
type Intergalactic,
} from '@semcore/core';
import type { Intergalactic } from '@semcore/core';
import { createComponent, Component, sstyled, Root } from '@semcore/core';
import fire from '@semcore/core/lib/utils/fire';
import getInputProps, { inputProps } from '@semcore/core/lib/utils/inputProps';
import logger from '@semcore/core/lib/utils/logger';
Expand All @@ -16,80 +10,9 @@ import Input, { type NSInput } from '@semcore/input';
import React from 'react';
import * as mask from 'text-mask-core';

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

export type IInputMaskAsFn = (rawValue?: string) => string | RegExp[];

export type InputMaskAliases = {
[s: string]: RegExp;
};

export type InputMaskValueProps = NSInput.Value.Props & {
/**
* Mask for entering text
*/
mask?: string | boolean | IInputMaskAsFn;
/**
* The property for visibility of the mask
* @default false
*/
hideMask?: boolean;
/**
* This function allows you to change the input value before it is displayed on the screen.
*/
pipe?: (
conformedValue: string,
config: {
rawValue: string;
},
) => string | false | { value: string; indexesOfPipedChars: number[] };
/** Internal */
keepCharPositions?: boolean;
/**
* The aliases object for the mask values. The key is the symbol used in the mask,
* and the value is the regular expression that this symbol must match
* @default {'9': /\d/, 'a': /[a-zA-Zа-яА-Я]/, '*': /[\da-zA-Zа-яА-Я]/}
*/
aliases?: InputMaskAliases;
/**
* Event that is called when the input value fully matches the mask
*/
onSuccess?: (value: string) => void;
/**
* A field that explains the mask for blind users
* */
title?: string;

/** Specifies which props to pass to input element */
includeInputProps?: string[];

/**
* Field for describe which symbols will use as mask
* @default `{_: true}`
*/
maskOnlySymbols?: Record<string, boolean>;

/**
* Overrids width of the input field
*/
inputW?: string | number;

/**
* Aria role for input
*/
inputRole?: string;
};

type InputMaskCtx = {
getInputProps: PropGetterFn;
getValueProps: PropGetterFn;
};

type InputMaskComponent = Intergalactic.Component<'div', NSInput.Props, InputMaskCtx> & {
Value: Intergalactic.Component<'input', InputMaskValueProps>;
Addon: typeof Input.Addon;
};

const { createTextMaskInputElement } = mask;

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

class InputMask extends Component<NSInput.Props> {
class InputMask extends Component<
Intergalactic.InternalTypings.InferComponentProps<NSInputMask.Component>
> {
static displayName = 'InputMask';
static style = style;

Expand All @@ -115,8 +40,12 @@ class InputMask extends Component<NSInput.Props> {
}
}

class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value: Array<(value?: string) => string> }> {
static defaultProps = {
class Value extends Component<
Intergalactic.InternalTypings.InferComponentProps<NSInputMask.Value.Component>,
typeof Value.enhance,
NSInputMask.Value.Handlers
> {
static defaultProps: NSInputMask.Value.DefaultProps = {
includeInputProps: inputProps,
defaultValue: '',
hideMask: false,
Expand All @@ -138,16 +67,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
textMaskCoreInstance: any = undefined;
usedMask: any = undefined;
prevConfirmedValue: any = undefined;
state: {
lastConformed:
| {
all: string;
userInput: string;
maskOnly: string;
}
| undefined;
maskWidth: number | undefined;
} = {
state: NSInputMask.Value.State = {
lastConformed: undefined,
maskWidth: undefined,
};
Expand All @@ -159,7 +79,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
});
}

componentDidUpdate(prevProps: any) {
componentDidUpdate(prevProps: typeof this.asProps) {
const maskConfigProps = ['mask', 'hideMask', 'pipe', 'keepCharPositions'] as const;
const maskConfigChanged = maskConfigProps.some(
(prop) => this.asProps[prop] !== prevProps[prop],
Expand Down Expand Up @@ -403,7 +323,7 @@ class Value extends Component<InputMaskValueProps, typeof Value.enhance, { value
*
* {@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}
*/
export default createComponent<InputMaskComponent, typeof InputMask>(InputMask, {
export default createComponent<NSInputMask.Component, typeof InputMask>(InputMask, {
Value,
Addon: Input.Addon,
});
119 changes: 119 additions & 0 deletions semcore/input-mask/src/InputMask.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { PropGetterFn, Intergalactic } from '@semcore/core';
import type { NSInput } from '@semcore/input';

declare namespace NSInputMask {
type Props = NSInput.Props;
type Ctx = {
getInputProps: PropGetterFn;
getValueProps: PropGetterFn;
};

namespace Value {
type Props = NSInput.Value.Props & {
/**
* Mask for entering text
*/
mask?: string | boolean | ((rawValue?: string) => string | RegExp[]);
/**
* The property for visibility of the mask
* @default false
*/
hideMask?: boolean;
/**
* This function allows you to change the input value before it is displayed on the screen.
*/
pipe?: (
conformedValue: string,
config: {
rawValue: string;
},
) => string | false | { value: string; indexesOfPipedChars: number[] };
/** Internal */
keepCharPositions?: boolean;
/**
* The aliases object for the mask values. The key is the symbol used in the mask,
* and the value is the regular expression that this symbol must match
* @default {'9': /\d/, 'a': /[a-zA-Zа-яА-Я]/, '*': /[\da-zA-Zа-яА-Я]/}
*/
aliases?: {
[s: string]: RegExp;
};
/**
* Event that is called when the input value fully matches the mask
*/
onSuccess?: (value: string) => void;
/**
* A field that explains the mask for blind users
* */
title?: string;

/** Specifies which props to pass to input element */
includeInputProps?: string[];

/**
* Field for describe which symbols will use as mask
* @default `{_: true}`
*/
maskOnlySymbols?: Record<string, boolean>;

/**
* Overrids width of the input field
*/
inputW?: string | number;

/**
* Aria role for input
*/
inputRole?: string;
};
type DefaultProps = {
includeInputProps: Array<string>;
defaultValue: '';
hideMask: false;
keepCharPositions: false;
aliases: {
'9': RegExp;
'a': RegExp;
'*': RegExp;
};
maskOnlySymbols: {
_: boolean;
};
};
type Handlers = {
value: Array<(value?: string) => string>;
};
type State = {
lastConformed:
| {
all: string;
userInput: string;
maskOnly: string;
}
| undefined;
maskWidth: number | undefined;
};

type Component = Intergalactic.Component<'input', Props>;
}

namespace Addon {
type Component = NSInput.Addon.Component;
}

type Component = Intergalactic.Component<'div', Props, Ctx> & {
Value: Value.Component;
Addon: Addon.Component;
};
}

/** @deprecated It will be removed in v19. */
export type InputMaskValueProps = NSInputMask.Value.Props;
/** @deprecated It will be removed in v19. */
export type InputMaskAliases = {
[s: string]: RegExp;
};
/** @deprecated It will be removed in v19. */
export type IInputMaskAsFn = (rawValue?: string) => string | RegExp[];

export type { NSInputMask };
1 change: 1 addition & 0 deletions semcore/input-mask/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default } from './InputMask';
export * from './InputMask';
export * from './InputMask.type';
2 changes: 1 addition & 1 deletion website/docs/components/input-mask/input-mask-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import InputMask from '@semcore/ui/input-mask';
<InputMask.Value />;
```

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

## InputMask.Addon

Expand Down
Loading