mui table is not responsive - reactjs

I am using mui table. It is not responsive. I need to set the width in %.
The output is
I attach the screenshort from two different screen

Create a div container with overflow: auto, then another container
with: width: '100%', display: 'table', tableLayout: 'fixed'
And that's all.
<div className="App">
<Box sx={{ overflow: "auto" }}>
<Box sx={{ width: "100%", display: "table", tableLayout: "fixed" }}>
<Table>
<TableHead>
<TableRow>
<TableCell>
<TableSortLabel>Name</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>User</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>Title</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>Role</TableSortLabel>
</TableCell>
<TableCell align={"right"}></TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
component={"a"}
sx={{ cursor: "pointer" }}
>
Darwling Jackson
</Link>
</Box>
</Box>
</TableCell>
<TableCell>DJackson</TableCell>
<TableCell>Tester</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
sx={{ cursor: "pointer" }}
>
Jhon Phoneix
</Link>
</Box>
</Box>
</TableCell>
<TableCell>JP</TableCell>
<TableCell>Database Administrator</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
sx={{ cursor: "pointer" }}
>
Maria Dominguez
</Link>
</Box>
</Box>
</TableCell>
<TableCell>MDominguez</TableCell>
<TableCell>IT Manager</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
</TableBody>
</Table>
</Box>
</Box>
</div>
See my example: Mui responsive code sandbox

Can't really demonstrate without code, but if you don't want to let Material UI to determine the width, a common strategy is to wrap the component in a Box, and give the width (in percent) to the Box instead. Reference.

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 />

In material-table/core I want to display a list of data that is inside my rowData as detailPanel

In my table each row has a list of attachments that I need to display in detailPanel.
This is my code of details panel
detailPanel={(rowData) => {
return rowData?.document_attachment ? rowData?.document_attachment.map((attachment) => {
return <Box
sx={{ m: 1 }}
onClick={() => handleClickPreviewDoc(attachment)}
>
<Typography sx={{ fontSize: "13px", display: "Flex", alignItems: "center", gap: "10px" }}
>
<FilePresentIcon sx={{ fontSize: "13px", opacity: "0.6" }} />
{attachment.name}
</Typography>
</Box>
}):<Box
sx={{ m: 1 }}
>
<Typography sx={{ fontSize: "13px", display: "Flex", alignItems: "center", gap: "10px" }}
>
No Attachment Found
</Typography>
</Box>
}}
This does not display any attachment even though it is inside the data.
Okay this is for someone who also had this problem, in order to access the list inside the rowData you have to write rowData.rowData.
so correct code is:
detailPanel={(rowData) => {
return rowData?.rowData?.document_attachment ? rowData?.rowData?.document_attachment.map((attachment) => {
return <Box
sx={{ m: 1 }}
onClick={() => handleClickPreviewDoc(attachment)}
>
<Typography sx={{ fontSize: "13px", display: "Flex", alignItems: "center", gap: "10px" }}
>
<FilePresentIcon sx={{ fontSize: "13px", opacity: "0.6" }} />
{attachment.name}
</Typography>
</Box>
}):<Box
sx={{ m: 1 }}
>
<Typography sx={{ fontSize: "13px", display: "Flex", alignItems: "center", gap: "10px" }}
>
No Attachment Found
</Typography>
</Box>
}}
This problem might only be in material-table/core.

Is there a way to disable React-Beatiful-DND from outlining focused draggable items when I shift+click?

I built my own multi-selecting code to use with react-beautiful-dnd but I can't seem to disable the outline. It seems to be built into the draggable component. It only seems to happen when I shift + click on an item in the list.enter image description here
<Draggable key={_id} draggableId={`${_id} + ${menu}`} index={index} isDragDisabled={false} >
{(provided) => (
<Card
className="noselect"
onClick={(e) => handleClick(e, menu, index)}
sx={{
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
height: 60,
width: 400,
marginTop: 1,
alignItems: "center",
backgroundColor: selected ? theme.palette.third.main : "white",
}}
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
<Divider
sx={{
backgroundColor: moveSelectedToggle ? "black" : "",
width: "100%",
}}
/>
<Grid container sx={{ alignItems: "center" }}>
<Grid
item
xs={2}
sx={{
backgroundColor: getColor("bg"),
borderRadius: 15,
maxWidth: 30,
height: 30,
display: "flex",
alignItems: "center",
justifyContent: "center",
marginRight: 2,
}}
>
<Typography color={getColor("text")} sx={{}}>
{index + 1}
</Typography>
</Grid>
<Grid item xs={7}>
<Typography>{SADDRESS}</Typography>
</Grid>
<Grid item xs={3}>
<Box sx={{display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
<Typography>{ROUTXT}</Typography>
<Typography>{SEQUENCE}</Typography>
</Box>
</Grid>
</Grid>
<Divider />
</Card>
)}
</Draggable>

How to stretch the card according to the content displayed in reactjs?

I'm new to material UI framework, i want to stretch the grid accordingly displayed in the content. I couldn't able to do that. Can anyone help me in this?
<Card>
<CardHeader
avatar={<Avatar aria-label="recipe">S</Avatar>}
title={
<>
<InputBase placeholder="Search Google Maps" margin="normal" />
<IconButton type="submit" aria-label="search">
<SearchIcon />
</IconButton>
</>
}
/>
<Divider />
<CardContent
style={{ overflow: "scroll" }}
className={classes.contentHeight}
id="chatList"
>
<div>
<Message isSender content="Hello" />
{this.state.data.map(item => {
if (item.isSender) {
return (
<Message isSender name="Sanjana" content={item.content} />
);
}
return <Message name="Sanjana" content={item.content} />;
})}
</div>
</CardContent>
<Divider />
<CardActions>
<Paper className={classes.contentPaper}>
<Input
margin="dense"
className={classes.input}
placeholder="Enter a Message"
disableUnderline
/>
</Paper>
</CardActions>
</Card>
Can anyone help me in this query?
Thanks in advance!
Remove width: 100
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
width: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
should be
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
because the width: 100 gives it a width of 100px.
To make the content to stretch full width, you can use width: "fit-content" and assign your width value of 100 to minWidth: 100.
So your card will look like,
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
diaplay: "block",
minHeight: 40,
width: "fit-content",
minWidth: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
To make the content left align inside card, you can assign style to Grid like,
<Grid style={{ paddingBottom: 8, textAlign: "left" }} item xs={12}>
<span>{name}</span>
</Grid>
Forked sandbox

How to align table pagination internal components (captions,Actions)

I was able to center the 'entire' table pagination but I couldn't align the internal components of tablePagination from MaterialUI separately with the code below.I would like to center the Pagination caption and 'right' action components.
Pagination: {
display:'flex',
justifyContent: 'center',
width: "100%",
alignItems: 'left',
padding:'0px',
},
PaginationCaption: {
color: 'white',
fontSize: '1.8rem',
flexShrink: 0,
// width: '50%',
textAlign: 'center',
},
PaginationSelectIcon: {
color: 'white',
fontSize: '1.8rem'
},
PaginationSelect: {
color: 'white',
fontSize: '1.8rem'
},
PaginationActions: {
color: 'white',
fontSize: '1.8rem',
// width: '50%',
textAlign: 'right',
}
<Grid container
alignItems="center"
justify="center"
>
<Grid item xs={12} style={{height:'38vh'}}>
<div className={classes.tableWrapper}>
<Table className={classes.table}>
<TableHead style={{backgroundColor:'#4e4e4e'}}>
<TableRow >
<TableCell component="th" scope="row" className=
{classes.tablecell} style={{width: '20%'}}> License Plate</TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> Status</TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> IN </TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> OUT </TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> Time on-site</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.slice(page * rowsPerPage, page * rowsPerPage +
rowsPerPage).map(row => (
<TableRow key={row.name} >
<TableCell component="th" scope="row" className=
{classes.tablecell}>
{row.name}
</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.calories}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.fat}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.calories}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.fat}</TableCell>
</TableRow>
))}
{emptyRows > 0 && (
<TableRow style={{ height: 48 * emptyRows }}>
<TableCell colSpan={6} />
</TableRow>
)}
</TableBody>
</Table>
</div>
<TablePagination
component="div"
className={classes.Pagination}
classes={{
// root: classes.Pagination,
caption: classes.PaginationCaption,
selectIcon: classes.PaginationSelectIcon,
select: classes.PaginationSelect,
actions: classes.PaginationActions,
}}
// rowsPerPageOptions={[5, 7, 10]}
colSpan={5} // Important!!!!
count={rows.length}
rowsPerPageOptions={[]} //added
rowsPerPage={rowsPerPage}
page={page}
SelectProps={{
inputProps: { 'aria-label': 'Rows per page' },
native: true,
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
ActionsComponent={TablePaginationActions}
/>
</Grid>
</Grid>
I've also have looked into the source code of tablePagination and tried the css styles there as well. But the results are the same.

Resources