How to design fieldset legend using react-native - reactjs

In HTML we can use fieldset-legend like this:
Code:
<fieldset>
<legend>Heading</legend>
</fieldset>
O/P:
How can I design the same things using react-native? Here I got a technique where it created text with a straight line, but I want to design it like fieldset-legend. How can I do that?

I got a technique to create this fieldset-legend without any library/package. Here is my JSX code:
JSX view:
<View style={styles.fieldSet}>
<Text style={styles.legend}>Heading</Text>
<Text>Some Text or control</Text>
</View>
JSX style:
const styles = StyleSheet.create({
fieldSet:{
margin: 10,
paddingHorizontal: 10,
paddingBottom: 10,
borderRadius: 5,
borderWidth: 1,
alignItems: 'center',
borderColor: '#000'
},
legend:{
position: 'absolute',
top: -10,
left: 10,
fontWeight: 'bold',
backgroundColor: '#FFFFFF'
}
});

You could you Readymade package - react-native-fieldset
Its very easy to work with it.
You could directly do-
import FieldSet from 'react-native-fieldset';
import { View, Text } from 'react-native';
//...
return (
<View>
<FieldSet label="Fieldset label">
<Text>Field Set Body</Text>
</FieldSet>
</View>
);
//...

This FieldSet is not working
import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity, SafeAreaView, TextInput } from 'react-native';
import FieldSet from 'react-native-fieldset';
import LinearGradient from 'react-native-linear-gradient'
// create a component
export default function MyComponent() {
return (
<View>
<FieldSet label="Fieldset label" label2='try'>
<TextInput placeholder='House Name/ Number'/>
</FieldSet>
</View>
);
};

Related

React Native Project Structure Issue

