The text was not showing in the grid - css

Here is a list view with grid and detail within the grid with React Native 0.63.2 app. Within a grid, the left-most image shows up fine. However that text is not showing. Here is the code:
import { Container, Header, Title, Content, List, ListItem, Footer, FooterTab, Button, Left, Right, Body, Icon, Text } from 'native-base'; //ver 2.15.2
import FastImage from 'react-native-fast-image';
import { Col, Row, Grid } from 'react-native-easy-grid'; //ver 0.2.2
const { height } = Dimensions.get("window");
function ListBlock ({item}) {
return(
<TouchableOpacity onPress={() => {}}>
<Grid style={styles.grid}>
<Col size={1} >
<FastImage source={{uri:item.artimages[0].path}} style={styles.itemImage}/>
</Col>
<Col size={4}>
<Content>
<Text>Hello world</Text> //<<==text does not show. No stylesheet applied.
</Content>
</Col>
</Grid>
</TouchableOpacity>
);
}
const App = () => {
var items = [{id:1, name:"1st item", description:"this is the first item", artimages:[{path:'https://homepages.cae.wisc.edu/~ece533/images/boat.png'}]},
{id:2,name:"2nd item", description: "this is second item", artimages:[{path:'https://homepages.cae.wisc.edu/~ece533/images/baboon.png'}]}];
return (
<Container>
<Header />
<Content>
<List>
{items.map(item => {
return(
<ListItem key={item.id}>
<ListBlock item={item} />
</ListItem>)
})}
</List>
</Content>
</Container>
);
};
const styles = StyleSheet.create({
titleText:{
alignItems:'center',
alignContent:'flex-start',
textAlignVertical:'center',
fontSize:14
},
grid:{
//width:"90%",
height:40,
paddingTop:2,
paddingBottom:2,
padding:2,
},
imgCard:{
alignItems:'center',
alignContent:'center',
},
itemImage:{
paddingLeft:20,
width:40,
height:40,
},
})
export default App;
Here is how screen shot:
How to bring up the text inside a grid?

Use View instead of Content as a wrapper for the Text

Related

filling 100% width of website using mantine

My goal is to have my header and content to be 100% of the width of the page. I have no css files but instead am using mantine for styles.
I've tried editing widths to 100% on different components with no luck.
The end goal once I've got my page using 100% width is using display grid to organise my components in columns.
I would like my WeatherComponent to fill the full width so say gridColumnStart: 1 to 4
TodoList to start and end 1 to 2
and NewsComponent to start and end 2 to 4.
This is my dashboard where components are rendered:
import { Container } from '#mantine/core';
import { Group } from '#mantine/core';
import NewsComponent from '../components/NewsComponent';
import TodoList from '../components/TodoList';
import WeatherComponent from '../components/WeatherComponent';
const Dashboard = () => {
return (
<Container style={{ display: 'grid', gap: '20px' }}>
<Group style={{ gridColumn: '1 / span 3' }}>
<WeatherComponent />
</Group>
<Group style={{ gridColumnStart: 1, gridColumnEnd: 1.5 }}>
<TodoList />
</Group>
<Group style={{ gridColumnStart: 1.5, gridColumnEnd: 3 }}>
<NewsComponent />
</Group>
</Container>
);
};
export default Dashboard;
And this is my WeatherComponent which is basically serving as my Header:
import axios from 'axios';
import { Weather } from '../types';
import UserForm from './UserForm';
import { Title, Text, Container } from '#mantine/core';
import { useEffect, useState, createContext } from 'react';
export const WeatherContext = createContext<any>(null);
const WeatherComponent = () => {
const [weather, setWeather] = useState<Weather | null>();
const fetchWeatherData = async () => {
const response = await axios.get('http://mock-api-call/weather/get-weather');
setWeather(response.data.result.weather);
};
useEffect(() => {
fetchWeatherData();
}, []);
return (
<Container>
<WeatherContext.Provider value={weather?.forcast}>
<UserForm />
</WeatherContext.Provider>
<Title order={2}>Today&apos;s Weather</Title>
<Text size="lg">
{weather?.forcast} with a low of {weather?.min} and a high of {weather?.max}
</Text>
<Text size="md" data-testid="description">
{weather?.description}
</Text>
</Container>
);
};
export default WeatherComponent;
Ideally I'd like to use mantine stylings to reach 100% and not create a css file to style *.
To prevent mantine from enforcing max-width on the container element, use the fluid prop on the container.
return (
<Container fluid>
{content}
</Container
)
Solved!
I changed the width of the WeatherComponents Container to 100% and it fixed the issue.
in WeatherComponent
return (
<Container style={{ width: '100%' }}>
)

How to handle images in input fields in React?

