Handling TextInput events in React Native - react-native

How can we handle two events at the same time for React Native text input? For example, in my component I want the text to change when I am typing and submit when I am hit enter. I tried with the code below and it did not work but I think that's the idea
<TextInput onChangeText={this.functionA} onSubmitEditing={this.functionB}></TextInput>

Here what i have done testing and checking for you.
import React, { Component } from 'react';
import { AppRegistry, TextInput, View } from 'react-native';
export default class UselessTextInput extends Component {
constructor(props) {
super(props);
this.state = { text: 'Useless Placeholder' };
}
functionA(){
alert('AAA');
}
functionB(){
alert('BBB');
}
render() {
return (
<View style={{paddingTop:40}}>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
// onChangeText={(text) => this.setState({text})}
onChangeText={this.functionA} onSubmitEditing={this.functionB}
value={this.state.text}
/>
</View>
);
}
}
Please make sure you have read the Document
And moreover, you should understand:
onChangeText is called when the text input's text changes.
============
onSubmitEditing is called when the text input's submit button is pressed.
Hope this will help you to figure and guide you more information.

Related

How do I restrict the number of lines in textInput? In React

this is my first stackoverflow question, please bare with me if I do anyhting wrong. Here goes my question: how do I restrict the number of lines in a textInput using react? I have to say im new to react and dont know if i am doing it wrong. I have searched for this question before asking this one and they recommend to use maxheight. Ive tried it and it doesnt work. this is my code. I am trying to restrict the number of written lines to 4. Currently, a textInput appears but i can write more thatn 4 lines.
import React, { Component } from 'react';
import { TextInput } from 'react-native';
export default class UselessTextInput extends Component {
constructor(props) {
super(props);
this.state = { text: 'Write something' };
}
render() {
return (
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
multiline={true}
numberOfLines={5}
/>
);
}
}
Any type of help is very much appreciated.

How to disable one button when another button is pressed in react-native-paper

In react-native-paper (or even in react-native dirctly) I don't understand how to do the equivalent of getElementById to modify an element. In JavaScript, I would assign each button a unique id, and then when one button is clicked, I can call function that will disable/enable the other button based on its id.
However I am not seeing how to accomplish this task in react-native-paper (or react-native either).
Here is sample code:
import React from 'react';
import {View, Alert} from 'react-native';
import {Button} from 'react-native-paper';
export default class App extends React.Component {
render() {
return (
<View>
<Button mode="contained" color="green" onPress={() => this.buttonOnePressed()}>Button One</Button>
<Button mode="contained" color="red" onPress={() => this.buttonTwoPressed()}>Button Two</Button>
</View>
);
}
buttonOnePressed() {
// If Button Two is disabled, then enable it.
// If Button Two is enabled, then disable it.
Alert.alert('Button ONE pressed');
}
buttonTwoPressed() {
// Do something when Button Two is pressed
Alert.alert('Button TWO pressed');
}
}
In your code make a state for the 1st button and another state for the 2nd button. Then by handling the state of the 2, you can achieve what you want
Here's a sample code:
export class default Test extends React.Component{
constructor(props){
super(props);
this.state = {
firstButtonEnable: false,
secondButtonDisable: false,
}
this.handlingButton = this.handlingButton.bind(this)
}
handlingButton(){
firstButtonEnable
? this.setState({secondButtonDisable: true})
: null;
}
render(){
return(
<View>
<Button title='Button1' onPress={() => {
this.setState({firstButtonEnable: true});
handlingButton();
}} />
<Button disabled={secondButtonDisable} title='Button2' } />
</View>
)
}
}

React native disable submit keyboard on device if text input is empty string

Like a title, I want to disable a submit button (Search) on device's keyboard if text input is an empty string.
I creating a search bar and using TextInput.
How can I do this?
you should have 2 state (textSearch and buttonStatus) and check every onChangeText in TextInput props
here's a simple code I've used:
import React, {Component} from 'react';
import RN, {View, StyleSheet, Text} from 'react-native';
export default class History extends Component {
constructor(props) {
super(props);
this.state={
search:null,
disabledButton:true
}
}
render(){
return(
<View style={styles.container}>
<RN.TextInput
value={this.state.search}
onChangeText={(e)=>{
this.setState({search:e, disabledButton:e==""?true:false})
}}
/>
<RN.Button
title="Search"
disabled={this.state.disabledButton}
/>
</View>
)
}
}
const styles = StyleSheet.create({
container:{flex:1, justifyContent:'center', alignItems:'center'}
})
this is the first time I've provided help here, so hopefully it helps someone.
The inputText should be whatever state you use to store your text.
addItem() is whatever function name you want to execute when there are enough characters.
I wanted to require the user to type more than 1 character, so adjust to your liking.
onSubmitEditing={() => {(inputText.length < 2) ? console.log('test') : addItem()}}
<TextInput
style={styles.input}
onSubmitEditing={() => {
(inputText.length < 2) ? console.log('test') : addItem()
}}
maxLength={20}
autoCorrect={false}
onChangeText={onChangeText}
placeholder={'Scavenger Hunt Item'}
value={inputText}>
</TextInput>

how to show multiple wheel or Components in Picker of React Native?

I have use this reference url https://snack.expo.io/HkjxvRS-W for adding Picker in React Native, but here it's show only one wheel in Picker, but I want to display two wheel in Picker, so how do I do it ? is anyone have any idea?
import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';
export default class HomePage extends Component {
constructor(props) {
super(props);
this.state = {
selectedValue: ''
arrayPickers: ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten']
}
}
render() {
return (
<View style={styles.container}>
<Picker
style={{width: 320}}
selectedValue={this.state.selectedValue}
onValueChange={(value) => this.setState({selectedValue: value})}>
{this.state.arrayPickers.map((value, index) => (
<Picker.Item label={value} value={value} key={index} />
))}
</Picker>
</View>
);
}
}
From Above code I am able to show one Wheel or Component in Picker, but I want to show two Wheel or Component in Picker.
Please have look below image for further understanding.
Picker
Here is a library that you can use to achieve what you want react-native-picker, as didn't specify how you wanted or posted any source code, I feel like this could help you.

Element type invalid when adding button

I was following React Native basic tutorial. Then after it ended, I tried to add a button in the code used in TextInput tutorial, following example in Button page
import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, Button, View } from 'react-native';
class AwsumProjek extends Component {
constructor(props) {
super(props);
this.state = {tiText: ''};
}
render() {
return (
<View style={{padding: 10}}>
<TextInput
style={{height: 40}}
placeholder="Type here to translate!"
onChangeText={(tiText) => this.setState({tiText})}
/>
<Text style={{padding: 10, fontSize: 42}}>
{this.state.tiText.split(' ').map((word) => word && 'WA').join(' ')}
</Text>
<Button
title="Press Purple"
color="#841584"
accessibilityLabel="Learn more about purple"
/>
</View>
);
}
}
AppRegistry.registerComponent('AwsumProjek', () => AwsumProjek);
Instead I got this error
Element type is invalid: expected a string (for built-in components)
or a class/function (for composite components) but got: undefined.
Check the render method of 'AwsumProjek'.
What did I do wrong? Seeing other answer, is it something related to importing something?
I'm native android developer trying to learn React Native, and as me now, Javascript is totally unfamiliar for me.
Make sure you are using correct version of React Native. Button component was introduced in React Native version 0.37