React - Material UI: How to remove scrollbar from table - html

I've built a simple table with react and material UI with these instructions: https://material-ui.com/components/tables/#table.
It works fine but the scrollbar bothers me.
Is there an option to let the scrollbar start at the red arrow? Or remove it entirely?
Thank you in advance
code
<TableContainer component={Paper} style={{maxHeight: 350}}>
<Table className={styles.table} size="small" stickyHeader>
<TableHead>
<TableRow >
<TableCell className={styles.header}>
<Checkbox checked={allSelected} onClick={handleSelectAll} color="primary"/>
</TableCell>
<TableCell className={styles.header} align="left">Name</TableCell>
{props.showAdmin && <TableCell className={styles.header}>Admin</TableCell>}
</TableRow>
</TableHead>
<TableBody>
{props.employees.map(empl => (
<TableRow key={empl.id}>
<TableCell>
<Checkbox checked={isSelected(empl.id)} onClick={() =>handleSelect(empl.id)} className={styles.checkBox} color="primary"/>
</TableCell>
<TableCell component="th" scope="row" style={{paddingRight: 30}}>{empl.name}</TableCell>
{props.showAdmin && <TableCell align="center"><Checkbox disabled checked={empl.isAdmin} className={styles.checkBox}/></TableCell>}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
style
createStyles({
table: {
maxWidth: 350,
maxHeight: 300
},
header: {
backgroundColor: '#123456',
color: '#ffffff',
fontSize: 18
},
checkBox: {
paddingTop: 1,
paddingBottom: 1,
}
}),
);

If you remove the maxHeight style for TableContainer, the scroll would disappear.
<TableContainer component={Paper} style={{ maxHeight: 350 }}>
to
<TableContainer component={Paper}>
Update
If you want to scroll from below header, simply add the related CSS to material-ui component Table and TableBody would be fine.
table: {
display: "block",
maxWidth: 350,
},
body: {
display: "block",
overflow: "auto",
height: "300px"
},
Refer:
how-to-set-tbody-height-with-overflow-scroll
how-do-i-completely-fill-a-table-100-with-tbody-in-html
Try it online:

to remove scroll bars using jss material ui, simple do this
container_with_scrolls:{
overflowX:'scroll',
'&::-webkit-scrollbar':{
width:0,
}
},

Related

Why does the table rows increment on component re-render?

I created a component that accepts a label and an array and then returns a table for displaying the data that was passed. But for some reason, whenever the component re-renders, the table rows increments because the data repeats and displays it again below the table. Here's my code...
import React from 'react';
interface Props {
labels: any[];
data: any[];
}
const sampleData = [
{
rowLabel: 'Fever',
key: 'key',
value: 'value',
},
{
rowLabel: 'Colds',
key: 'key',
value: 'value',
},
];
const Display: React.FC<Props> = ({ labels, data }: Props) => {
return (
<>
<table
style={{
direction: 'ltr',
width: '100%',
flexDirection: 'row',
}}
>
{labels.map((label, index) => (
<tr key={index} style={{ border: '2px solid #e8e8e9' }}>
<td style={{ border: '2px solid #e8e8e9', textAlign: 'center' }}>
{label}
</td>
{data.map((data) => {
return data.rowLabel === label ? (
<tr
key={index}
style={{
display: 'flex',
flexDirection: 'row',
borderInline: '1px solid #e8e8e9',
borderBottom: '1px solid #e8e8e9',
paddingLeft: '5px',
}}
>
<td
style={{
width: '50%',
textOverflow: 'ellipsis',
}}
>
{data.key}
</td>
<td
style={{
borderLeft: '2px solid #e8e8e9',
width: '50%',
textAlign: 'right',
paddingRight: '5px',
}}
>
{data.value}
</td>
</tr>
) : null;
})}
</tr>
))}
</table>
</>
);
};
export default Display;
And here is me calling the component
<Display
labels={['Fever','Colds']}
data={sampleData}
/>
Im still a noob and any help would be greatly appreciated, thank you!

Change text color of table cell on hovering table row

I want to change color text of each row eg. red, when hovered on each row of a table.
I tried &:hover with color property, but it's not working.
<TableRow
key={row.id}
sx={{
"&:hover": {
color: "red" // not working
}
// "& .MuiTableCell-root:hover":{ // --- on hover only hovered cell text color is changing
// color:"red"
// }
}}
>
<TableCell align="left">{row.category}</TableCell>
<TableCell align="left">{row.name}</TableCell>
<TableCell align="left">
<IconButton aria-label="delete">
<span className="material-icons-outlined">
delete_forever
</span>
</IconButton>
</TableCell>
</TableRow>
MuiTableCell-root override is done on <TableRow>, but this is affecting only each cell, but not entire row.
I need color to be changed on hovering each row? how to do that?
CodeSandbox Demo.
Solution:
<TableRow
key={row.id}
sx={{
"&:hover .MuiTableCell-root, &:hover .material-icons-outlined": {
color: "red"
}
}}
>
UPDATED: Do this inside your TableRow
<TableRow
key={row.id}
sx={{
"&:hover .MuiTableCell-root, &:hover .MuiTableCell-root span.material-icons-outlined": {
color: "red"
}
}}
>
{/* some code */}
</TableRow>
You can do it by
<Table
sx={{
"& .MuiTableCell-root:hover": {
color: "red"
}
}}
/*...other code is omitted for the brevity*/
or by creating styles:
const styles = theme => ({
tableCell: {
"$hover:hover &": {
color: "red"
}
},
hover: {}
});
and then:
<TableCell
className={classes.tableCell}
component="th"
scope="row"
>
{row.category}
</TableCell>
use "&:hover .MuiTableCell-root" with color red

Material Ui React - navbar not aligning box to right

I have a navbar that is suppose to align all the page options to the right. However, it does not do this. The entire toolbar is a flexbox and I've tried using alignSelf: end but this would only move the text slightly downwards.
I am not sure why it does this because the orientation I've chosen for the toolbar is row and not column.
I have tried removing the options that appear when the screen resizes but this did not work either.
Below is the code that is the issue. I've also commented as to which box is causing the issue. (comment labeled ISSUE)
{/* Issue */}
{/* ABOUT, PROJECTS, CONTACT - full screen */}
<Box sx={{ display: { xs: 'none', md: 'flex' }, alignItems: "flex-end" }}>
{pages.map((page) => (
<Button
key={page}
onClick={handleCloseNavMenu}
sx={{ my: 2, color: 'black', display: 'block' }}
>
{page}
</Button>
))}
</Box>
The entire code:
import * as React from 'react';
import AppBar from '#mui/material/AppBar';
import Box from '#mui/material/Box';
import Toolbar from '#mui/material/Toolbar';
import IconButton from '#mui/material/IconButton';
import Typography from '#mui/material/Typography';
import Menu from '#mui/material/Menu';
import MenuIcon from '#mui/icons-material/Menu';
import Container from '#mui/material/Container';
import Button from '#mui/material/Button';
import MenuItem from '#mui/material/MenuItem';
const pages = ['About', 'Projects', 'Contact'];
const NavBar = () => {
const [anchorElNav, setAnchorElNav] = React.useState(null);
const handleOpenNavMenu = (event) => {
setAnchorElNav(event.currentTarget);
};
const handleCloseNavMenu = () => {
setAnchorElNav(null);
};
return (
<AppBar position="static" sx={{backgroundColor: "blue"}}>
<Container maxWidth="xl">
<Toolbar disableGutters sx={{display: { xs: 'flex' }, flexDirection: "row", backgroundColor: "blue"}}>
{/* LOGO */}
<Typography
variant="h2"
noWrap
component="div"
color="black"
sx={{ mr: 2, display: { xs: 'none', md: 'flex' } }}
>
name
</Typography>
{/*Drawer - small screen */}
<Box sx={{ flexGrow: 1, display: { xs: 'flex', md: 'none'} }}>
{/* Menu triple bar */}
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
{/* ABOUT, PROJECTS, CONTACT - small screen */}
<Menu
id="menu-appbar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: 'block', md: 'none' },
}}
>
{pages.map((page) => (
<MenuItem key={page} onClick={handleCloseNavMenu}>
<Typography textAlign="center">{page}</Typography>
</MenuItem>
))}
</Menu>
</Box>
{/* LOGO - small screen */}
<Typography
variant="h6"
noWrap
component="div"
color="black"
sx={{ flexGrow: 1, display: { xs: 'flex', md: 'none' } }}
>
name
</Typography>
{/* Issue */}
{/* ABOUT, PROJECTS, CONTACT - full screen */}
<Box sx={{ display: { xs: 'none', md: 'flex' }, alignItems: "flex-end" }}>
{pages.map((page) => (
<Button
key={page}
onClick={handleCloseNavMenu}
sx={{ my: 2, color: 'black', display: 'block' }}
>
{page}
</Button>
))}
</Box>
</Toolbar>
</Container>
</AppBar>
);
};
export default NavBar;
You need to use justifyContent or justifySelf to move content on the main axis and alignContent and alignSelf to move content on cross axis, you were using the wrong CSS property since the main axis in this case is row. For more info. regarding flexbox axes please refer to this link
Let me simplify this for you.
The work around is fairly easy. The parent Container has two children "Logo/Typography" and "Box", you want the "Box" child to move at the end of the container, you just need to give parent (Toolbar) justifyContent: space-between property to make sure both children are at opposite ends.
Play around with the code here
<AppBar position="static" sx={{ backgroundColor: "blue" }}>
<Container maxWidth="xl">
<Toolbar
disableGutters
sx={{
display: { xs: "flex" },
flexDirection: "row",
backgroundColor: "blue",
justifyContent: "space-between"
}}
>
{/* LOGO */}
<Typography
variant="h2"
noWrap
component="div"
color="black"
sx={{ mr: 2, display: { xs: "none", md: "flex" } }}
>
name
</Typography>
{/*Drawer - small screen */}
<Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
{/* Menu triple bar */}
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
{/* ABOUT, PROJECTS, CONTACT - small screen */}
<Menu
id="menu-appbar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
keepMounted
transformOrigin={{
vertical: "top",
horizontal: "left"
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: "block", md: "none" }
}}
>
{pages.map((page) => (
<MenuItem key={page} onClick={handleCloseNavMenu}>
<Typography textAlign="center">{page}</Typography>
</MenuItem>
))}
</Menu>
</Box>
{/* LOGO - small screen */}
<Typography
variant="h6"
noWrap
component="div"
color="black"
sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}
>
name
</Typography>
{/* Issue */}
{/* ABOUT, PROJECTS, CONTACT - full screen */}
<Box
sx={{
display: { xs: "none", md: "flex" }
}}
>
{pages.map((page) => (
<Button
key={page}
onClick={handleCloseNavMenu}
sx={{ my: 2, color: "black", display: "block" }}
>
{page}
</Button>
))}
</Box>
</Toolbar>
</Container>
</AppBar>