I’ve just started using react native and expo and I’ve been restructuring folders over and over again because I run into multiple errors where a js file can’t find an image that’s located under ./asses/navigation/the_image.png. The only time I got it to work was having all the js files within the App.js and have the App.js located under “./” and the pictures in the same location.
This is what the error looks like: error
And this is what my project structure looks like: project structure
What’s the proper way to structure my project and how can I get files such as my profile.js to find images in assets?
Code for messages.js:
import 'react-native-gesture-handler';
import React, { useState } from 'react';
import { Image, StyleSheet, TouchableOpacity, View, TextInput } from 'react-native';
export default function messages({ navigation })
{
const [filtertext, setfilter] = useState('');
return(
<View style={styles.home_container}>
<View style={styles.home_background}>
<TextInput
style= {styles.filter}
placeholder= "Search"
placeholderTextColor= "#96a7af"
onChangeText={filtertext => setfilter(filtertext)}
defaultValue={filtertext}
/>
<Image source= {require ('./assets/navigation/3_Elements_Circled_Navigation_Message_On.png')}/>
<TouchableOpacity onPress={() =>navigation.navigate('Home')} style={styles.home_button}>
<Image source= {require ('./assets/buttons/new_message_icon.png')} style={styles.new_msg_buton}/>
</TouchableOpacity>
<TouchableOpacity onPress={() =>navigation.navigate('Profile')} style={styles.profile_button}>
<Image source= {require ('./assets/buttons/profile_icon.png')}/>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create(
{
new_msg_buton:
{
position:'absolute',
height: 60,
width: 60,
top: -696,
left: 129,
},
filter:
{
position: 'absolute',
height: 54,
width: 275,
top: -660,
left: 19,
paddingLeft: 20,
borderColor: 'black',
borderRadius: 23,
borderTopWidth: 3,
borderBottomWidth: 3,
borderLeftWidth: 3,
borderRightWidth: 3,
}
})
Using ./ refers to the current directory the file is located in
Using ../dir_onelevel_above refers to the dir_onelevel_above as you've gone one level above in the folder structure.
That being said, your should need ../assets/navigation/icon.png to access the image from messages.js

React Native - "Objects are not valid as React child"

Problem
I'm trying to import a functional component I created into my React class component, and I receive the following error:
"Invariant Violation: Objects are not valid as a React child (found: object with keys {width, height, backgroundColor, textAlign, fontSize, alignSelf, color, margin, padding, borderRadius}). If you meant to render a collection of children, use an array instead."
I can't figure out why. I've tried changing the child component to a class based component but this didn't help. I also commented out the stylesheet in child component but then I still receive a similar error:
"Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead."
Code
Parent Component
import React, {Component} from 'react';
import {
StyleSheet,
View,
Text,
Image,
TextInput,
TouchableOpacity,
TouchableWithoutFeedback,
Linking,
ScrollView
} from 'react-native';
import CustomTextInput from '../../Components/CustomTextInput/CustomTextInput';
export default class SignupScreen extends React.Component{
constructor(props) {
super();
this.state = {
ageConfirmed: false
}
}
render(){
return(
<View style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollView}
keyboardDismissMode={'on-drag'}
pinchGestureEnabled={false}
>
<CustomTextInput>
autoCorrect={false}
autoFocus={true}
placeholder={'Fortnite username'}
placeholderTextColor={'white'}
</CustomTextInput>
</ScrollView>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center'
},
scrollView: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center'
}
})
Child Component
import React from 'react';
import { TextInput, StyleSheet } from 'react-native';
import { windowWidth, windowHeight } from '../../Constants';
export default CustomTextInput = (
onChangeText,
autoCorrect,
autoFocus,
placeholder,
placeholderTextColor
) => {
return (
<TextInput>
style={styles.CustomTextInput}
{/* onChangeText={onChangeText} */}
autoCorrect={autoCorrect}
autoFocus={autoFocus}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
</TextInput>
);
}
const styles = StyleSheet.create({
CustomTextInput: {
width: windowWidth*0.54,
height: windowHeight*0.036,
backgroundColor: 'blue',
textAlign: 'center',
fontSize: windowHeight*0.03,
alignSelf: 'center',
color: 'white',
margin: windowHeight*0.01,
padding: 0,
borderRadius: windowHeight*0.015 //TODO: assess if this is the right way to calc radius
}
});
child component
import React from 'react';
import { TextInput, StyleSheet } from 'react-native';
import { windowWidth, windowHeight } from '../../Constants';
export default ({
onChangeText,
autoCorrect,
autoFocus,
placeholder,
placeholderTextColor
}) => {
return (
<TextInput
style={styles.CustomTextInput}
autoCorrect={autoCorrect}
autoFocus={autoFocus}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
/>
);
}
const styles = StyleSheet.create({
CustomTextInput: {
width: windowWidth*0.54,
height: windowHeight*0.036,
backgroundColor: 'blue',
textAlign: 'center',
fontSize: windowHeight*0.03,
alignSelf: 'center',
color: 'white',
margin: windowHeight*0.01,
padding: 0,
borderRadius: windowHeight*0.015 //TODO: assess if this is the right way to calc radius
}
});
There is a typo in both files. You have the component properties inside the tag whereas it should be defined as attributes of the tag, like so:
<CustomTextInput
autoCorrect={false}
autoFocus={true}
placeholder='Fortnite username'
placeholderTextColor='white'
/>
Sames goes for <TextInput />.

React Native styling not showing using style.container

I am learning React Native and applying some simple styling to a View. But the styling is not showing up at all. What am I doing wrong?
import React, { Component } from 'react'
import { Text, View } from 'react-native'
export default class Header extends Component {
render() {
return (
<View style={styles.container}>
<Text> Hello </Text>
</View>
)
}
}
const styles = {
container: {
backgrounColor: '#ddd',
color: 'red',
paddingTop: 14
}
}
If I change it to <View style={{backgroundColor: '#ddd', color: 'red', paddingTop: 14}}> it would work, but why won't the first approach work?
I am using:
react-native-cli: 2.0.1 and
react-native: 0.57.8
You need to use StyleSheet to create JavaScript styles in react-native
Import StyleSheet from react-native and change your variable style to:
const styles = StyleSheet.create({
container: {
backgrounColor: '#ddd',
color: 'red',
paddingTop: 14
}
});

Using a button to change the color of text

