react bootsrap element not centering - reactjs

I have a "div" that was centered horizontally, then updated to use react bootstrap "
form".
Now the elements on the "form" are not centered, appearing to the left...
render() {
return (
<div className="App">
<div className="App-title">
Countdown to {this.state.deadline}
</div>
<Clock
deadline={this.state.deadline}
/>
<Form inline>
<FormControl
className="Deadline-input"
placeholder='new date'
onChange={event => this.setState({newDeadline: event.target.value})}
/>
<Button onClick={() => this.changeDeadline()}>
Submit
</Button>
</Form>
</div>
)
}
All is under className App
.App {
text-align: center;
font-size: 35px;
margin-top: 20%;
}
.App-title {
font-size: 50px;
}
.Clock-days,
.Clock-hours,
.Clock-minutes,
.Clock-seconds {
display: inline;
margin: 10px;
}
.Deadline-input {
font-size: 25px;
margin: 5px;
}
Why it is not centered anymore? how to align my form on the center?
:]

When you import 'bootstrap/dist/css/bootstrap.min.css'; bootstrap adds some styles and classes by default to the elements, Because of this form, it is folded to the left.
You can just add justify-content-center class to <Form inline />
<Form inline className="justify-content-center">
like this:
render() {
return (
<div className="App">
<div className="App-title">
Countdown to {this.state.deadline}
</div>
<Clock
deadline={this.state.deadline}
/>
// add justify-content-center class to the <Form />
<Form inline className="justify-content-center">
<FormControl
className="Deadline-input"
placeholder='new date'
onChange={event => this.setState({newDeadline: event.target.value})}
/>
<Button onClick={() => this.changeDeadline()}>
Submit
</Button>
</Form>
</div>
)
}

Related

React Component CSS does not work when digit zero "0" is displayed but works fine with other values

