Skip to content

Commit f69c7bf

Browse files
authored
feat: type intellisense for mask type prop
* fix: add jsx support for ts compiler * feat: Type intellisense for mask type prop
1 parent 73bcc4f commit f69c7bf

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

example/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"compilerOptions": {},
2+
"compilerOptions": {
3+
"jsx": "react"
4+
},
35
"extends": "expo/tsconfig.base"
46
}

src/@types/FormatType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type FormatType = 'custom' | 'currency' | 'date' | 'time'

src/components/MaskedTextInput.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import React, {
77
import { TextInput, TextInputProps } from 'react-native'
88
import { mask, unMask } from '../utils/mask'
99
import type { MaskOptions } from '../@types/MaskOptions'
10+
import type { FormatType } from '../@types/FormatType'
1011

1112
type TIProps = Omit<TextInputProps, 'onChangeText'>
1213

1314
export interface MaskedTextInputProps extends TIProps {
1415
mask?: string
15-
type?: 'custom' | 'currency'
16+
type?: FormatType
1617
options?: MaskOptions
1718
defaultValue?: string
1819
onChangeText: (text: string, rawText: string) => void
19-
inputAccessoryView?: JSX.Element;
20+
inputAccessoryView?: JSX.Element
2021
}
2122

2223
export const MaskedTextInputComponent: ForwardRefRenderFunction<
@@ -35,38 +36,43 @@ export const MaskedTextInputComponent: ForwardRefRenderFunction<
3536
},
3637
ref
3738
): JSX.Element => {
38-
const getMaskedValue = (value: string) => mask(value, pattern, type, options);
39-
const getUnMaskedValue = (value: string) => unMask(value, type);
39+
const getMaskedValue = (value: string) => mask(value, pattern, type, options)
40+
const getUnMaskedValue = (value: string) =>
41+
unMask(value, type as 'custom' | 'currency')
4042

4143
const defaultValueCustom = defaultValue || ''
4244
const defaultValueCurrency = defaultValue || '0'
4345

44-
const initialMaskedValue = getMaskedValue(type === 'currency' ? defaultValueCurrency : defaultValueCustom);
46+
const initialMaskedValue = getMaskedValue(
47+
type === 'currency' ? defaultValueCurrency : defaultValueCustom
48+
)
4549

46-
const initialUnMaskedValue = getUnMaskedValue(type === 'currency' ? defaultValueCurrency : defaultValueCustom);
50+
const initialUnMaskedValue = getUnMaskedValue(
51+
type === 'currency' ? defaultValueCurrency : defaultValueCustom
52+
)
4753

48-
const [maskedValue, setMaskedValue] = useState(initialMaskedValue);
49-
const [unMaskedValue, setUnmaskedValue] = useState(initialUnMaskedValue);
54+
const [maskedValue, setMaskedValue] = useState(initialMaskedValue)
55+
const [unMaskedValue, setUnmaskedValue] = useState(initialUnMaskedValue)
5056

5157
function onChange(value: string) {
52-
const newUnMaskedValue = unMask(value, type);
53-
const newMaskedValue = mask(newUnMaskedValue, pattern, type, options);
58+
const newUnMaskedValue = unMask(value, type as 'custom' | 'currency')
59+
const newMaskedValue = mask(newUnMaskedValue, pattern, type, options)
5460

55-
setMaskedValue(newMaskedValue);
56-
setUnmaskedValue(newUnMaskedValue);
61+
setMaskedValue(newMaskedValue)
62+
setUnmaskedValue(newUnMaskedValue)
5763
}
5864

5965
useEffect(() => {
60-
onChangeText(maskedValue, unMaskedValue);
61-
}, [maskedValue, unMaskedValue]);
66+
onChangeText(maskedValue, unMaskedValue)
67+
}, [maskedValue, unMaskedValue])
6268

6369
useEffect(() => {
6470
if (value) {
65-
setMaskedValue(getMaskedValue(value));
66-
setUnmaskedValue(getUnMaskedValue(value));
71+
setMaskedValue(getMaskedValue(value))
72+
setUnmaskedValue(getUnMaskedValue(value))
6773
} else {
68-
setMaskedValue(initialMaskedValue);
69-
setUnmaskedValue(initialUnMaskedValue);
74+
setMaskedValue(initialMaskedValue)
75+
setUnmaskedValue(initialUnMaskedValue)
7076
}
7177
}, [value])
7278

@@ -81,7 +87,7 @@ export const MaskedTextInputComponent: ForwardRefRenderFunction<
8187
/>
8288
{inputAccessoryView}
8389
</>
84-
);
85-
};
90+
)
91+
}
8692

87-
export const MaskedTextInput = forwardRef(MaskedTextInputComponent);
93+
export const MaskedTextInput = forwardRef(MaskedTextInputComponent)

src/utils/mask.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-confusing-arrow */
22
import { BigNumber } from 'bignumber.js'
3+
import type { FormatType } from '../@types/FormatType'
34
import toPattern from './toPattern'
45

56
/**
@@ -109,7 +110,7 @@ function multimasker(value: string, patterns: string[], options: any) {
109110
function mask(
110111
value: string | number,
111112
pattern: string | string[] = '',
112-
type: 'custom' | 'currency' | 'date' | 'time' = 'custom',
113+
type: FormatType = 'custom',
113114
options?: any
114115
): string {
115116
if (type === 'currency') {

0 commit comments

Comments
 (0)