I'm trying to create a simple React Native app that:
Renders "Hello, [name]!" when a user enters a name (this part works)
Changes the "Hello, [name]!" text color when a button is pressed.
Any ideas as to how I should go about this?
I gave this an initial state of black, but that doesn't even seem to be doing anything.
What I want to happen is to trigger makeRed when the red button is clicked, which will turn the text red. Once I have this working, I'll add more color buttons.
Thank you!
See below for my App.js code. All other files were left in their default state.
import React, { Component } from 'react';
import {
AppRegistry,
Platform,
StyleSheet,
Text,
TextInput,
View,
Button
} from 'react-native';
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
text: 'World',
color: 'black'
};
}
makeRed = () => {
this.setState({
color: 'red'
});
}
render() {
return (
<View style={styles.container}>
<Text style={[styles.welcome, {color: undefined}]}>
Hello, {this.state.text}!
</Text>
<TextInput
style={styles.instructions}
placeholder="Enter a name here!"
onChangeText={(text) => this.setState({text})}
/>
<Button
title='⬤'
onPress={this.makeRed}
color='red'
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 40,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Here's a screenshot of the app for reference:
app screenshot
All you need to do is change this :
style={[styles.welcome, {color: undefined}]}
To
style={[styles.welcome, {color : this.state.color }]}
Please check : WORKING DEMO
color is not referenced as a property of state , in the style of the Text component. Try this as the Text element:
<Text style={{color: this.state.color, ...styles.welcome}}>
Hello, {this.state.text}!
</Text>
And, makeRed needs to be have it's context bound in the constructor, or else this.setState will be undefined. Like this (under super(props)):
this.makeRed = this.makeRed.bind(this)

React Native ScrollView with single child component

I want to have a fullscreen ScrollView which contains all of the contents of my Home screen in my app. The issue I'm facing is that it works as expected when I have two child components for the ScrollView (I added a Text component just to try it), but not if I just have the single content View that I want.
The contents of <View style={style.content}> are taller than the screen size, so it should scroll. But when I remove <Text style={{margin: 60, marginTop: 800}}>Here is some text</Text> the entire screen goes white.
Here's what I've heard about ScrollView:
It needs a limited height (but I should have that since all of the content is currently static (no dynamic data) and all of my components there have a fixed height)
You need to use {flex: 1} on the components inside the ScrollView (I have this style set on my Container component)
Here is my Home component:
import React, { Component } from "react";
import {
StatusBar, StyleSheet, View, ScrollView, Text
} from "react-native";
import { LinearGradient } from "expo";
import MaterialCommunityIcon from "react-native-vector-icons/MaterialCommunityIcons"
import Color from "color";
import globalStyles from "../config/globalStyles";
import Container from "../components/Container/Container";
import Header from "../components/Header/Header";
import FullWidthImage from "../components/Images/FullWidthImage";
import MainFeedbackText from "../components/Text/MainFeedbackText";
import MainStatistics from "../components/Statistics/MainStatistics";
import Button from "../components/Buttons/Button"
export default class Home extends Component {
render() {
return (
<ScrollView style={{borderColor:"red", borderWidth: 2}}>
<Container>
<View style={style.content}>
<MainFeedbackText/>
<Button
text="START NEW WORKOUT"
textStyle={{fontSize: 24}}
buttonStyle={{
backgroundColor: globalStyles.colors.green,
paddingHorizontal: 20,
}}
icon={<MaterialCommunityIcon style={style.settings} name="dumbbell" color="white" size={30}/>}
/>
<Button
text="My workouts"
textStyle={{fontSize: 24, paddingRight: 40}}
buttonStyle={{
backgroundColor: Color("black").fade(0.8),
paddingHorizontal: 20,
margin: 20
}}
icon={<MaterialCommunityIcon style={style.settings} name="format-list-bulleted" color="white" size={20}/>}
/>
<MainStatistics/>
</View>
{/**TODO: ScrollView breaks when removing this line*/}
<Text style={{margin: 60, marginTop: 800}}>Here is some text</Text>
<StatusBar translucent={false}/>
<LinearGradient colors={["transparent", globalStyles.colors.dark]} style={style.gradient}>
<View style={style.image}>
<FullWidthImage requireSource={require("./lifting.jpg")}/>
</View>
</LinearGradient>
<Header/>
</Container>
</ScrollView>
);
}
}
const style = StyleSheet.create({
image: {
opacity: 0.3,
zIndex: -1,
},
gradient: {
position: "absolute",
top: 0,
zIndex: -1
},
content: {
flex: 1,
position: "absolute",
top: 80,
height: 4000,
}
});
Set contentContainerStyle={{ flex: 1 }} for ScrollView to become fullscreen.

Resources