React Material Tabs content gets shifted to the out of the expected Container

I want to implement the Tabs Component for my React Website. I got the Problem, that the content I want to display in the red / blue Container gets shifted to the bottom for the blue container. I know the Problem comes from the red box, which is invisible but somehow is still there and shifts my content to the bottom.
The JSX looks like the following:
<div className='drinks'>
<Header />
<div className="drinks-tabs-wrapper">
<Tabs value={tab} onChange={ (_, value) => setTab(value) }>
<Tab label='Drinks' style={{ color: 'white' }} />
<Tab label='Categories' style={{ color: 'white' }} />
</Tabs>
<div style={ getTabWrapperStyle('drinks') }>
{ (!drinks || !drinkCategories) ?
<CircularProgress size={ 100 } /> :
<div hidden={ tab !== 0 }>
{ drinks.map(drink => (
<div key={ drink.id }>
<p>{ drink.id }</p>
<p>{ drink.name }</p>
<p>{ drink.price }</p>
</div>
))}
</div> }
</div>
<div style={ getTabWrapperStyle('drinkCategories') }>
{ drinkCategories &&
<div hidden={ tab !== 1 }>
{ drinkCategories.map(drinkCategory => (
<div key={ drinkCategory.id }>
<p>{ drinkCategory.name }</p>
</div>
))}
</div> }
</div>
</div>
</div>
The drinks and drinkCategories come from an API (therefore the Loader)
The getTabWrapperStyle which gets the CSS looks like the following:
const getTabWrapperStyle = (tabName) => {
if (tabName === 'drinks') {
if (tab !== 0) {
return {
"display": "flex",
"alignItems": "center",
"justifyContent": "center",
"height": "calc(100% - 50px)",
"background": "blue",
"overflowY": "scroll"
}
} else if (tab !== 1) {
return {
"height": 0
}
}
} else if (tabName === 'drinkCategories') {
if (tab !== 0) {
return {
"height": 0
}
} else if (tab !== 1) {
return {
"display": "flex",
"alignItems": "center",
"justifyContent": "center",
"height": "calc(100% - 50px)",
"background": "red",
"overflowY": "scroll"
}
}
}
}
Everything looks fine for the red box, but the blue one doesn't work.
The shifting looks like the following:
[1
Turned out the Problem was the display: flex in the blue / red containers css. Just remove this and work without the flex property and everything works fine.

Material-ui 1.3 fontsize not responsive to screen width?

I'm trying to build an app that has responsive font sizes based on the screen width with the new material-ui (v1.3). I have a drawer with a menu on it for navigation. I'd like to be able to shrink the font size (among other things on the page), when the screen size is smaller.
I have the following code and it doesn't seem to work when I shrink the screen down manually in the browser. The font size will change but I actually have to refresh the page to see the changes?? With the last version I used, (v.0.13) it would shrink as the screen size was changing, by manually making the browser size smaller with clicking and dragging with mouse and making it smaller. Does anyone have any ideas as to why this is happening?
class App extends Component {
constructor(props) {
super(props);
this.state = {
open: false,
};
}
getDrawerFontSize() {
if (window.innerWidth <= 575) {
return '10px';
} else if (window.innerWidth <= 767) {
return '11px';
} else if (window.innerWidth <= 991) {
return '12px';
} else if (window.innerWidth <= 1199) {
return '13px';
}
return '14px';
}
render() {
const drawerFontSize = this.getDrawerFontSize();
const { open } = this.state;
const theme = createMuiTheme({
overrides: {
MuiDrawer: {
paper: {
background: '#333333',
borderRadius: '0',
width: '250px',
padding: '0 10px',
color: 'white',
marginTop: '80px',
},
paperAnchorDockedLeft: {
borderRight: '0px',
},
},
MuiTypography: {
subheading: {
color: '#999999',
fontSize: drawerFontSize,
},
},
MuiListItemIcon: {
root: {
color: '#999999',
},
},
MuiListItemText: {
root: {
paddingRight: '5px',
paddingLeft: '5px',
}
},
MuiDivider: {
root: {
backgroundColor: '#999999',
},
},
},
});
const styles = {
app: {
backgroundColor: 'black',
},
appBar: {
backgroundColor: '#333333',
},
titleBar: {
backgroundColor: '#111111',
height: '35px',
width: '100%',
},
venn: {
height: '50px',
display: 'inline-block',
verticalAlign: 'middle',
},
logo: {
height: '80px',
width: '80px',
display: 'inline-block',
verticalAlign: 'middle',
},
appHeader: {
backgroundColor: 'black',
height: '150px',
padding: '20px',
color: 'white',
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
appTitle: {
fontSize: '1.5em',
},
appIntro: {
fontSize: 'large',
},
rightImages: {
marginLeft: 'auto',
marginRight: -12,
},
drawer: {
width: '150',
position: 'relative',
},
title: {
marginRight: '15px',
verticalAlign: 'middle',
display:'inline-block',
},
activeLink: {
textDecoration: 'none',
color: 'white',
}
}
return (
<MuiThemeProvider theme={theme}>
<div style={styles.app}>
<AppBar style={styles.appBar} position="static">
<Toolbar >
<div style={styles.drawerHeader}>
<Typography style={styles.title} variant="display2" color="inherit">
My APP
</Typography>
<img src={venn} style={styles.venn}/>
</div>
<section style={styles.rightImages}>
<img src={logo} style={styles.logo}/>
</section>
</Toolbar>
</AppBar>
<Drawer variant={"permanent"} anchor="left">
<div
tabIndex={0}
role="button" >
<List component="nav">
<NavLink style={styles.activeLink} to="/" href="/">
<ListItem button >
<ListItemIcon>
<HomeIcon />
</ListItemIcon>
<ListItemText primary="Home" />
</ListItem>
</NavLink>
<Link style={styles.activeLink} to="/account" href="/account">
<ListItem button>
<ListItemIcon>
<PersonIcon />
</ListItemIcon>
<ListItemText primary="My Account" />
</ListItem>
</Link>
<Link style={styles.activeLink} to={"/logout"} href="/logout">
<ListItem button>
<ListItemIcon>
<ExitIcon />
</ListItemIcon>
<ListItemText primary="Logout" />
</ListItem>
</Link>
</List>
<Divider />
<List component="nav">
<Link style={styles.activeLink} to={"/help"} href="/help">
<ListItem button>
<ListItemIcon>
<HelpIcon />
</ListItemIcon>
<ListItemText primary="Help" />
</ListItem>
</Link>
</List>
</div>
</Drawer>
<div>
<Router />
</div>
</div>
</MuiThemeProvider>
);
}
}
export default withRouter(connect( mapStateToProps, mapDispatchToProps)(App));
You can make responsive font by multiple way :
1) Using JavaScript :
- Like you can use java script Library (i.e. http://simplefocus.com/flowtype/)
2) Using CSS :
- In place of px use vw or vh -
- 1vw = 1% of viewport width
-1vh = 1% of viewport height
-1vmin = 1vw or 1vh, whichever is smaller
-1vmax = 1vw or 1vh, whichever is larger
OR
You can use Media Query in css
https://www.w3schools.com/css/css_rwd_mediaqueries.asp
Thanks!! Hope it Helps.