i need help adding 2 decimal place in after adding a value. for example if i add 100000 then it will convert it to 100,000.00 here is my current code.
<TextInput
placeholder='0'
keyboardType = 'decimal-pad'
underlineColorAndroid="transparent"
style={{
width:'90%',
fontSize: 20,
fontWeight:'400'}}
onChangeText={TextInputValue => this.onEnterText(TextInputValue)}
/>
onEnterText = (TextInputValue) =>{
if(TextInputValue.trim() != 0){
this.setState({TextInputValue : TextInputValue, ErrorStatus : true}) ;
}else{
this.setState({TextInputValue : TextInputValue, ErrorStatus : false}) ;
}
}
Try using toFixed(2) for variable approach.
For decimals in input you might find this link useful : https://codesandbox.io/s/9zjo1lp86w
Try below code.
<TextInput
placeholder='0'
keyboardType = 'decimal-pad'
underlineColorAndroid="transparent"
style={{
width:'90%',
fontSize: 20,
fontWeight:'400'}}
onChangeText={TextInputValue =>
this.onEnterText(TextInputValue % 1 == 0 ? TextInputValue+".00" : TextInputValue.toFixed(2))}
/>
The above code definitely works.
but I don't know what is inside your onEnterText()
Related
when I entered any number value in textInput then automatically '$' symbol is generating
<TextInput
style={styles.touchmodelstyle}
autoFocus={true}
keyboardType='number-pad'
>
</TextInput>
Its a little tricky but does the job
<TextInput
value={this.state.value}
onChangeText={(text) => {
text = text.split('$').join('')
this.setState({
value: `${text}$`
})
}
}
/>
I found my question answer so I want to post it.
handleDollar = (value) => {
this.setState({ dollar: value })
};
<View style={styles.touchmodelstyle}>
<Text style={{ marginLeft: RFValue(60),
marginVertical: RFValue(20),
width: wp('4%'),
height: hp('5%'),
elevation: 1 }}>
{this.state.dollar === "" ? "" : "$"}</Text>
<TextInput
autoFocus={true}
placeholder={"other Amount"}
keyboardType='number-pad'
maxLength={3}
value={this.state.dollar}
onChangeText={(value) => this.handleDollar(value)}
>
</TextInput>
</View>
I'm using TextInput to allow only numbers using state, it works on android but not on iOS. Here's how I'm using state to allow only numbers.
handleInputChange = (text) => {
if (/^\d+$/.test(text) || text === '') {
this.setState({
text: text
});
}
}
my render method
render = () => {
return (
<View style={{
flex: 0,
flexDirection: 'row',
alignItems: 'flex-end',
marginTop: 50
}}>
<View style={{ flex: 0, marginLeft: 10 }}>
<Text style={{ fontSize: 20}}>$</Text>
</View>
<View style={{flex: 1,}}>
<TextInput
onChangeText={this.handleInputChange}
value={this.state.text}
underlineColorAndroid='transparent'
autoCorrect={false}
spellCheck={false}
style={{ paddingLeft: 5, fontSize: 20 }} />
</View>
</View>
);
}
This only works in Android, I guess because the state has changed react doesn't update the ui.
pls try this:
keyboardType='numeric' in the tag TextInput
when you prove don't put the numbers with the keyboard of your pc, pls use the keyboard of the emulator
if still not working put this textContentType='telephoneNumber'
As Ravi Rupareliya said this's a bug, which TextInput doesn't update, when the state text is shorter than the current TextInput value. Seems like the bug has been fixed in react-native 0.57.RC. For now I'm using the following fix.
handleInputChange = (text) => {
const filteredText = text.replace(/\D/gm, '');
if(filteredText !== text) {
// set state text to the current TextInput value, to trigger
// TextInput update.
this.setState({ text: text });
// buys us some time until the above setState finish execution
setTimeout(() => {
this.setState((previousState) => {
return {
...previousState,
text: previousState.text.replace(/\D/gm, '')
};
});
}, 0);
} else {
this.setState({ text: filteredText });
}
}
React native not provided keyboardType which remove punctuation from keyboard. You need to use regular expression with replace method to remove punctuation from text and set keyboardType = 'numeric'.
Regular Expression
/[- #*;,.<>{}[]/]/gi
Example code
onTextChanged(value) {
// code to remove non-numeric characters from text
this.setState({ number: value.replace(/[- #*;,.<>\{\}\[\]\\\/]/gi, '') });
}
Please check snack link
https://snack.expo.io/#vishal7008/1e004c
Worked for me:
<TextInput
...
textContentType='telephoneNumber'
dataDetectorTypes='phoneNumber'
keyboardType='phone-pad'
/>
While having input from user you can change the keyBoard type for that particular text input box like this i.e. numeric or alphabatic etc...
<TextInput>
//contains some code
keyboardType="Numeric"
</TextInput>
<TextInput>
keyboardType="number-pad"
</TextInput>
onChangeText={value => setuserPrimaryPhone(value.replace(/[^0-9]/g, ''))}
javascript simple method replace(/[^0-9]/g, ''))}
I am using expo v27.0, react native 0.55 and I as you can see in the picture that the tab have somewhat a fixed width like a default width from the tab navigation, and the text wrap into three lines, I want the text to be in 1 line and nowrap, and i have tried styling (flexWrap:
'nowrap', flex: 1) in TabStyle, LabelStyle in TabBarOptions, but still can't get the tab to have the width according to the text inside the tab.
I populate the text for the tabs dynamically from json using fetch, therefore all tabs will have different width according to the text. How to I make the tab to follow the width of the text ?
All answers are greatly welcomed.
Thank you in advance.
Solved, turns out just need to set the width to auto as follows:
tabBarOptions: {
tabStyle: {
width: 'auto'
}
}
You can use render label in render header and in that you can return your Text component and Text is having numberOfLines props that will be 1 and it will add ... at end of the text after one line.
Check example snippet:
_renderLabel = props => {
let preparedProps = {
style: {
fontFamily: fonts.Regular,
marginVertical: 8
},
fontType: props.focused ? "Medium" : "Light"
};
return (
<Text
{...preparedProps}
numberOfLines={1}
ref={ref => {
ref && this.props.addAppTourTarget(ref, props.route.key);
}}
>
{props.route.type === "free" && this.state.is_premium_member
? this.labels.premium
: props.route.title}
</Text>
);
};
_renderHeader = props => (
<TabBar
{...props}
bounces={true}
style={{
backgroundColor: colors.cardBlue
}}
indicatorStyle={{
backgroundColor: colors.radicalRed,
height: 1,
borderRightWidth: initialLayout.width * 0.1,
borderLeftWidth: initialLayout.width * 0.1,
borderColor: colors.cardBlue
}}
tabStyle={{
padding: 0,
borderTopColor: "transparent",
borderWidth: 0
}}
renderLabel={this._renderLabel}
/>
);
_handleIndexChange = index => this.setState({ index });
_renderScene = ({ route, focused }) => {
switch (route.key) {
case "a":
return <One {...this.props} route={route} focused={focused} />;
case "b":
return (
<Two {...this.props} isSeries={true} focused={focused} />
);
case "c":
return <Three {...this.props} route={route} focused={focused} />;
default:
return null;
}
};
i want second line if i entered more then 10 char in textInput. I have done changing text's font size if more then 5 char entered. that is works fine.
But if i enter more then 11 char it should comes down in second line
please help me to clear this
here is my code ...
_onChangeText(text) {
this.setState({ fontSize: (text.lenght > 6 ? 40 : 80) });
}
render() {
return (
// Giving an array of objects to style property can help
you to define a default value
<TextInput
onChangeText={this._onChangeText.bind(this)}
style={[ {fontSize: 80}, {fontSize: this.state.fontSize} ]}
/>
)
}
Set multiline prop to true. No check is required for the no. of characters. It automatically takes care of the no. of characters it can accomodate according to the width. Works completely fine for me ->
If you have a specific requirement of 10 characters then set the value of multiline as true as soon as the length of the characters reaches 10
Docs available here
you can do something like this,
state = {
fontSize: 80,
inputValue: ''
}
onChangeText(event) {
this.setState({
fontSize: event.nativeEvent.text.length > 6 ? 40 : 80,
inputValue: event.nativeEvent.text
});
}
render() {
return (
< TextInput
multiline
onChange={(event) =>
this.onChangeText(event)
}
onContentSizeChange={(event) => {
this.setState({ height: event.nativeEvent.contentSize.height })
}}
value={this.state.inputValue}
style={{ fontSize: this.state.fontSize, height: Math.max(35,
this.state.height) }}
/>
)
}
}
set multiline and handle the height of your textinput
Use multiline (boolean) property to detect when you need to show multiple lines.
_onChangeText(text) {
const areCharsExceeded = text.length > 10;
this.setState({ areCharsExceeded });
}
<TextInput
multiline={this.state.areCharsExceeded}
onChangeText={this._onChangeText.bind(this)}
/>
<! -- use multiline to able the effect, multilive is a boolean and by default is false -->
<TextInput
placeholder="broke line"
multiline
/>
<! -- if you set a fixed height then multiline will not work, to fix this issue use min-height instead of height-->
I have something like this:
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
value={0}
onChangeText={(input) => this.setState({ value: input })}
/>
However, the input box is always empty after load, any ideas?
TextInput component only accept strings. Looks like you have a integer there. Try changing that to a string. Heres a link to the doc.
Its caused by an integer value. Do JSON.stringify(value)
just change the value = {this.state.value}
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
value={this.state.value}
onChangeText={(input) => this.setState({ value: input })}
/>
use toString()
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
value={this.state.value.toString()}
onChangeText={(input) => this.setState({ value: input })}
/>
Try this it works with me!
const [_employee, setEmployee]=useState(
{
name: "name"
salary: 0
})
.
.
.
<Input
onChangeText={(salary) =>
(salary = parseInt(salary)) & setEmployee({ ..._employee, salary })
}
value={JSON.stringify(_employee.salary)}
/>