Autocomplete react-select bad styles - reactjs

I'm using react-select autocomplete component for my react js web site. I just copied the example code in this link inside to a table cell, but it is not working properly as shown in the below image.
However, it individually works fine. Please help me with this issue. Here is the code.
Autocomplete.jsx has the same code as in the above link.
...
import Autocomplete from './Autocomplete.jsx';
...
const useStyles = makeStyles(theme => ({
textField: {
padding: theme.spacing(),
width: '100%',
},
}));
...
export default function Mapping(props) {
const classes = useStyles();
...
return (
<TableRow>
<TableCell>
<TextField
required
id='outlined-required'
label='URL pattern'
variant='outlined'
value={name}
onChange={event => setName(event.target.value)}
className={classes.textField}
/>
</TableCell>
<TableCell>
<Autocomplete arn={arn} setArn={setArn} />
</TableCell>
{resource.editable ?
<TableCell>
<Button onClick={saveResource}>Save</Button>
<Button onClick={cancel}>Cancel</Button>
</TableCell>
:
<TableCell>
<Button onClick={addResource}>Add</Button>
</TableCell>
}
</TableRow>
);
}
=========================================================================
...
import Mapping from './Mapping.jsx';
...
const useStyles = makeStyles(theme => ({
root: {
paddingBottom: theme.spacing(),
paddingTop: theme.spacing(),
width: '100%',
marginTop: theme.spacing(),
},
mappingsWrapper: {
padding: theme.spacing(),
borderRight: '#c4c4c4',
borderRightStyle: 'solid',
borderRightWidth: 'thin',
},
mappingsHeading: {
display: 'flex',
alignItems: 'center',
},
mappingsTitle: {
paddingLeft: theme.spacing(),
fontSize: '1rem',
paddingTop: theme.spacing(),
paddingBottom: theme.spacing(),
},
}));
...
export default function Mappings() {
const classes = useStyles();
...
return (
<Paper className={classes.root}>
<Grid container item xs={12}>
<Grid xs className={classes.mappingsWrapper}>
<div className={classes.mappingsHeading}>
<Typography className={classes.mappingsTitle}>
Mappings
</Typography>
</div>
<Table stickyHeader>
<TableHead>
<TableRow>
{columns.map(column => (
<TableCell
key={column.id}
align={column.align}
style={{ minWidth: column.minWidth }}
>
{column.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{resources.map((resource) => {
if (resource.editable) {
return (
<Mapping
resource={resource}
resources={resources}
setResources={setResources}
/>
);
} else {
return (
<TableRow>
<TableCell>{resource.name}</TableCell>
<TableCell>{resource.arn}</TableCell>
<TableCell>
<Button onClick={() => editResource(resource)}>Edit</Button>
<Button onClick={() => deleteResource(resource)}>Delete</Button>
</TableCell>
</TableRow>
);
}
})}
<Mapping
resource={{ name: '', arn: '', editable: false }}
resources={resources}
setResources={setResources}
/>
</TableBody>
</Table>
</Grid>
</Grid>
</Paper>
);
}

Related

How to change dropdown in each row of mui table cell in a loop?

import * as React from "react";
import {
Select,
MenuItem,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper
} from "#mui/material";
import KeyboardArrowDownIcon from "#mui/icons-material/KeyboardArrowDown";
function createData(name, mobile, access) {
return { name, mobile, access };
}
const rows = [
createData("Sam", 9865745159),
createData("Amily", 8723879237),
createData("Eva", 9432671262),
createData("Jack", 7898083305),
createData("Diana", 8973667356)
];
export default function DenseTable() {
const [access, setAccess] = React.useState(1);
const handleChange = (event, index, data) => {
setAccess(event.target.value);
};
return (
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} size="small" aria-label="a dense table">
<TableHead>
<TableRow>
<TableCell align="center">Name</TableCell>
<TableCell align="center">Mobile</TableCell>
<TableCell align="center">Access</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow
key={row.name}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell component="th" scope="row" align="center">
{row.name}
</TableCell>
<TableCell align="center">{row.mobile}</TableCell>
<TableCell align="center">
<Select
value={access}
onChange={handleChange}
MenuProps={{
MenuListProps: { disablePadding: true }
}}
fullWidth
size="small"
IconComponent={() => (
<KeyboardArrowDownIcon
sx={{
position: "absolute",
right: 10,
width: "20px",
pointerEvents: "none"
}}
/>
)}
sx={{
fontSize: "14px",
width: "100px",
height: "28px"
}}
>
<MenuItem
value={1}
sx={{
fontSize: "14px",
height: "25px",
width: "100%"
}}
>
Allow
</MenuItem>
<MenuItem
value={2}
sx={{
fontSize: "14px",
height: "30px",
width: "100%"
}}
>
Decline
</MenuItem>
</Select>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
I am using ReactJs with Material UI table with dropdown in each row of the table cell. Whenever I am changing the dropdown option of one row then automatically changes to same option in dropdown of all rows.I have to handle each row separately. How to change dropdown in each row of mui table cell in a loop? file.
Move the dropdown to a new jsx component and manage the state there
function dropdownComponent (){
const [access, setAccess] = React.useState(1);
const handleChange = (event, index, data) => {
setAccess(event.target.value);
};
return
(<Select
value={access}
onChange={handleChange}
MenuProps={{
MenuListProps: { disablePadding: true }
}}
fullWidth
size="small"
IconComponent={() => (
<KeyboardArrowDownIcon
sx={{
position: "absolute",
right: 10,
width: "20px",
pointerEvents: "none"
}}
/>
)}
sx={{
fontSize: "14px",
width: "100px",
height: "28px"
}}
>
<MenuItem
value={1}
sx={{
fontSize: "14px",
height: "25px",
width: "100%"
}}
>
Allow
</MenuItem>
<MenuItem
value={2}
sx={{
fontSize: "14px",
height: "30px",
width: "100%"
}}
>
Decline
</MenuItem>
</Select>
)
}
Call it like this
<TableCell align="center">
<dropdownComponent />

Collapsing Table Row Collapses all Rows

I have a state to check if collapse is true or false
const [open, setOpen] = useState(false);
then returns on render:
<TableBody> {isCityReady && cities ? (cities.map((row, index) => (
<TableRow key={index} sx={{ borderBottom: "2px solid black", "&:last-child td, &:last-child th": { border: 0 } }}>
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => setOpen(!open)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
<TableCell component="th" scope="row" align="left">
<Typography variant="body1" fontWeight="bold">
{row.city}
</Typography>
</TableCell>
<TableCell>
<Collapse in={open} timeout="auto" unmountOnExit>
<Typography variant="body1" fontWeight="bold">
{isReady && locations ? (locations.filter(rowLocation => rowLocation.city === row.city).map((rowLocation, indexLocation) => (
<TableRow key={indexLocation} sx={{ "&:last-child td, &:last-child th": { border: 0 } }}>
<TableCell style={{ width: '100%' }}>
<Typography variant="body1">
{rowLocation.office}
</Typography>
</TableCell>
<TableCell align="right" style={{ width: '100%' }}>
<Button variant="contained" color="primary" onClick={() => handleOpenLocationsDialog("Edit", rowLocation.city, rowLocation.office, rowLocation.id)}>
Edit
</Button>
</TableCell>
</TableRow>
))) : (<LocationsTableSkeleton />)}
</Typography>
</Collapse>
</TableCell>
</TableRow>
))
) : (
<LocationsTableSkeleton />
)}
every row would collapse/uncollapse simultanously. How do I make it so that only the row that was clicked would collpase?
You need to keep a flag for each row.
something like the following
const [openCities, setOpenCities] = useState({});
const toggleCity = useCallback((city)=>{
setOpenCities(state => ({
...state,
[city]: !Boolean(state[city])
}));
},[]);
then returns on render:
<TableBody> {isCityReady && cities ? (cities.map((row, index) => {
// extract the flag/state of the specific city
const open = Boolean(openCities[row.city]);
return (
<TableRow key={index} sx={{ borderBottom: "2px solid black", "&:last-child td, &:last-child th": { border: 0 } }}>
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => toggleCity(row.city)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
......
<TableCell>
<Collapse in={open} timeout="auto" unmountOnExit>
.....

Make entire cell contents clickable MUI table

I've got a MUI table with cells that link to another part of the app when clicked. Rather than wrap the entire row, I'm selectively wrapping certain cells. The link/routing is working - but I'm left with this annoying dead unclickable space due to some styling interactions.
In the picture below I've opened the 'inspect element' view on dev tools to show specifically the dead zone I'm dealing with in the first cell, and to show some mui styling details that might be relevant. Code with styles included further down.
Styles:
const useStyles = makeStyles(theme => ({
table: {
minWidth: 650,
position: 'relative',
fontSize: 10
},
largeIcon: {
width: 60,
height: 60
},
tableContainer: {
minHeight: 320
},
tableBodyContainer: {
minHeight: 265
},
tableHeadRow: {
'& .MuiTableCell-root': {
borderRight: `1px solid ${COLORS.WHITE}`,
borderBottom: `none`,
padding: '8px 5px 8px',
fontSize: 10,
cursor: 'pointer'
}
},
arrow: {
color: theme.palette.grey[500]
},
arrowActive: {
transform: 'rotate(-180deg)',
color: theme.palette.primary.main,
display: 'inline-block'
},
tableRow: {
'& .MuiTableCell-root': {
borderRight: `1px solid ${theme.palette.grey[200]}`,
borderBottom: 'none',
cursor: 'pointer',
padding: 5,
paddingTop: 8,
paddingBottom: 8,
minWidth: 25
},
'& .MuiTableCell-root:first-child': {
border: 'none'
}
},
selectedRow: {
backgroundColor: `${COLORS.SECONDARY} !important`,
'& .MuiTableCell-root': {
color: COLORS.WHITE
}
},
editIcon: {
backgroundImage: `url(${EditIcon})`,
backgroundSize: 'cover',
backgroundPosition: 'center',
width: 18,
height: 18
}
}));
And the table:
return (
<div className={classes.tableContainer}>
<TableContainer className={classes.tableBodyContainer}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow className={classes.tableHeadRow}>
<TableCell />
{tableHeadElements.map(e => (
<TableCell key={e.key} align="center">
{e.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{folders?.items.map((folder: IFolderDTO, index: number) => {
const { id, name, updatedAt } = folder;
return (
<TableRow
className={classes.tableRow}
classes={{ selected: classes.selectedRow }}
selected={selectedRow === id}
onClick={() => setSelectedRow(id)}
key={index}
>
<TableCell align="center" >
<Link to={APP_DASHBOARD_CHILD_FOLDER_CONTENTS_PATH(id)}>
<Box>
<IconButton color="default" size={'medium'}>
<FolderIcon fontSize="default" />
</IconButton>
</Box>
</Link>
</TableCell>
{[name, new Date(updatedAt)].map(cell => (
<TableCell key={index} align="center">
<Link to={APP_DASHBOARD_CHILD_FOLDER_CONTENTS_PATH(id)}>
<Box>{getCorrectFormat(cell)}</Box>
</Link>
</TableCell>
))}
<FolderActionsMenu
folderId={id}
onDeleteFolder={onDeleteFolder}
openDialogWithId={openDialogWithId}
/>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
<FolderFormDialog />
</div>
Kill the dead unclickable space! (please)
Thanks

Onchange is not working when editing cell of material-table in react.js

how can I make an editable cell in React with Material-UI? I'm trying to make cell editable in material UI table but when I pass inputChangeHandler to onChange method it doesn't work as expected, the text just adds to the new row.
snapshot:
the edited data shifted to the new table row
I am using react-hooks useState to store the initial state.
code:
const MyForm = () => {
const [data, setdata] = useState([
{
id: 1,
name: "Frozen yoghurt",
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
},
{
id: 2,
name: "Ice cream sandwich",
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
},
{ id: 3, name: "Eclair", calories: 300, fat: 7.0, carbs: 67, protein: 4 },
{
id: 4,
name: "Velvet cake",
calories: 237,
fat: 2.5,
carbs: 17,
protein: 3,
}
]);
const [editingIndex, setEditingIndex] = useState(-1);
const addNewUserHandler = () => {
let newData = [{
id: "",
name: "",
calories: "",
fat: "",
carbs: "",
protein: ""
}];
const Data = data;
// Data.push(newData)
console.log(newData)
setdata([...Data, newData])
};
const addNewData = e => {
console.log(e);
};
// const editDataHandler = id => {
// // console.log(id);
// // setEditing(true)
// setEditingIndex(id)
// };
const startEditing = i => {
setEditingIndex(i);
};
const stopEditing = () => {
setEditingIndex(-1);
};
const inputChangeHandler = (e) => {
// e.persist();
// console.log(id, e.target.value, "e,target");
setdata([{[e.target.name]: e.target.value}])
};
const submitInputHandler = (e) => {
e.persist();
console.log("this is the input change handler", e.target.value);
};
const deleteRowHandler = id => {
const temp = [...data];
const filteredData = temp.filter(data => data.id !== id);
setdata([...filteredData,]);
};
const classes = useStyles();
return (
<div className={classes.root}>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell>Id.</TableCell>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
<TableCell align="right">
<InputBase
placeholder="search"
inputProps={{ "aria-label": "search" }}
style={{ verticalAlign: "middle" }}
/>
<SearchIcon style={{ verticalAlign: "middle" }} />
</TableCell>
<TableCell align="right">
<Tooltip title="Add data" aria-label="add">
<Fab
color="primary"
className={classes.fab}
onClick={addNewUserHandler}
>
<AddIcon />
</Fab>
</Tooltip>
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map((data, id) => (
<TableRow key={id}>
{editingIndex === data.id ? (
<TableCell component="th" scope="row">
<TextField
style={{ width: "3rem" }}
name="id"
onChange={(e) =>inputChangeHandler(e)}
value={id+1}
/>
</TableCell>
) :
(
<TableCell component="th" scope="row">
{id+1}
</TableCell>
)}
{editingIndex === data.id ? (
<TableCell>
<TextField
style={{ width: "8rem" }}
onChange={(e) =>inputChangeHandler(e)}
name="name"
value={data.name}
/>
</TableCell>
) : (
<TableCell style={{ textAlign: "center" }}>
{data.name}
</TableCell>
)}
{editingIndex === data.id ? (
<TableCell align="center">
<TextField
style={{ width: "8rem" }}
onChange={(e) =>inputChangeHandler(e)}
name="calories"
value={data.calories}
/>
</TableCell>
) : (
<TableCell style={{ textAlign: "center" }}>
{data.calories}
</TableCell>
)}
{editingIndex === data.id ? (
<TableCell>
<TextField
style={{ width: "8rem" }}
onChange={(e) =>inputChangeHandler(e)}
name="fat"
value={data.fat}
/>
</TableCell>
) : (
<TableCell style={{ textAlign: "center" }}>
{data.fat}
</TableCell>
)}
{editingIndex === data.id ? (
<TableCell align="center">
<TextField
style={{ width: "8rem" }}
onChange={(e) =>inputChangeHandler(e)}
name="carbs"
value={data.carbs}
/>
</TableCell>
) : (
<TableCell style={{ textAlign: "center" }}>
{data.carbs}
</TableCell>
)}
{editingIndex === data.id ? (
<TableCell align="center">
<TextField
disabled={false}
style={{ width: "8rem" }}
onChange={(e) =>inputChangeHandler(e)}
name="protein"
value={data.protein}
/>
</TableCell>
) : (
<TableCell style={{ textAlign: "center" }}>
{data.protein}
</TableCell>
)}
<TableCell style={{ textAlign: "center" }}>
{editingIndex !== data.id ? (
<EditIcon onClick={() => startEditing(data.id)} style= {{cursor: "pointer"}}/>
) : (
<CheckIcon onClick={submitInputHandler} style= {{cursor: "pointer"}} />
)}
</TableCell>
<TableCell>
{editingIndex !== data.id ? (
<DeleteIcon onClick={() => deleteRowHandler(data.id)} style= {{cursor: "pointer"}}/>
) : (
<CloseIcon onClick={stopEditing} style= {{cursor: "pointer"}} />
)}
</TableCell>
</TableRow>
))}
{/* <TableRow>
<TablePagination
count={rows.length}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
/>
</TableRow> */}
</TableBody>
</Table>
</Paper>
</div>
);
};
export default MyForm;
const inputChangeHandler = (e, id) => {
let result = data.map((data, id) =>{
return data.id == id ? {...data, name:e.target.value} : {...data}
})
setdata(result)
}
//then
onChange={(e) =>inputChangeHandler(e, data.id)}

How to center out table on a Material UI Paper element

The problem I have is I try to center out the fixed-sized table in the middle of the page (in a Paper component) and fail miserably. I am also quite new in Material-ui, so I am not sure this is a proper way for structuring this kind of items. Can you please help me centering it out in the middle of the page?
import rows from "./mockdata/mock_dashboard";
const rowsData = rows;
const useStyles = makeStyles(theme => ({
root: {
width: "100%"
},
paper: {
marginTop: theme.spacing(3),
width: "100%",
overflowX: "auto",
marginBottom: theme.spacing(2),
margin: "auto"
},
table: {
minWidth: 650,
maxWidth: 1200
}
}));
export default function MainDashboard() {
const classes = useStyles();
return (
<div className={classes.root}>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell>Nz.</TableCell>
<TableCell>Data Przyjęcia</TableCell>
<TableCell>Koordynator</TableCell>
<TableCell>Link do Pliku</TableCell>
<TableCell>Dod. opis</TableCell>
<TableCell>Data Wykonania</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rowsData.map(row => (
<TableRow key={row.orderNo}>
<TableCell component="th" scope="row">
{row.orderNo}
</TableCell>
<TableCell align="center">{row.orderDate}</TableCell>
<TableCell align="center">{row.coordinator}</TableCell>
<TableCell align="center">{row.link}</TableCell>
<TableCell align="center" className={classes.descriptionFont}>
{row.description}
</TableCell>
<TableCell align="center">{row.dueDate}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
</div>
);
What about wrapping table in fixed Container and get rid of fixed table width? It is basic element for Material-ui layout.
import Container from '#material-ui/core/Container';
import rows from "./mockdata/mock_dashboard";
const rowsData = rows;
const useStyles = makeStyles(theme => ({
root: {
width: "100%"
},
paper: {
marginTop: theme.spacing(3),
width: "100%",
overflowX: "auto",
marginBottom: theme.spacing(2),
margin: "auto"
},
table: {
width: '100%',
}
}));
export default function MainDashboard() {
const classes = useStyles();
return (
<Container fixed>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell>Nz.</TableCell>
<TableCell>Data Przyjęcia</TableCell>
<TableCell>Koordynator</TableCell>
<TableCell>Link do Pliku</TableCell>
<TableCell>Dod. opis</TableCell>
<TableCell>Data Wykonania</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rowsData.map(row => (
<TableRow key={row.orderNo}>
<TableCell component="th" scope="row">
{row.orderNo}
</TableCell>
<TableCell align="center">{row.orderDate}</TableCell>
<TableCell align="center">{row.coordinator}</TableCell>
<TableCell align="center">{row.link}</TableCell>
<TableCell align="center" className={classes.descriptionFont}>
{row.description}
</TableCell>
<TableCell align="center">{row.dueDate}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
</Container>
);
}

Resources