I have this ChartCard component which shows the design applied to it if the value is greater than zero but when the value is 0 it ignores the scss applied to it and adapts another random style. I am a beginner in React that is why I couldn't understand whether the problem is in the style or component.
Here is the ChartCard Component:
render() {
const { chartProps, title, amount, percent, successPercent, strokeWidth, showInfo } = this.props
return (
<div style={{
minHeight : '110px',
}}>
{chartProps && (
<div className="chartCard__chart">
{/* <Sparkline {...chartProps} /> */}
<Progress type="circle" width={100} percent={ percent } successPercent={ successPercent } stokeWidth={ strokeWidth } showInfo={ showInfo } />
</div>
)}
<div style={{marginTop: '30px', width: '65%'}}>
{amount && <div className="chartCard__amount">{amount}</div>}
{title && <div className="chartCard__title">{title}</div>}
</div>
</div>
)
Scss for it:
.chartCard {
padding: rem(15) rem(30) rem(20);
position: relative;
overflow: hidden;
&__chart {
height: 100%;
width: 100px;
position: absolute;
top: 0;
right: 5px;
}
&__amount {
font-size: rem(36);
color: $black;
margin-bottom: rem(-10);
}
&__title {
color: $text;
text-transform: uppercase;
font-weight: bold;
}
}
And where it is being used:
<div>
<Row gutter={4}>
<Col xs={24} lg={12} xl={12}>
<ChartCard
title="Active Volunteers"
amount={volunteer.active_volunteers}
successPercent={
(volunteer.active_volunteers / volunteer.total_volunteers) * 100
}
/>
</Col>
<Col xs={24} lg={12} xl={12}>
<ChartCard
title="Verified Volunteers"
amount={volunteer.verified_volunteers}
successPercent={
(volunteer.verified_volunteers / volunteer.total_volunteers) * 100
}
/>
</Col>
</Row>
</div>
You can see that when the value is greater than 0 it shows the styling but not when the value is 0.
In the ChartCard Component code, I removed the && sign which was considering the value 0 as false and it was cancelling the whole design.
I changed this:
<div style={{marginTop: '30px', width: '65%'}}>
{amount && <div className="chartCard__amount">{amount}</div>}
{title && <div className="chartCard__title">{title}</div>}
</div>
to this:
<div style={{marginTop: '30px', width: '65%'}}>
{<div className="chartCard__amount">{amount}</div>}
{title && <div className="chartCard__title">{title}</div>}
</div>

How to update the state in different mobile screens using React Hooks

I am working on React project, In that I have a button under that I have another div, I written a function if My screen width is 320px then margin-bottom: 150px has to apply under button. it is working fine, but when I am in 320px screen if I click the button then under button margin-bottom: 150px is applied. Here the problem comes now when I go to 375 px here also margin-bottom: 150 px applied automatically. so someone help to how to update state in 375px. Because in 375px screen I have to apply margin-bottom: 300 px.
If you have any questions please let me know thank you.
This is My code
This is App.js
import React, { useState, useLayoutEffect } from 'react';
import './App.css';
const App = () => {
const [style, setStyle] = useState(null)
function useMediaQuery() {
const [screenSize, setScreenSize] = useState([0]);
useLayoutEffect(() => {
function updateScreenSize() {
setScreenSize([window.innerWidth]);
}
window.addEventListener("resize", updateScreenSize);
updateScreenSize();
return () => window.removeEventListener("resize", updateScreenSize);
}, []);
return screenSize;
}
const [mediaQuery] = useMediaQuery();
console.log(mediaQuery, '***')
const applyStyle = () => {
if(mediaQuery === 320) {
setStyle({
marginBottom: '150px'
})
}
}
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='first'>
<button onClick={applyStyle} style={style} className='btn btn-primary'>Click here</button>
<span className='closeWindow'><i className="far fa-window-close"></i></span>
</div>
<div className='second'>
</div>
</div>
</div>
</div>
)
}
export default App
in App.css write a #media query:
#media screen and (max-width: 375px) {
button {
margin-bottom: 300px;
}
}
You can apply media queries in React either programmatically, or using css.
Option 1, using css:
.myButton{
/* media queries */
#media (max-width: 320px) {
margin-bottom: 150px;
}
#media (max-width: 375px) {
margin-bottom: 300px;
}
}
and then:
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='first'>
<button onClick={applyStyle} className='myButton btn btn-primary'>Click here</button>
<span className='closeWindow'><i className="far fa-window-close"></i></span>
</div>
<div className='second'>
</div>
</div>
</div>
</div>
)
Option 2, programmatically:
import React, { useState, useEffect, useLayoutEffect } from 'react';
import './App.css';
const App = () => {
const [mediaMatch, setMediaMatch] = useState();
const handler = e => setMediaMatch(e.matches);
useEffect(() => {
window.matchMedia('(min-width: 768px)').addListener(handler);
return () => {
window.removeEventListener(handler);
};
}, []);
return (
<div className="container">
<div className="row">
<div className="col-12">
<div className="first">
<button
onClick={applyStyle}
style={{
marginBottom: mediaMatch ? '300px' : '150px'
}}
className="btn btn-primary"
>
Click here
</button>
<span className="closeWindow">
<i className="far fa-window-close" />
</span>
</div>
<div className="second" />
</div>
</div>
</div>
);
};

How can I put the arrows in the center and outside the red border? React-multi-carousel