I'm creating a reusable component using Reactstrap. What I want to do is display an image in the input field as below
What happens is this
How to get the image to the front??
InputFieldWithImage.js
import {
Input,
InputGroup,
InputGroupAddon,
InputGroupText,
Button
} from 'reactstrap';
import { Form, FormGroup } from 'reactstrap';
import Amy from './../../assets/images/Amy.png';
import Image from 'react-bootstrap/Image';
function InputFieldWithImage(props) {
const [inputType] = useState(props.type);
const [inputValue, setInputValue] = useState('');
function handleChange(event) {
console.log('Input.js');
// console.log(inputValue);
setInputValue(event.target.value);
if (props.onChange) props.onChange(event);
}
return (
<>
<InputGroup>
<Input
type={inputType}
value={inputValue}
name="input-form"
onChange={handleChange}
class="inputclass"
/>
<InputGroupAddon addonType="append" >
<Image
style={{ width:50, height: 50, marginLeft: -70 }}
src={Amy}
roundedCircle
/>
</InputGroupAddon>
</InputGroup>
</>
);
}
export default InputFieldWithImage;
You could simply handle this issue using CSS by giving the picture "position: absolute"
and the Input container "position: "relative".

How do you change the colour of the subheader in a material ui card?

I have a Material UI Card which has a dark background colour, so I need the text to be white. The style I have applied seems to successfully change the CardHeader title but not the subheader? How can I get the subheader to also change to white?
My code is as below:
const useStyles = makeStyles(theme => ({
menuColor: {
backgroundColor: theme.palette.primary.main,
color: '#ffffff',
},
}));
const ProfileUser = () => {
const classes = useStyles();
return (
<Card elevation={0} className={classes.menuColor}>
<CardHeader
avatar={<Avatar />}
title="Person name"
titleTypographyProps="h4"
subheaderTypographyProps="body1"
subheader="Person role"
/>
</Card>
);
};
export default ProfileUser;
You can pass a node to the subheader prop.
One way to achieve this is to create a new makeStyle class with the desired colour:
const useStyles = makeStyles(theme => ({
menuColor: {
backgroundColor: theme.palette.primary.main,
color: '#ffffff',
},
subColor: {
color: '#000000'
}
}));
You could then pass something to the subheader prop with this class (probably best suited to a MUI typography component):
<Card elevation={0} className={classes.menuColor}>
<CardHeader
avatar={<Avatar />}
title="Person name"
titleTypographyProps="h4"
subheaderTypographyProps="body1"
subheader={<Typography className={classes.subColor}>Person role</Typography>}
/>
</Card>;
You can just simply add CSS to the subheaderTypographyProps as an object and it will work. that's how it worked for me.
You can see it in the sample codes below:
<CardHeader
title={title}
subheaderTypographyProps={{ color: 'white' }}
subheader={`Release Date: ${release_date}`}
className={classes.title}
/>
For a one-off fix, this less scalable approach might be appropriate:
<Card>
<CardHeader
title="Hello..."
subheader={<Typography sx={{color: 'white',}}>...You, Me, World</Typography>}
/>
</Card>

Material UI TextField border overlaps with Label

