Skip to content

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 11 commits into from
May 8, 2022
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {ScrollView, StyleSheet} from 'react-native';
import {Typography, View, Text, MaskedInput, Button} from 'react-native-ui-lib'; //eslint-disable-line
import {Typography, View, Text, MaskedInput, Button, Colors} from 'react-native-ui-lib'; //eslint-disable-line

export default class MaskedInputScreen extends Component {
constructor(props) {
super(props);

this.state = {
error: '',
timeValue: '15'
};
}
minInput = React.createRef<any>();
priceInput = React.createRef<any>();
state = {
error: '',
timeValue: '15'
};

componentDidMount() {
setTimeout(() => {
this.minput.focus();
this.minInput.current.focus();
}, 500);
}

clearInputs = () => {
this.minput.clear();
this.priceInput.clear();
this.minInput.current.clear();
this.priceInput.current.clear();
};

renderTimeText(value) {
renderTimeText(value: string) {
const paddedValue = _.padStart(value, 4, '0');
const hours = paddedValue.substr(0, 2);
const minutes = paddedValue.substr(2, 2);
Expand All @@ -39,7 +37,7 @@ export default class MaskedInputScreen extends Component {
);
}

renderPrice(value) {
renderPrice(value: string) {
const hasValue = Boolean(value && value.length > 0);
return (
<View row center>
Expand All @@ -56,6 +54,20 @@ export default class MaskedInputScreen extends Component {
);
}

renderPINCode = (value = '') => {
return (
<View row centerH>
{_.times(4, i => {
return (
<View marginR-s3 center style={styles.pinCodeSquare}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixed!

<Text h1>{value[i]}</Text>
</View>
);
})}
</View>
);
};

render() {
const {timeValue} = this.state;

Expand All @@ -71,9 +83,9 @@ export default class MaskedInputScreen extends Component {
</Text>
<MaskedInput
migrate
ref={r => (this.minput = r)}
ref={this.minInput}
renderMaskedText={this.renderTimeText}
formatter={value => value?.replace(/\D/g, '')}
formatter={(value: string) => value?.replace(/\D/g, '')}
keyboardType={'numeric'}
maxLength={4}
initialValue={timeValue}
Expand All @@ -85,11 +97,24 @@ export default class MaskedInputScreen extends Component {
</Text>
<MaskedInput
migrate
ref={r => (this.priceInput = r)}
ref={this.priceInput}
renderMaskedText={this.renderPrice}
formatter={value => value?.replace(/\D/g, '')}
formatter={(value: string) => value?.replace(/\D/g, '')}
keyboardType={'numeric'}
/>

<Text text70 marginT-s5 marginB-s4>
PIN Code
</Text>
<MaskedInput
migrate
maxLength={4}
ref={this.priceInput}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create a dedicated ref, currently only one of the two users (price \ pin) are deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as well (:

renderMaskedText={this.renderPINCode}
formatter={(value: string) => value?.replace(/\D/g, '')}
keyboardType={'numeric'}
/>

<View centerH marginT-100>
<Button label="Clear All" onPress={this.clearInputs}/>
</View>
Expand All @@ -111,5 +136,12 @@ const styles = StyleSheet.create({
header: {
...Typography.text60,
marginVertical: 20
},
pinCodeSquare: {
width: 50,
height: 70,
borderWidth: 2,
borderColor: Colors.grey30,
borderRadius: 4
}
});
4 changes: 2 additions & 2 deletions src/components/maskedInput/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import View from '../view';
import Text from '../text';
import TouchableOpacity from '../touchableOpacity';

export interface MaskedInputProps extends TextInputProps {
export interface MaskedInputProps extends Omit<TextInputProps, 'value'> {
/**
* Initial value to pass to masked input
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ function MaskedInput(props: MaskedInputProps, ref: ForwardedRef<any>) {
[onChangeText, formatter]);

const focus = useCallback(() => {
inputRef.current?.focus?.();
inputRef.current?.focus();
}, []);

const _renderMaskedText = () => {
Expand Down