I'm using react-multi-carousel, What I would like to do is to always have the arrows in the middle and outside the border-box, how can I do that? Is there any trick in css to acomplish this?
I tried to put the react-multiple-carousel__arrow--left and react-multiple-carousel__arrow--righft with position absolute but this will always put the arrows at the top of the page. Below is my code so far...
<CarouselWrapper>
<Carousel
customLeftArrow={<CustomLeftArrow />}
customRightArrow={<CustomRightArrow />}
renderButtonGroupOutside={true}
customButtonGroup={<ButtonGroup />}
responsive={responsive}
arrows={false}
>
<div className="image">1</div>
<div className="image">2</div>
<div className="image">3</div>
<div className="image">4</div>
<div className="image">5</div>
<div className="image">6</div>
</Carousel>
</CarouselWrapper>
css
.image {
height: 150px;
width: 150px;
font-size: 25px;
margin: 10px;
display: inline-block;
line-height: 100px;
border: 3px red solid;
}
function CustomRightArrow({ onClick }) {
return (
<button
onClick={handleClick}
aria-label="Go to next slide"
className="react-multiple-carousel__arrow react-multiple-carousel__arrow--right"
/>
);
}
function CustomLeftArrow({ onClick }) {
return (
<button
onClick={handleClick}
aria-label="Go to previous slide"
className="react-multiple-carousel__arrow react-multiple-carousel__arrow--left"
/>
);
}
const ButtonGroup = ({ next, previous }) => {
return (
<div className="carousel-button-group">
<CustomLeftArrow
onClick={() => previous()}
/>
<CustomRightArrow onClick={() => next()} />
</div>
);
};

React Material UI textfield label problem

I have problem with material ui text field.
There is a form with more textinput. When I scroll down the page, the textinputs label overlap on the header. Could You any idea solve this problem.
Thank You for Your help!
Without scrolling
Scrolling
Code Sandbox: https://codesandbox.io/s/serverless-night-wpkrb?file=/src/App.js
Code from sandbox below:
textinput.js
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
const useStyles = makeStyles((theme) => ({
root: {
"& > *": {
margin: theme.spacing(1),
width: "25ch"
}
}
}));
const id = (error) => {
if (error === true) {
return "outlined-error";
} else {
return "outlined";
}
};
const shrink = (arg) => {
// ez a func biztosítja, hogy teljes label legyen és ne legyen kezelési hiba
if (arg === "") {
return false;
} else {
return true;
}
};
export default function BasicTextFields(props) {
const classes = useStyles();
return (
<form className={classes.root} noValidate autoComplete="off">
<TextField
error={props.error}
id={id(props.error)}
label={props.label}
variant="outlined"
onChange={props.change}
style={{ width: props.width }}
value={props.value}
InputLabelProps={{ shrink: shrink(props.value) }}
type={props.type}
inputProps={{ maxLength: props.maxlength }}
/>
</form>
);
}
App.js
import React from "react";
import "./styles.css";
import "w3-css/w3.css";
import BasicTextFields from "./textinput";
export default function App() {
return (
<div className="body">
<div className="w3-top w3-padding-8 w3-border-bottom w3-border-black">
<div className="w3-center w3-padding-16">
<div className="t1">
TündErella - <span style={{ fontSize: 45 }}> some text here.</span>{" "}
</div>
</div>
</div>
<div style={{ marginTop: 200 }}>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
<div className="name">
<BasicTextFields label="Vezetéknév: *"></BasicTextFields>
<BasicTextFields label="Keresztnév: *"></BasicTextFields>
</div>
<div className="name">
<BasicTextFields label="Vezetéknév: *"></BasicTextFields>
<BasicTextFields label="Keresztnév: *"></BasicTextFields>
</div>
<div className="name">
<BasicTextFields label="Vezetéknév: *"></BasicTextFields>
<BasicTextFields label="Keresztnév: *"></BasicTextFields>
</div>
<div className="name">
<BasicTextFields label="Vezetéknév: *"></BasicTextFields>
<BasicTextFields label="Keresztnév: *"></BasicTextFields>
</div>
</div>
);
}
styles.css
.App {
font-family: sans-serif;
text-align: center;
}
.body {
border: 1px solid white;
/*background-image: url("./static/background_maarten-deckers_1.jpg");*/
background-color: ivory;
}
.w3-top {
background-color: #daf0da;
}
.t1 {
font-size: 60px;
font-family: "Great Vibes", cursive;
}
The label for outlined TextField is rendered with a z-index of 1. This is the same z-index as applied by w3-top.
You need to bump up the z-index of w3-top in your styles.css:
.w3-top {
background-color: #daf0da;
z-index: 2;
}
In order for these styles to win over the styles defined in w3-css, you need to flip the order of your imports
from:
import "./styles.css";
import "w3-css/w3.css";
to:
import "w3-css/w3.css";
import "./styles.css";
Here's a working example: https://codesandbox.io/s/override-w3-top-z-index-k5fjv?file=/src/styles.css:198-252