I'm using Material UI TextField and Material UI Tab. I have two tabs and each has a text field inside them. After I click on the TextField, the border should open for the label, but this doesn't happen if the current Tab is not the Tab1 !!
I managed to reproduce this problem in this CodeSandBox and the code is also included below.
import React from "react";
import PropTypes from "prop-types";
import { makeStyles } from "#material-ui/core/styles";
import AppBar from "#material-ui/core/AppBar";
import Tabs from "#material-ui/core/Tabs";
import Tab from "#material-ui/core/Tab";
import Typography from "#material-ui/core/Typography";
import Box from "#material-ui/core/Box";
import TextField from "#material-ui/core/TextField";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`scrollable-force-tabpanel-${index}`}
aria-labelledby={`scrollable-force-tab-${index}`}
{...other}
>
<Box p={1}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired
};
function a11yProps(index) {
return {
id: `scrollable-force-tab-${index}`,
"aria-controls": `scrollable-force-tabpanel-${index}`
};
}
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
width: "100%",
backgroundColor: theme.palette.background.paper,
padding: 0,
margin: 0
},
Tab: {
MuiTab: {
root: {
minWidth: "130px"
}
}
}
}));
export default function Demo(props) {
const classes = useStyles();
const [value, setValue] = React.useState(0);
function handleChange(event, newValue) {
setValue(newValue);
console.log(newValue);
}
return (
<div className={classes.root}>
<AppBar position="static" color="default">
<Tabs
key={"tabs"}
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="on"
indicatorColor="primary"
textColor="primary"
aria-label="scrollable force tabs example"
>
<Tab
key={"tab1"}
className={classes.Tab}
label={0}
{...a11yProps(0)}
/>
<Tab
key={"tab2"}
className={classes.Tab}
label={1}
{...a11yProps(1)}
/>
</Tabs>
</AppBar>
<TabPanel
key={"panel1"}
value={value}
index={0}
style={{ padding: 0, margin: 0 }}
>
<div key={"div1"}>
hi im tab1{" "}
<TextField
key={"textfield1"}
variant={"outlined"}
margin={"dense"}
label={"im tab 0 textfield"}
/>
</div>
</TabPanel>
<TabPanel
key={"panel2"}
value={value}
index={1}
style={{ padding: 0, margin: 0 }}
>
<div key={"div2"}>
hi im tab2
<TextField
key={"textfield2"}
variant={"outlined"}
margin={"dense"}
label={"im tab 1 textfield"}
/>
</div>
</TabPanel>
</div>
);
}
Edit1:
I managed to find a similar question...,
Material-UI TextField Outline Label is overlapping with border when conditionally rendered
It seems this is not related to tabs as it is related to conditional rendering, which for me happened when i was using tabs
Edit2:
I tried giving the Textfield a key, but the problem still remains and there is an overlap between Textfield border and label, i updated the sandbox so it can reflect this
The label width is calculated during the initial rendering of the TextField and is only recalculated if the label changes. During the initial rendering of the TextField on your second tab, the TextField is not visible and thus the label's width is 0. Switching the TabPanel to visible does not cause a re-calculation of the label width, so no space is allotted for it in the outline.
You can fix this by using the same approach within your TabPanel as is used in the demos which is to only render the children of the panel when it is visible. This allows the label width to be correctly calculated after the initial rendering.
So instead of
<Box p={1}>{children}</Box>
you should instead have
{value === index && <Box p={1}>{children}</Box>}
I had the overlap problem with the select. Slightly different scenario though. I went through numerous pages and just couldn't find a solution to these problems:
Label text overlapping with the border - when the focus is received
Placeholder text touching the left border
If not problem # 1 - then Placeholder text staying inside the borders even when the value is selected/entered
A simple solution to all this are the following checks that worked for me -
<FormControl variant="outlined">
Make sure variant is added.
<InputLabel id="input_designationselected" style={{backgroundColor:'white'}}>Designation*</InputLabel>
Make sure that background is set for the label. Or refer to this link here https://github.com/mui-org/material-ui/issues/14530
the attribute labelId for the input control/select control matches the ID of the InputLabel
Final output is as we need it.
It drove me crazy for days - and It thought I will share it for others also. Sorry if this is the wrong place to post it.

React - Change Color of Text in Component Based on View

I have a component in use for making comments and I want to change the color based on the view (or state?) of the application.
I'm using this component
<Grid item xs={6}>
<Typography variant="subtitle1" color="secondary">
Previous comments:
</Typography>
<CommentHistory comments={comments} />
</Grid>
in a larger component and I want to have the typography text color changed based on which larger component i'm using it in.The case is a field giving back from the service, however I want to change the class name based on which component this smaller component is being used.
return comments.map(comment => {
return comment && this.renderComment(comment);
});
}
private renderComment(comment: Map<{}, {}>) {
const { classes } = this.props;
let from: React.ReactNode;
switch (comment.getIn(["from", "role"])) {
case "ROLE_MENTOR":
from = (
<Typography
variant="body2"
className={classnames(
classes.commentFromMentor,
"comment-from comment-from--mentor"
)}>
Mentor POC
</Typography>
);
break;
case "ROLE_OSBP_SUPPORT":
from = (
<Typography
variant="body2"
className={classnames(
classes.commentFromOsbpSupport,
"comment-from comment-from--osbp_support"
)}>
Mentor Protégé Program Reviewer
</Typography>
);
break;
default:
from = (
<Typography variant="body2" className="comment-from">
Unknown Commenter
</Typography>
);
break;
}
}
You can do it on basis of state.
take a state element lets say textRed if its true color will be red else color will be black .
I will show you how you can do that
state = {textRed: false;}
change the state logic the way you want whenever you want it to be true.
now in react component
<Grid item xs={6}>
<Typography className ={this.state.textRed === false? "secondary" : "danger"} variant="subtitle1">
Previous comments:
</Typography>
<CommentHistory comments={comments} />
</Grid>
I've simulated a SmallComponent which is reused in components One and Two. SmallComponent just takes a prop called className and adds it to the element whose CSS is configurable from outside (in this case, it's a button). Then, we can pass a different className and style it as we like
const SmallComponent = ({ className, text }) => (
<button className={className}>{text}</button>
);
const One = () => <SmallComponent text="From One" className="one" />;
const Two = () => <SmallComponent text="From Two" className="two" />;
const App = () => (
<div>
<One />
<Two />
</div>
);
ReactDOM.render(<App />, document.getElementById("app"));
.one {
color: red;
}
.two {
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>

Resources