Skip to content

Commit 721a02d

Browse files
committed
fix: prevent from unnecessary state updates
1 parent 32c9674 commit 721a02d

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

lib/index.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export default class PhoneInput extends Component {
4646

4747
componentDidUpdate() {
4848
const { value, disabled } = this.props;
49-
this.setState({ disabled });
49+
if (disabled != null && disabled !== this.state.disabled) {
50+
this.setState({disabled});
51+
}
5052

5153
if (value && value !== this.state.value) {
5254
this.setState({ value });
@@ -183,16 +185,18 @@ export default class PhoneInput extends Component {
183185
const TextComponent = this.props.textComponent || TextInput;
184186
return (
185187
<View style={[styles.container, this.props.style]}>
186-
<TouchableWithoutFeedback
187-
onPress={this.onPressFlag}
188-
disabled={disabled}
189-
>
190-
<Image
191-
source={Flags.get(iso2)}
192-
style={[styles.flag, this.props.flagStyle]}
188+
{this.props.shouldShowCountryPicker && (
189+
<TouchableWithoutFeedback
193190
onPress={this.onPressFlag}
194-
/>
195-
</TouchableWithoutFeedback>
191+
disabled={disabled}
192+
>
193+
<Image
194+
source={Flags.get(iso2)}
195+
style={[styles.flag, this.props.flagStyle]}
196+
onPress={this.onPressFlag}
197+
/>
198+
</TouchableWithoutFeedback>
199+
)}
196200
<View style={{ flex: 1, marginLeft: this.props.offset || 10 }}>
197201
<TextComponent
198202
ref={ref => {
@@ -211,23 +215,25 @@ export default class PhoneInput extends Component {
211215
/>
212216
</View>
213217

214-
<CountryPicker
215-
ref={ref => {
216-
this.picker = ref;
217-
}}
218-
selectedCountry={iso2}
219-
onSubmit={this.selectCountry}
220-
buttonColor={this.props.pickerButtonColor}
221-
buttonTextStyle={this.props.pickerButtonTextStyle}
222-
cancelText={this.props.cancelText}
223-
cancelTextStyle={this.props.cancelTextStyle}
224-
confirmText={this.props.confirmText}
225-
confirmTextStyle={this.props.confirmTextStyle}
226-
pickerBackgroundColor={this.props.pickerBackgroundColor}
227-
itemStyle={this.props.pickerItemStyle}
228-
onPressCancel={this.props.onPressCancel}
229-
onPressConfirm={this.props.onPressConfirm}
230-
/>
218+
{this.props.shouldShowCountryPicker && (
219+
<CountryPicker
220+
ref={ref => {
221+
this.picker = ref;
222+
}}
223+
selectedCountry={iso2}
224+
onSubmit={this.selectCountry}
225+
buttonColor={this.props.pickerButtonColor}
226+
buttonTextStyle={this.props.pickerButtonTextStyle}
227+
cancelText={this.props.cancelText}
228+
cancelTextStyle={this.props.cancelTextStyle}
229+
confirmText={this.props.confirmText}
230+
confirmTextStyle={this.props.confirmTextStyle}
231+
pickerBackgroundColor={this.props.pickerBackgroundColor}
232+
itemStyle={this.props.pickerItemStyle}
233+
onPressCancel={this.props.onPressCancel}
234+
onPressConfirm={this.props.onPressConfirm}
235+
/>
236+
)}
231237
</View>
232238
);
233239
}
@@ -265,11 +271,13 @@ PhoneInput.propTypes = {
265271
confirmText: PropTypes.string,
266272
confirmTextTextStyle: styleType,
267273
disabled: PropTypes.bool,
268-
allowZeroAfterCountryCode: PropTypes.bool
274+
allowZeroAfterCountryCode: PropTypes.bool,
275+
shouldShowCountryPicker: PropTypes.bool
269276
};
270277

271278
PhoneInput.defaultProps = {
272279
initialCountry: "us",
273280
disabled: false,
274-
allowZeroAfterCountryCode: true
281+
allowZeroAfterCountryCode: true,
282+
shouldShowCountryPicker: true
275283
};

0 commit comments

Comments
 (0)