-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e11ef74
commit c3e3b93
Showing
5 changed files
with
186 additions
and
25 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
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,35 @@ | ||
import type { ForwardedRef } from 'react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import type { TextInput } from 'react-native'; | ||
import uuid from 'react-native-uuid'; | ||
import type { BTRef, Mask } from '../BaseElementTypes'; | ||
import { useBtRefUnmount } from './shared/useBtRefUnmount.hooks'; | ||
import { useBtRef } from './shared/useBtRef.hooks'; | ||
|
||
export type UseTextElementProps = { | ||
btRef?: ForwardedRef<BTRef>; | ||
mask?: Mask; | ||
}; | ||
|
||
export const useTextElement = ({ btRef, ...props }: UseTextElementProps) => { | ||
const textInputRef = useRef<TextInput>(null); | ||
const [id] = useState(uuid.v4() as string); | ||
const [mask, setMask] = useState<Mask | undefined>(undefined); | ||
const [textInputValue, setTextInputValue] = useState<string>(''); | ||
|
||
useBtRefUnmount({ btRef }); | ||
|
||
useEffect(() => { | ||
setMask(props.mask); | ||
}, [textInputValue, props.mask]); | ||
|
||
useBtRef({ btRef, textInputRef, id, setTextInputValue }); | ||
|
||
return { | ||
textInputRef, | ||
id, | ||
setTextInputValue, | ||
textInputValue, | ||
mask, | ||
}; | ||
}; |
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,39 @@ | ||
import React from 'react'; | ||
import type { StyleProp, TextStyle } from 'react-native'; | ||
import { useTextElement, UseTextElementProps } from './TextElement.hooks'; | ||
import MaskInput from 'react-native-mask-input'; | ||
import { _elementValues } from '../ElementValues'; | ||
|
||
type TextElementProps = { | ||
style?: StyleProp<TextStyle>; | ||
editable?: boolean; | ||
placeholder?: string; | ||
} & UseTextElementProps; | ||
|
||
export const CardNumberElement = ({ | ||
btRef, | ||
style, | ||
editable, | ||
placeholder, | ||
}: TextElementProps) => { | ||
const { textInputRef, id, setTextInputValue, textInputValue, mask } = | ||
useTextElement({ | ||
btRef, | ||
}); | ||
|
||
return ( | ||
<MaskInput | ||
editable={editable} | ||
mask={mask} | ||
onChangeText={(masked) => { | ||
_elementValues[id] = masked; | ||
setTextInputValue(masked); | ||
}} | ||
placeholder={placeholder} | ||
placeholderFillCharacter="" | ||
ref={textInputRef} | ||
style={style} | ||
value={textInputValue} | ||
/> | ||
); | ||
}; |
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