Css is not rendering when i move from Login page to registration page in Routing

When I move from the Login page to the Registration page, CSS is not changing.
It is only showing me the CSS of Login page. It is moving to the Registeration page. I don't know how it is happening.
Can anyone tell me how to render registration.css when I move to Registeration page?
Thanks in advance. Below are my code snippets:
App.js:
import React from 'react';
import Login from './htmlfiles/Login';
import Register from './htmlfiles/Register';
import Buttons from './htmlfiles/Buttons';
import Change from './htmlfiles/Change';
import {BrowserRouter as Router, Route} from 'react-router-dom';
function App() {
return (
<div>
<Router>
<Route path="/"exact strict component={Login}/>
<Route path="/Register" exact strict component={Register}/>
<Route path ="/Buttons" exact strict component={Buttons}/>
<Route path ="/Change" exact strict component={Change}/>
</Router>
</div>
);
}
export default App;
Login.js
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button } from 'react-bootstrap';
import Avatar from 'react-avatar';
import '../cssfile/Login.css';
import avatar from '../image.png';
import { Formik} from 'formik';
import * as yup from 'yup';
import { Link } from 'react-router-dom';
const Login = (history) => (
<Formik
initialValues={{
UserName:'',
Password:''
}}
onSubmit={(value)=>
{
console.log("The values are",value);
// history.push('./Register')
}}
validationSchema={yup.object(
{
UserName:yup.string()
.required('Required')
.min(4,'Minimum 4 characters required')
.max(8,'Maximum limit is 8 characters'),
Password:yup.string()
.required('Required')
.min(4,'Minimum 4 characters required')
.max(8,'Maximum limit is 8 characters')
}
)}
>
{({handleChange,handleSubmit,errors,values})=> (
<div className="contain">
<div className="card">
<h5 className="card-title">Login</h5>
<div className="card-body">
<div className="avatar">
<Avatar src={avatar} round={true} size="100"/>
</div>
<form onSubmit={handleSubmit} action='/Register'>
<div className="form-group">
<label>User Name:</label>
<input
type="text"
className={errors.UserName ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="UserName"
value={values.UserName}
/>
{errors.UserName ? <div className="text-danger" style={{height:'0px'}}>*{errors.UserName}*</div>:null}
</div>
<div className="form-group">
<label>Password:</label>
<input
type="password"
className={errors.Password ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="Password"
value={values.Password}
/>
{errors.Password ?<div className="text-danger" style={{height:'0px'}}> *{errors.Password}*</div>:null}
</div>
<div className="button">
<Button type="submit" className="btn-lg btn-block">Submit</Button>
</div>
</form>
<Link to="./Register" id="p">Don't have an account?</Link>
</div>
</div>
</div>
)
}
</Formik>
);
export default Login;
Register.js
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button } from 'react-bootstrap';
import Avatar from 'react-avatar';
import '../cssfile/Register.css';
import avatar from '../image.png';
import { Formik} from 'formik';
import * as yup from 'yup';
const phone = /[7-9]{1}[0-9]{4}[0-9]{5}$/;
const Register = () => (
<Formik
initialValues={{
UserName:'',
email:'',
mobile:'',
Password:'',
CPassword:''
}}
onSubmit={(value)=>
{
console.log("The values are",value);
}}
validationSchema={yup.object(
{
UserName:yup.string()
.required('Required')
.min(4,'Minimum 4 characters required')
.max(8,'Maximum limit is 8 characters'),
email:yup.string()
.required('Required')
.email('Email-id is invalid'),
mobile:yup.string()
.required('Required')
.matches(phone,'Phone number is invalid'),
Password:yup.string()
.required('Required')
.min(4,'Minimum 4 characters required')
.max(8,'Maximum limit is 8 characters'),
CPassword:yup.string()
.required('Required')
.oneOf([yup.ref('Password'),null],"Password doesn't match")
}
)}
>
{({handleChange,handleSubmit,errors,values})=> (
<div className='contain'>
<div className="card">
<h5 className="card-title">Registeration</h5>
<div className="card-body">
<div className="avatar">
<Avatar src={avatar} round={true} size="90"/>
</div>
<form onSubmit={handleSubmit} action='/Login'>
<div className="form-group">
<label>UserName:</label>
<input
type="text"
className={errors.UserName ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="UserName"
value={values.UserName}/>
{errors.UserName ?<div className="text-danger" style={{height:'0px'}}> *{errors.UserName}*</div>:null}
</div>
<div className="form-group">
<label>Email id:</label>
<input
type="text"
className={errors.email ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="email"
value={values.email}/>
{errors.email ?<div className="text-danger" style={{height:'0px'}}> *{errors.email}*</div>:null}
</div>
<div className="form-group">
<label>Phone Number:</label>
<input
type="text"
className={errors.mobile ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="mobile"
value={values.mobile}/>
{errors.mobile ?<div className="text-danger" style={{height:'0px'}}> *{errors.mobile}*</div>:null}
</div>
<div className="form-group">
<label>Password:</label>
<input
type="text"
className={errors.Password ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="Password"
value={values.Password}/>
{errors.Password ?<div className="text-danger" style={{height:'0px'}}> *{errors.Password}*</div>:null}
</div>
<div className="form-group">
<label>Confirm Password:</label>
<input
type="text"
className={errors.CPassword ? "form-control is-invalid":"form-control"}
onChange={handleChange}
name="CPassword"
value={values.CPassword}/>
{errors.CPassword ?<div className="text-danger" style={{height:'0px'}}> *{errors.CPassword}*</div>:null}
</div>
<div className="button">
<Button type="submit" className="btn-lg btn-block">Register</Button>
</div>
</form>
</div>
</div>
</div>
)
}
</Formik>
);
export default Register;
Login.css
.card
{
width: 20%;
height:80%;
margin-left: 600px;
background-color:grey;
}
.card-title
{
text-align: center;
padding-top: 80px;
}
.contain
{
width:100%;
height: 100%;
padding-top: 150px;
background-color:rgb(250, 112, 112);
}
.button
{
margin-top: 30px;
}
.card-body
{
position:relative;
bottom: 100px;
}
.avatar
{
position: relative;
bottom: 90px;
left: 80px;
}
#p{
margin-left: 50px;
color:blue;
text-decoration: underline;
cursor: pointer;
}
**Register.css**
.card
{
width: 40%;
height:95%;
margin-left: 450px;
background-color:grey;
}
.card-title
{
text-align: center;
padding-top: 50px;
}
.contain
{
width:100%;
height: 100%;
padding-top: 80px;
background-color:rgb(250, 112, 112);
}
.button
{
margin-top: 30px;
}
.card-body
{
position:relative;
bottom: 110px;
}
.avatar
{
position: relative;
left: 240px;
bottom: 40px;
}
You need to make use of CSS modules as described here
So you need to rename your css files according to the convention as described in that link. So rename
Register.css to Register.module.css & Login.css to Login.module.css
Now you need to import them in your js files like this:
*** Register.js ***
import style from '../cssfile/Register.module.css';
*** Login.js ***
import style from '../cssfile/Login.module.css';
And then in your components, instead of:
className="contain"
You need to do:
className={style.contain}
Now refactor all your className usages like this and it will work as expected.

Resources