-
Notifications
You must be signed in to change notification settings - Fork 734
Infra/masked input to ts #1976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Infra/masked input to ts #1976
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d168396
Rename file to tsx
ethanshar 9a278e4
Migrate MaskedInput to TS and remove usaged of legacy TextField compo…
ethanshar 9770921
Create a MaskedInputMigrator
ethanshar ec7e6be
PR fixes and I changed the component to be uncontrolled because of th…
ethanshar b9c461d
Update src/components/maskedInput/new.tsx
ethanshar 4d1b090
Update src/components/maskedInput/new.tsx
ethanshar 9ee0dcd
Migrate MaskedInput screen to TS
ethanshar 6702734
Merge branch 'master' into infra/MaskedInput_toTS
ethanshar f77610c
Add pin code example to MaskedInput screen
ethanshar 5e97e17
Merge branch 'master' into infra/MaskedInput_toTS
ethanshar 1bc8436
Fix PR comments
ethanshar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,17 @@ | ||
import React, {forwardRef} from 'react'; | ||
// @ts-expect-error | ||
import MaskedInputOld from './old'; | ||
import MaskedInputNew, {MaskedInputProps} from './new'; | ||
|
||
function MaskedInputMigrator(props: any, refToForward: any) { | ||
const {migrate, ...others} = props; | ||
|
||
if (migrate) { | ||
return <MaskedInputNew {...others} ref={refToForward}/>; | ||
} else { | ||
return <MaskedInputOld {...others} ref={refToForward}/>; | ||
} | ||
} | ||
|
||
export {MaskedInputProps}; | ||
export default forwardRef(MaskedInputMigrator); |
This file contains hidden or 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,129 @@ | ||
import React, { | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
useState, | ||
useImperativeHandle, | ||
forwardRef, | ||
ReactElement, | ||
ForwardedRef | ||
} from 'react'; | ||
import _ from 'lodash'; | ||
import {StyleSheet, Keyboard, TextInput, TextInputProps, StyleProp, ViewStyle} from 'react-native'; | ||
import View from '../view'; | ||
import Text from '../text'; | ||
import TouchableOpacity from '../touchableOpacity'; | ||
|
||
export interface MaskedInputProps extends Omit<TextInputProps, 'value'> { | ||
/** | ||
* Initial value to pass to masked input | ||
*/ | ||
initialValue?: string; | ||
/** | ||
* callback for rendering the custom input out of the value returns from the actual input | ||
*/ | ||
renderMaskedText?: ReactElement; | ||
/** | ||
* Custom formatter for the input value | ||
*/ | ||
formatter?: (value?: string) => string | undefined; | ||
/** | ||
* container style for the masked input container | ||
*/ | ||
containerStyle: StyleProp<ViewStyle>; | ||
} | ||
|
||
/** | ||
* @description: Mask Input to create custom looking inputs with custom formats | ||
* @gif: https://camo.githubusercontent.com/61eedb65e968845d5eac713dcd21a69691571fb1/68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f4b5a5a7446666f486f454b334b2f67697068792e676966 | ||
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/MaskedInputScreen.js | ||
*/ | ||
function MaskedInput(props: MaskedInputProps, ref: ForwardedRef<any>) { | ||
const {initialValue, formatter = _.identity, containerStyle, renderMaskedText, onChangeText, ...others} = props; | ||
const [value, setValue] = useState(initialValue); | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const inputRef = useRef<TextInput>(); | ||
const keyboardDidHideListener = useRef<any>(); | ||
|
||
useImperativeHandle(ref, () => { | ||
return { | ||
isFocused: () => inputRef.current?.isFocused(), | ||
focus, | ||
blur: () => inputRef.current?.blur(), | ||
clear: () => { | ||
inputRef.current?.clear(); | ||
setValue(''); | ||
// NOTE: This fixes an RN issue - when triggering imperative clear method, it doesn't call onChangeText | ||
onChangeText?.(''); | ||
} | ||
}; | ||
}); | ||
|
||
useEffect(() => { | ||
if (initialValue !== value) { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setValue(initialValue); | ||
} | ||
}, [initialValue]); | ||
|
||
useEffect(() => { | ||
keyboardDidHideListener.current = Keyboard.addListener('keyboardDidHide', () => { | ||
if (inputRef.current?.isFocused()) { | ||
inputRef.current?.blur(); | ||
} | ||
}); | ||
|
||
return () => keyboardDidHideListener.current.remove(); | ||
}, []); | ||
|
||
const _onChangeText = useCallback((value: string) => { | ||
const formattedValue = formatter(value) ?? ''; | ||
setValue(formattedValue); | ||
onChangeText?.(formattedValue); | ||
}, | ||
[onChangeText, formatter]); | ||
|
||
const focus = useCallback(() => { | ||
inputRef.current?.focus(); | ||
}, []); | ||
|
||
const _renderMaskedText = () => { | ||
if (_.isFunction(renderMaskedText)) { | ||
return renderMaskedText(value); | ||
} | ||
return <Text>{value}</Text>; | ||
}; | ||
|
||
return ( | ||
<TouchableOpacity style={containerStyle} activeOpacity={1} onPress={focus}> | ||
<TextInput | ||
{...others} | ||
value={value} | ||
// @ts-expect-error | ||
ref={inputRef} | ||
style={styles.hiddenInput} | ||
enableErrors={false} | ||
hideUnderline | ||
placeholder="" | ||
caretHidden | ||
multiline={false} | ||
onChangeText={_onChangeText} | ||
/> | ||
<View style={styles.maskedInputWrapper}>{_renderMaskedText()}</View> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
hiddenInput: { | ||
...StyleSheet.absoluteFillObject, | ||
zIndex: 1, | ||
color: 'transparent', | ||
backgroundColor: 'transparent', | ||
height: undefined | ||
}, | ||
maskedInputWrapper: { | ||
zIndex: 0 | ||
} | ||
}); | ||
|
||
MaskedInput.displayName = 'MaskedInput'; | ||
export default forwardRef(MaskedInput); |
File renamed without changes.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've written this in the main PR area, they should make that more noticeable somehow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely. Fixed and also add a new cool example (: