Set grey background around main area - reactjs

I am trying to get the look of this app using material ui:
Currently, my app looks like this:
There are a few things I need to do. Put some kind of box around the Keyword Processor text, put a box around the stuff below it, set background colour of central area boxes to white and have the background area of the rest to grey.
Any ideas how to modify my code below to do this?
<ThemeProvider theme={theme}>
</ThemeProvider>
<div>
<ThemeProvider theme={theme}>
<Container>
{/* <Box bgcolor="primary.main"> */}
<Grid container spacing={2}>
{/* <Grid xs={12}>
<AppBar />
<br />
</Grid> */}
<Grid item xs={12}>
<Typography variant="h6" gutterBottom>
Keyword Processor
</Typography>
{/* <br /> */}
</Grid>
<Grid item xs={6}></Grid>
<Grid item xs={6}>
<Box textAlign="right">
<Button
color="primary"
// mt={5}
onClick={this.inputToOutput}
//**********************
>
A-Z
</Button>
</Box>
{/* <Typography align="right">A-Z Frequency</Typography>{' '} */}
</Grid>
<Grid item xs={6}>
<TextField
label="Input Keywords"
fullWidth
multiline
rows={10}
variant="outlined"
margin="normal"
onChange={this.handleInputText}
defaultValue={defaultInputText}
/>
<br />
<Button
variant="contained"
color="primary"
mt={5}
onClick={this.inputToOutput}
//**********************
>
Parse
</Button>
<Button
variant="contained"
color="secondary"
mt={5}
// onClick={this.parseInput}
>
Clear
</Button>
<br />
<br />
<Checkboxes
handleDedupe={this.handleDedupe}
handleRemoveNumbers={this.handleRemoveNumbers}
handleConvertToLowercase={this.handleConvertToLowercase}
handleOneWordPerLine={this.handleOneWordPerLine}
handleAddCommas={this.handleAddCommas}
handleAddCommasSpace={this.handleAddCommasSpace}
/>
</Grid>
<Grid item xs={6}>
<TextField
label="Output Keywords"
fullWidth
multiline
rows={30}
variant="outlined"
margin="normal"
value={this.state.outputText}
/>
<Button
variant="contained"
color="primary"
mt={5}
onClick={this.clearOutput}
>
Copy
</Button>
<Button
variant="contained"
color="secondary"
mt={5}
onClick={this.clearOutput}
>
Clear
</Button>
</Grid>
<Grid>
<br />
<br />
</Grid>
</Grid>
{/* </Box> */}
</Container>
</ThemeProvider>
</div>
</>```

You don't need to do that all, here is what you can do instead. I believe the theme you added to ThemeProvider is custom, the edit the theme file pallete object to background colors i.e paper and default, the default color is what you need.
Here is mine:
import { createMuiTheme } from '#material-ui/core/styles';
// Main Material UI theme
const theme = createMuiTheme({
palette: {
background: {
paper: '#fff',
default: '#fafafa',
},
},
});
export default theme;
Change '#fafafa' to the color you want.
If you didn't create a custom theme, just copy mine then import it in your ThemeProvider, it will change the background of your body in all your html pages.

Related

MUI Accordion - Adjusting the Accordion Details height with dynamic content

I am using MUI Accordion. In the Accordion details I have multiline textfield component to capture user responses and a button at the bottom for saving the data. Now if the user is keying in multiple lines, the button moves down and then disappears. Accordion details is not adjusting the height dynamically with the increase in the height for textfield component. If the user collapse and expand the accordion again the height readjusts.
How can I adjust the height of the accordion details dynamically as the text field contents grows ?
<AccordionDetails key={"SubCatIDDetails_" + this.props.SubCategoryID} >
{quesSubCategories.map((quesSubCat) => (
<>
<Grid container spacing={2} marginTop={1} marginBottom={1} textAlign={'Left'}>
<Grid item xs={3}>
<Typography> {quesSubCat.question}
{quesSubCat.hint.length > 0 && <HtmlTooltip
title={<Typography color="inherit"><i>{quesSubCat.hint}</i></Typography>}>
<IconButton>
<InfoIcon color="primary" />
</IconButton>
</HtmlTooltip>}
</Typography>
</Grid>
<Grid item xs={3.5}>
<TextField multiline fullWidth label="Response" name="Response" defaultValue={quesSubCat.response || ''} onBlur={(event) => this.props.handler([quesSubCat.quesID, quesSubCat.responseID, event.target.value, event.target.name])} />
</Grid>
<Grid item xs={3}>
<TextField multiline fullWidth label="Notes" name="Notes" onBlur={(event) => this.props.handler([quesSubCat.quesID, quesSubCat.responseID, event.target.value, event.target.name])} />
<NotesDetails Updated={this.props.UpdatingResponse} NoteType="Question" QuestionID={quesSubCat.quesID} ApplicationID={this.props.ApplicationID} userEmail={this.props.userEmail} />
</Grid>
<Grid item xs={2.5}>
<Status currentStatus={quesSubCat.quesStatus} QuesCat={quesSubCat.quesID} ResponseID={quesSubCat.responseID} currentTargetDt={quesSubCat.targetDate} handlerChange={this.HandleStatusChange} />
</Grid>
</Grid>
<Divider style={{ background: '#c4540c' }} />
</>
))}
{this.props.UpdatingResponse && <p style={{ marginLeft: '15px' }}><em>Updating...</em></p>}
<Button style={{ marginTop: '10px', marginBottom: '10px', marginLeft: '10px' }} color="warning" variant="contained" startIcon={<SaveIcon />} onClick={this.props.handlerClick}>Save & Continue</Button>
</AccordionDetails>

Space between two buttons in Material Design

I have two buttons and would like to put a space between them. So, I have tried to do that by putting a box around them and then applying a margin to the box. See code below.
render() {
return (
<>
<ThemeProvider theme={theme}>
<CssBaseline />
<BrowserRouter>
<Header />
</BrowserRouter>
</ThemeProvider>
<ThemeProvider theme={theme}>
<Grid container justify="center">
<Box pt={3}>
<Paper>
<Box pl={3} pr={3} pb={3} pt={3}>
<Grid container spacing={0}>
<Grid item xs={6}>
<Typography
variant="h6"
gutterBottom={false}
onClick={this.handleSetDummyText}
>
Keyword Processor
</Typography>
</Grid>
<Grid item xs={6}>
{/* ********* Offending code section ********** */}
<Button
color={
this.state.sorttypeStatus === 'default'
? 'primary'
: 'secondary'
}
onClick={this.handleSortDefault}
>
DEFAULT
</Button>
{/* ********* Offending code section ********** */}
<Button
color={
this.state.sorttypeStatus === 'alpha'
? 'primary'
: 'secondary'
}
onClick={this.handleSortAlpha}
>
A-Z
</Button>
<Button
color={
this.state.sorttypeStatus === 'length'
? 'primary'
: 'secondary'
}
onClick={this.handleSortLength}
>
LENGTH
</Button>
</Grid>
<Grid container spacing={2}>
<Grid item xs={6}>
<TextField
label="Input Keywords"
fullWidth
multiline
rows={10}
variant="outlined"
margin="normal"
onChange={this.handleInputText}
// defaultValue={defaultInputText}
value={this.state.inputText}
/>
<Box m={1}>
<Button
variant="contained"
color="primary"
onClick={this.inputToOutput}
>
Parse
</Button>
<Button
variant="outlined"
color="secondary"
onClick={this.clearInput}
// default={this.state.inputText}
>
Clear
</Button>
</Box>
<br />
<Checkboxes
handleDedupe={this.handleDedupe}
handleRemoveNumbers={this.handleRemoveNumbers}
handleConvertToLowercase={
this.handleConvertToLowercase
}
handleOneWordPerLine={this.handleOneWordPerLine}
handleAddCommas={this.handleAddCommas}
handleAddCommasSpace={this.handleAddCommasSpace}
handleStartWords={this.handleStartWords}
handleEndWords={this.handleEndWords}
/>
</Grid>
<Grid item xs={6}>
<TextField
label="Output Keywords"
fullWidth
multiline
rows={30}
variant="outlined"
margin="normal"
value={this.state.outputText}
/>
<Button
variant="contained"
color="primary"
mt={5}
onClick={() => {
navigator.clipboard.writeText(
this.state.outputText
);
}}
>
Copy
</Button>
<Button
variant="outlined"
color="secondary"
mt={5}
onClick={this.clearOutput}
>
Clear
</Button>
<Button
variant="outlined"
color="secondary"
mt={5}
onClick={this.handleSetDummyText}
>
Dummy Data
</Button>
</Grid>
</Grid>
</Grid>
</Box>
</Paper>
</Box>
</Grid>
</ThemeProvider>
</>
);
}
}
But the boxes seem to stick together like glue! I do have a custom theme but I cannot seem to get that to work with my App.js file because I am using a class component, rather than a functional component. I understand that you are supposed to use withStyles with class components, but I have struggled to get that to work. So, as a fix, I am trying the Box method.
Any idea how to get my box method to work?
you could use Grid component of material ui and as justify content use space-between
<Grid
style={{width:'140px'}}
container
direction="row"
justify="space-between"
alignItems="center">
<Button/>// here are the buttons
<Button/>
</Grid>
read more about the Grid https://material-ui.com/components/grid/
or you could do it with <div/> i used inline style so that you can read it
easier
<div style ={{ width:'200px',
display:'flex',
flexDirection:'row',
justifyContent:'space-between'
}}>
<button>button 1</button>
<button>button 2</button>
</div>
see working examples with div https://codesandbox.io/s/youthful-taussig-jyesc?file=/src/App.js:67-263
Have you tried to put each button in a box?
<div>
<Box m={1}>
<Button variant="contained">one</Button>
</Box>
<Box m={1}>
<Button variant="contained">two</Button>
</Box>
</div>
The <Box> component works like a <div>, it is a block, so your code can not align buttons in a row.
You can use flex-box to style to get what you want. Use Grid item, container and spacing API of Grid fit your need as well.
Below is an example:
<Grid container spacing={4}>
<Grid item xs={2}>
<Button fullWidth variant="contained" color="primary">
button A{" "}
</Button>
</Grid>
<Grid item xs={2}>
<Button fullWidth variant="contained" color="primary">
button B{" "}
</Button>
</Grid>
</Grid>
When using <Grid container>, you create a flex-box container. By default on ReactJS, the flex-items inside will be aligned by row. That's the reason why We do not have to declare props direction="row" on the container.
You can change the spacing value to change the space between two Grid items. By default, 1 = 8px and it can be customized in material ui as well.
xs indicates the size of your screen, in material ui we have 5 of them xs, sm, md, lg and xl, you can read about it in material ui docs.
fullWidth buttons just to make the buttons take the whole width of containers for ease of seeing space between buttons.
Working example on codesandbox: https://codesandbox.io/s/angry-wescoff-lnj71?file=/src/App.js

How to align material-ui Grid items vertically centered?

I have a Grid container and Buttons as it's children (Grid-items). I want to align Grid-items vertically centered.
Here is the visual representation of my requirement
Here is the markup
<Box height="10vh" mr={4}>
<Grid container justify="flex-end" spacing={2}>
<Button variant="contained" color="default" type="reset">
Reset
</Button>
<Button
type="submit"
variant="contained"
color="primary"
onClick={() => handleSubmit()}
>
Search
</Button>
</Grid>
</Box>;
Can anybody tell me a solution based on material-ui grid API?
I figured out, this should work,
const useStyles = makeStyles({
grid: {
height: "100%"
}
});
export default function Hook() {
const classes = useStyles();
return (
<Box height="10vh" mr={4}>
<Grid
className={classes.grid}
container
justify="flex-end"
alignItems="center"
spacing={2}
>
<Button variant="contained" color="default" type="reset">
Reset
</Button>
<Button
type="submit"
variant="contained"
color="primary"
onClick={console.log}
>
Search
</Button>
</Grid>
</Box>
);
}
Working sandbox, https://codesandbox.io/s/material-demo-forked-m7fyj?file=/demo.js
<Grid
container
direction="row"
justify="flex-end"
alignItems="center"
>
I recommend you to try the Interactive Demo

Why does AccordionSummary background change to grey when clicking inside TextField in Material-UI?

I am using Material-UI's Accordion and inside AccordionSummary I have a <TextField /> (which is actually controlled by Formik and formik-material-ui) and the background of AccordionSummary turns grey when clicking inside the field only. I don't want it to do this and it doesn't appear to be anything configured by the Accordion API, unless I am missing something. How can I stop it from behaving this way?
I've recreated the issue on Codesandbox.
Here's the full code from the sandbox:
import React from "react";
import { Formik, Field, Form, ErrorMessage } from "formik";
import { TextField, CheckboxWithLabel, Select } from "formik-material-ui";
import Grid from "#material-ui/core/Grid";
import Box from "#material-ui/core/Box";
import FormControl from "#material-ui/core/FormControl";
import MenuItem from "#material-ui/core/MenuItem";
import Button from "#material-ui/core/Button";
import Accordion from "#material-ui/core/Accordion";
import AccordionDetails from "#material-ui/core/AccordionDetails";
import AccordionSummary from "#material-ui/core/AccordionSummary";
export default function App() {
return (
<div className="App">
<Formik>
<Form>
<Accordion>
<AccordionSummary>
<FormControl fullWidth>
<Field
component={TextField}
name="itemName"
placeholder="What do you want to buy?"
variant="outlined"
/>
</FormControl>
</AccordionSummary>
<AccordionDetails>
<Grid container>
<Grid item xs={2}>
<Box pt={2}>
<FormControl fullWidth>
<Field
component={TextField}
name="quantity"
label="Qty"
type="number"
variant="outlined"
size="small"
/>
</FormControl>
</Box>
</Grid>
<Grid item xs={2}>
<Box pt={2} pl={1}>
<FormControl fullWidth>
<Field
component={TextField}
name="volume"
label="Vol"
type="number"
variant="outlined"
size="small"
/>
</FormControl>
</Box>
</Grid>
<Grid item xs={2}>
<Box pt={2} pl={1}>
<FormControl label="Type" size="small" fullWidth>
<Field
component={Select}
name="volumeType"
as="select"
variant="outlined"
>
<MenuItem value="g">grams</MenuItem>
<MenuItem value="kg">kg</MenuItem>
<MenuItem value="ml">ml</MenuItem>
<MenuItem value="cl">cl</MenuItem>
<MenuItem value="litre">litre</MenuItem>
<MenuItem value="pint">pint</MenuItem>
<MenuItem value="pack">pack</MenuItem>
</Field>
</FormControl>
</Box>
</Grid>
<Grid item xs={4}>
<Box pt={2} pl={1}>
<FormControl fullWidth>
<Field
component={TextField}
name="brandName"
label="Brand"
variant="outlined"
size="small"
/>
</FormControl>
</Box>
</Grid>
<Grid item xs={2}>
<Box pt={2} pl={1}>
<Button
type="submit"
variant="contained"
color="primary"
disableElevation
fullWidth
>
Add
</Button>
</Box>
</Grid>
</Grid>
</AccordionDetails>
</Accordion>
</Form>
</Formik>
</div>
);
}
What you are seeing is the focused styling for AccordionSummary. You can observe this in the Accordion demos as well by clicking on an accordion and then using the Tab key to move to the next AccordionSummary. This is there for accessibility reasons so that the user can tell where focus is within the Accordion when navigating with the keyboard.
You can override this with the following:
import MuiAccordionSummary from "#material-ui/core/AccordionSummary";
import { withStyles } from "#material-ui/core/styles";
const AccordionSummary = withStyles({
root: {
"&.Mui-focused": {
backgroundColor: "inherit"
}
}
})(MuiAccordionSummary);

Cannot load modal with different data

I am using material ui in react and after building the modals it loads only the last element of the object array.
<Grid container className={classes.root} spacing={2}>
<Grid item xs={12}>
<Grid container justify="center" spacing={spacing}>
{Object.keys(props.listOfAndis).map((keyName, i) => (
<Grid item>
<Card className={classes.card}>
<CardContent>
<Typography>Word of the Day {i}</Typography>
<Typography>be{bull}nev{bull}o{bull}lent</Typography>
<Typography>adjective</Typography>
<Typography>well meaning and kindly.<br/>{'"a benevolent smile"'}</Typography>
</CardContent>
<CardActions>
<div>
<Button size="small" onClick={handleOpen}>View Profile</Button>
</div>
<Modal
aria-labelledby="simple-modal-title"
aria-describedby="simple-modal-description"
open={open}
onClose={handleClose}>
<div style={modalStyle} className={classes.paper}>
<IconButton onClick={handleClose} tooltip="Close"
style={pointer && topRight}> X </IconButton>
<h2 id="simple-modal-title">Current
Project: {props.listOfAndis[keyName].currentProject}</h2>
<p id="simple-modal-description">Name: {props.listOfAndis[keyName].name}</p>
<p id="simple-modal-description">E-mail: {props.listOfAndis[keyName].emailAddress}</p>
<p id="simple-modal-description">Skills: {props.listOfAndis[keyName].skills}</p>
</div>
</Modal>
</CardActions>
</Card>
</Grid>
))}
</Grid>
</Grid>
</Grid>
Currently I have 3 mock entries, but only the last one is used for all three modals.
I have tried using key={i} with no success. What am I doing wrong?

Resources