I am implementing the following logging component in React:
I want all the buttons to have the same width and I want them to fill the whole width available.
This is my code:
export default function Login (props: any) {
return(
<Box
sx={{
display: 'flex',
flexWrap: 'wrap',
'& > :not(style)': {
m: 1,
width: 400,
height: 500,
},
}}
>
<Paper elevation={10}>
<div style={{paddingTop: 30}}>
LOGIN
</div>
<div style={{paddingTop: 40}}>
<form>
<div style={{paddingTop: 30}}>
<FormControl variant="standard">
<InputLabel htmlFor="email">
Email
</InputLabel>
<Input
id="email"
startAdornment={
<InputAdornment position="start">
<PersonIcon />
</InputAdornment>
}
/>
</FormControl>
</div>
<div style={{paddingTop: 30}}>
<FormControl variant="standard">
<InputLabel htmlFor="password">
Password
</InputLabel>
<Input
id="password"
startAdornment={
<InputAdornment position="start">
<KeyIcon />
</InputAdornment>
}
/>
</FormControl>
</div>
<div style={{paddingTop: 30}}>
<Button variant="contained">Accept</Button>
</div>
<div style={{paddingTop: 30}}>
<Button variant="contained" endIcon={<GoogleIcon />}>
Login with Google
</Button>
</div>
<div style={{paddingTop: 30}}>
<Button variant="contained" >
Cancel
</Button>
</div>
</form>
</div>
</Paper>
</Box>
)
}
To make a button take full width of container, you can set fullWidth prop to true in Button component. For more information you can refer MUI Button API.
Working demo: Demo
Like this:-
<Box
sx={{
display: "flex",
flexWrap: "wrap",
padding: "20px 40px",
"& > :not(style)": {
m: 1,
width: 400,
height: 500
}
}}
>
<Paper
elevation={10}
sx={{
display: "flex",
justifyContent: "center",
flexDirection: "column"
}}
>
<div style={{ paddingTop: 30, textAlign: "center" }}>LOGIN</div>
<div
style={{ paddingTop: 40, display: "flex", justifyContent: "center" }}
>
<form>
<div style={{ paddingTop: 30 }}>
<FormControl variant="standard">
<InputLabel htmlFor="email">Email</InputLabel>
<Input
id="email"
startAdornment={
<InputAdornment position="start">
<PersonIcon />
</InputAdornment>
}
/>
</FormControl>
</div>
<div style={{ paddingTop: 30 }}>
<FormControl variant="standard">
<InputLabel htmlFor="password">Password</InputLabel>
<Input
id="password"
startAdornment={
<InputAdornment position="start">
<KeyIcon />
</InputAdornment>
}
/>
</FormControl>
</div>
<div style={{ paddingTop: 30 }}>
<Button variant="contained" fullWidth>
Accept
</Button>
</div>
<div style={{ paddingTop: 30 }}>
<Button variant="contained" endIcon={<GoogleIcon />} fullWidth>
Login with Google
</Button>
</div>
<div style={{ paddingTop: 30 }}>
<Button variant="contained" fullWidth>
Cancel
</Button>
</div>
</form>
</div>
</Paper>
</Box>
just add full width to 100.
you can add it with a styled component, or inline in sx prop: {width:1}
in sx prop width 1 will be converted to 100% width
see the mui sizing page here
Set attribute of each button: style={{paddingTop: 30, width: "100%"}}
Related
I have Form.list like below
before
I want label on top, like this
I wish
is it possible?
I don't know which component to use..
You can use Row & Col from antd to layout your list labels.
import { PlusOutlined } from '#ant-design/icons';
import { Button, Form, Input, Row, Col, Typography } from 'antd';
function App() {
return (
<div className='container my-4'>
<Form>
<Form.List name='names'>
{(fields, { add, remove }) => (
<>
{!!fields.length && (
<Row align='middle'>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Subnet</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Netmask</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Router</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }} />
<Col span={4} />
</Row>
)}
{fields.map((field, index) => (
<Row key={index}>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='10.40.0.0' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='255.255.255.0' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='10.40.0.1' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='IP Pool' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={4}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Button onClick={() => remove(field.name)}>Delete</Button>
</Form.Item>
</Col>
</Row>
))}
<Button type='dashed' onClick={() => add()} icon={<PlusOutlined />} style={{ margin: '5px' }}>
Add Subnet
</Button>
</>
)}
</Form.List>
</Form>
</div>
);
}
export default App;
I'm making an app, and I am currently working on making the login page.
I want the login tools to be responsive, centered by width and height. justify-content :
'center' was not working for me, but it seemed to be working with other people's code, so I am not sure what the problem is.
This is the image of the website
My Body Code
<div>
<Paper elevation={10} className={classes.root}>
<Grid align="center">
<h2 className={classes.title}>Log in </h2>
</Grid>
<form onSubmit={this.submitLogin}>
<Paper className={classes.paper}>
<TextField
id="standard-basic"
label="Email"
name="name"
type="text"
value={this.state.name}
onChange={this.handleInputChange}
fullWidth
required
/>
</Paper>
<Paper className={classes.paper}>
<TextField
id="standard-basic"
label="Password"
name="password"
type="password"
value={this.state.password}
onChange={this.handleInputChange}
fullWidth
required
/>
</Paper>
<Typography className={classes.pf}>
<Link href="">Forgot Password?</Link>
</Typography>
<Typography className={classes.pf}>{this.state.text}</Typography>
<Tooltip title="Will Remember You When You Come Back">
<FormControlLabel
control={<Checkbox name="checkedB" color="primary" />}
label="Remember Me"
/>
</Tooltip>
<Button
type="submit"
className={classes.log}
onClick={this.submitLogin}
variant="contained"
fullWidth
>
LOG IN
</Button>
</form>
<Typography className={classes.fp}>
Haven't created an account?
<Tooltip title="Go to the Sign Up page">
<Link href="./register"> Sign Up</Link>
</Tooltip>
</Typography>
</Paper>
</div>
My Theme Code
const styles = (theme) => ({
root: {
padding: 50,
height: 450,
width: 400
},
title: {
fontSize: "30px"
},
paper: {
marginTop: "15px"
},
log: {
background: "#3f51b5",
color: "white",
border: "0px",
borderRadius: "5px",
borderColor: "grey",
fontSize: "20px",
height: "45px",
marginTop: "5%"
},
fp: {
marginTop: "80px",
marginLeft: "65px"
},
pf: {
marginTop: "8px"
}
});
I couldn't really find the 'working' solutions for me, so I'm posting a question
Wrap your Paper component in a div with the styles you mention, that should work as expected:
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
// Change the size to fit the parent element of this div
width: '100%',
height: '100%',
}}>
<Paper>
SomeStuff...
</Paper>
</div>
In a Project, I have list of countries with its specific country codes. In order to search the countries and their codes I have created TextField on Paper(Material UI component). When clicked on it closes unexpectedly. See the code below for reference.
<TextField
id="standard-select-currency"
select
value={countryCode}
onChange={handleChange}
SelectProps={{
renderValue: (value) => value
}}
>
<Paper style={{ width: "280px" }}>
<div
style={{
backgroundColor: "white",
top: "8px",
position: "sticky",
zIndex: "999",
width: "inherit"
}}
>
{/* Here this component closes when clicked on it */}
<TextField
placeholder="Enter Your Country"
onChange={onChange}
type="text"
/>
<hr />
</div>
{Object.values(countryData).map((values, i) => (
<MenuItem key={values.calling_code} value={values.calling_code}>
<div
style={{
display: "flex",
flexWrap: "wrap",
justifyContent: "space-between",
width: "100%",
position: "relative"
}}
>
<p>{values.country}</p>
<p>{"+" + values.calling_code}</p>
</div>
</MenuItem>
))}
</Paper>
</TextField>
What could be best possible solution to prevent it from shutting down?
Codesandbox Link: https://codesandbox.io/s/material-demo-forked-hfwb4
just add,
<TextField
placeholder="Enter Your Country"
onChange={onChange} onClick={(event)=>event.stopPropagation()}
type="text"
/>
it will
it will not close unexpectedly.
I'm using React and material-ui and I have a form with fields and a button. I'm not able to move the button on the right because I'm using: justify-content: "flex-start". I tried to define an other useStyles but doesn't work. How can I do?
const useStyles = makeStyles({
root: {
color: '#034563',
'&$checked': {
color: '#034563',
},
'& > *': {
margin: theme.spacing(1),
display: 'flex',
justifyContent: "flex-start",
},
},
});
<form
className={classes.root} onSubmit={onSubmitForm}>
<TextField
id="add-name"
label="Name"
multiline
value={enteredName}
onChange={(event => { setEnteredName(event.target.value); })} />
<TextField
id="add-description"
label="Description"
multiline
value={enteredDescription}
onChange={(event) => { setEnteredDescription(event.target.value); }} />
<FormControlLabel
control={<Switch checked={isActive} onChange={ActiveChange} color="primary" />}
label="active" />
<div>
<Box>
<Button
id="new-sumbit"
type="submit"
color="primary"
variant="contained"
autoFocus
>
Add
</Button>
</Box>
</div>
</form>
Try this
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<Box>
<Button
id="new-sumbit"
type="submit"
color="primary"
variant="contained"
autoFocus>
Add
</Button>
</Box>
</div>
I am new to reactjs and am facing some issues in the ui. I am trying to center my header in the space between my app bar and my navlink. However the heading is not coming in the center.
What is happening is that the navbar is taking up space at the top how do I modify it such that the heading is in the center.
Here is the code:
return (
<MuiThemeProvider theme={theme}>
<React.Fragment>
<div className={useStyles.root}>
<AppBar position="static">
<NavLink to="dashboard">
<ListItem button>
<ListItemIcon>
<DashboardIcon />
</ListItemIcon>
<ListItemText
style={{ color: "#FFFFFF" }}
/>
</ListItem>
</NavLink>
<Typography
gutterBottom
align="center"
style={{ width: "100%", alignItems: "center" }}
>
Best Practices Management System
</Typography>
</AppBar>
</div>
<br />
<Form onSubmit={this.handleSubmit} style={{ marginLeft: "5%" }}>
<Grid container>
<Grid item xs={6}>
<TextField
label="Title"
variant="outlined"
size="small"
name="Title"
id="Title"
placeholder="Enter the Title of the Best Practice"
onChange={handleChange("Title")}
defaultValue={values.Title}
multiline
rows={2}
rowsMax={4}
style={{ width: "90%" }}
/>
<label
id="title"
style={{ visibility: "hidden", color: "red" }}
>
Title must be atleast 5 characters long
</label>
</Grid>
<Grid item xs={6}>
<TextField
placeholder="Enter the details of the Best Practice"
label="Details of Best Practice"
id="Details"
size="small"
name="Details"
onChange={handleChange("Details")}
defaultValue={values.Details}
style={{ width: "90%" }}
variant="outlined"
multiline
rows={2}
rowsMax={4}
/>
<label
id="details"
style={{ visibility: "hidden", color: "red" }}
>
Details of Best Practice must be atleast 10 characters long
</label>
</Grid>
</Grid>
<Grid container>
<Grid item xs={6}>
<TextField
placeholder="What is the Best Practice?"
label="What is the Best Practice"
size="small"
id="What"
name="What"
onChange={handleChange("What")}
defaultValue={values.What}
style={{ width: "90%" }}
variant="outlined"
multiline
rows={2}
rowsMax={4}
/>
<label id="what" style={{ visibility: "hidden", color: "red" }}>
What is the Best Practice must be atleast 10 characters long
</label>
</Grid>
<Grid item xs={6}>
<TextField
placeholder="Why was the Best Practice Implemented"
label="Why was the Best Practice Implemented"
size="small"
name="Why"
id="Why"
onChange={handleChange("Why")}
defaultValue={values.Why}
style={{ width: "90%" }}
variant="outlined"
multiline
rows={2}
rowsMax={4}
/>
<label id="why" style={{ visibility: "hidden", color: "red" }}>
Why was the Best Practice implemented must be atleast 10
characters long
</label>
</Grid>
</Grid>
<Grid container>
<Grid item xs={6}>
<TextField
placeholder="How was the Best Practice Implemented"
label="How was the Best Practice Implemented"
size="small"
name="How"
id="How"
onChange={handleChange("How")}
defaultValue={values.How}
style={{ width: "90%" }}
variant="outlined"
multiline
rows={2}
rowsMax={4}
/>
<label id="how" style={{ visibility: "hidden", color: "red" }}>
How was the Best Practice implemented must be atleast 10
characters long
</label>
</Grid>
<Grid item xs={6}>
<FormControl id="Status" style={{ width: "90%" }} size="small">
<InputLabel
htmlFor="Implementation_Status"
id="Status"
style={{
marginLeft: 10,
top: "50%",
transform: "translate(0,-50%"
}}
>
Implementation Status
</InputLabel>
<Select
labelId="Implementation_Status"
name="Status"
id="Status"
onChange={handleChange("Status")}
defaultValue={values.Status}
variant="outlined"
inputProps={{
id: "Implementation_Status",
name: "age"
}}
>
<MenuItem value="1">Implemented</MenuItem>
<MenuItem value="2">Implementation in Progress</MenuItem>
<MenuItem value="3">Not Implemented</MenuItem>
</Select>
</FormControl>
<label
id="status"
style={{ visibility: "hidden", color: "red" }}
>
Implementation Status cannot be blank
</label>
</Grid>
</Grid>
<Grid container>
<Grid item xs={6}>
<Form.Group controlId="TBD_COST">
<TextField
placeholder="Cost of Implementation of the Best Practice"
label="Cost of Implementation"
name="Cost"
id="Cost"
size="small"
onChange={handleChange("Cost")}
defaultValue={values.Cost}
style={{ width: "90%" }}
variant="outlined"
/>
<label
id="cost"
style={{ visibility: "hidden", color: "red" }}
>
Cost of Implementation cannot be left blank
</label>
</Form.Group>
</Grid>
<Grid item xs={6}>
<TextField
placeholder="Benefits of the Best Practice"
label="Benefits of the Best Practice"
size="small"
name="Benefits"
id="Benefits"
onChange={handleChange("Benefits")}
defaultValue={values.Benefits}
style={{ width: "90%" }}
variant="outlined"
multiline
rows={2}
rowsMax={4}
/>
<label
id="benefits"
style={{ visibility: "hidden", color: "red" }}
>
Benefits must be at least 10 characters long
</label>
</Grid>
</Grid>
<Grid
container
direction="row"
justify="left"
alignItems="left"
style={{ width: "83%" }}
>
<Button
variant="contained"
size="small"
color="primary"
style={styles.button}
onClick={this.continue}
>
Continue
</Button>
<Button
variant="contained"
size="small"
color="primary"
type="submit"
style={styles.button}
>
Submit
</Button>
</Grid>
</Form>
</React.Fragment>
</MuiThemeProvider>
);
}
}
I am using material Ui for my code. Please help
The code for Use styles
const useStyles = makeStyles({
root: {
width: "100%",
maxWidth: 500
}
});
Demo
You need to wrap the AppBar components with Toolbar and remove the gutterBottom, something like:
<BrowserRouter>
<AppBar position="static">
<Toolbar>
<NavLink to="dashboard">
<DashboardIcon />
</NavLink>
<Typography
align="center"
style={{ width: "100%", alignItems: "center" }}
>
Best Practices Management System
</Typography>
</Toolbar>
</AppBar>
</BrowserRouter>