Skip to content

Commit da14611

Browse files
authored
Support clearing MaskedInput and make it more responsive to touch (#1240)
1 parent e20b41b commit da14611

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

demo/src/screens/componentScreens/MaskedInputScreen.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {ScrollView, StyleSheet} from 'react-native';
4-
import {Typography, View, Text, MaskedInput} from 'react-native-ui-lib'; //eslint-disable-line
4+
import {Typography, View, Text, MaskedInput, Button} from 'react-native-ui-lib'; //eslint-disable-line
55

66
export default class MaskedInputScreen extends Component {
77
constructor(props) {
88
super(props);
99

1010
this.state = {
1111
error: '',
12+
timeValue: '15'
1213
};
1314
}
1415

@@ -18,6 +19,11 @@ export default class MaskedInputScreen extends Component {
1819
}, 500);
1920
}
2021

22+
clearInputs = () => {
23+
this.minput.clear();
24+
this.priceInput.clear();
25+
};
26+
2127
renderTimeText(value) {
2228
const paddedValue = _.padStart(value, 4, '0');
2329
const hours = paddedValue.substr(0, 2);
@@ -51,12 +57,11 @@ export default class MaskedInputScreen extends Component {
5157
}
5258

5359
render() {
60+
const {timeValue} = this.state;
61+
5462
return (
5563
<View flex>
56-
<ScrollView
57-
contentContainerStyle={styles.container}
58-
keyboardShouldPersistTaps='always'
59-
>
64+
<ScrollView contentContainerStyle={styles.container} keyboardShouldPersistTaps="always">
6065
<Text text40 marginB-20>
6166
Masked Inputs
6267
</Text>
@@ -69,16 +74,21 @@ export default class MaskedInputScreen extends Component {
6974
renderMaskedText={this.renderTimeText}
7075
keyboardType={'numeric'}
7176
maxLength={4}
72-
value={'15'}
77+
value={timeValue}
78+
onChangeText={value => this.setState({timeValue: value})}
7379
/>
7480

7581
<Text text70 marginT-40>
7682
Price/Discount
7783
</Text>
7884
<MaskedInput
85+
ref={r => (this.priceInput = r)}
7986
renderMaskedText={this.renderPrice}
8087
keyboardType={'numeric'}
8188
/>
89+
<View centerH marginT-100>
90+
<Button label="Clear All" onPress={this.clearInputs}/>
91+
</View>
8292
</ScrollView>
8393
</View>
8494
);
@@ -89,13 +99,13 @@ const styles = StyleSheet.create({
8999
container: {
90100
flexDirection: 'column',
91101
justifyContent: 'flex-start',
92-
padding: 25,
102+
padding: 25
93103
},
94104
title: {
95-
...Typography.text20,
105+
...Typography.text20
96106
},
97107
header: {
98108
...Typography.text60,
99-
marginVertical: 20,
100-
},
109+
marginVertical: 20
110+
}
101111
});

src/components/maskedInput/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import BaseInput from '../baseInput';
66
import TextField from '../textField';
77
import View from '../view';
88
import Text from '../text';
9+
import TouchableOpacity from '../touchableOpacity';
910

1011
/**
1112
* @description: Mask Input to create custom looking inputs with custom formats
@@ -38,6 +39,11 @@ export default class MaskedInput extends BaseInput {
3839
this.keyboardDidHideListener.remove();
3940
}
4041

42+
clear() {
43+
this.setState({value: ''});
44+
this.input.clear();
45+
}
46+
4147
renderMaskedText() {
4248
const {renderMaskedText} = this.props;
4349
const {value} = this.state;
@@ -53,7 +59,7 @@ export default class MaskedInput extends BaseInput {
5359
const TextInputProps = TextField.extractOwnProps(this.props, ['containerStyle', 'style']);
5460

5561
return (
56-
<View style={containerStyle}>
62+
<TouchableOpacity style={containerStyle} activeOpacity={1} onPress={() => this.input.focus()}>
5763
<TextField
5864
{...this.props}
5965
ref={r => (this.input = r)}
@@ -68,7 +74,7 @@ export default class MaskedInput extends BaseInput {
6874
onChangeText={this.onChangeText}
6975
/>
7076
<View style={styles.maskedInputWrapper}>{this.renderMaskedText()}</View>
71-
</View>
77+
</TouchableOpacity>
7278
);
7379
}
7480
}

0 commit comments

Comments
 (0)