styling material-table (specifically height) - css

I'm using material table from material UI react from here and I can't figure out how to style the table properly, especially the height of rows.
I've tried all the ways explained in doc, alternatively and altogether and nothing makes it.
But there seems to be something somewhere that takes precedence over my css. Some of these CSS prop do have effects (like textAlign and vertical align), but most don't.
What am I missing here ?
I also overrode some css directly, without any effect
tr {
height: 100px;
}
tr td {
height: auto !important;
overflow:scroll;
}
MuiTableRow-root.{
height:50px;
max-height:50px;
}
here's my code:
let columns = [];
//console.log(this.props.data.matrix[0][0])
for(var i=0;i<this.props.headers.length;i++){
let filtering = false;
let obj = {'title':this.props.headers[i].display,'field':this.props.headers[i].accessor,headerStyle: {
backgroundColor: '#315481',
color: '#FFF',
},cellStyle:{maxHeight:50,height:50,overflow:'scroll',padding:1}}
columns.push(obj)
}
return(
<div>
<a className="anchor" id={this.props.id}></a>
<div className="centeringDiv"><h3>{this.props.title}</h3></div>
{this.props.infoText && <SectionInfo infoText = {this.props.infoText} />}
<div style={{ maxWidth: '100%',marginTop:15}}>
<MaterialTable
columns={columns}
data={this.props.dataToDisplay}
title={''}
onRowClick={((evt, selectedRow) => this.setState({ selectedRow }))}
options={{
pageSize:pageSizeCalc,
maxBodyHeight:400,
paging:true,
filtering:this.props.filtering,
pageSizeOptions: pageSizeOptionsCalc,
//fixedColumns: {left: 1},
rowStyle: rowData => ({backgroundColor: (this.state.selectedRow && this.state.selectedRow.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF',maxHeight:100}),
headerStyle:{height:60,maxHeight:60,padding:0,overflow:'none',lineHeight:1,textAlign:'center',fontSize:'2.5vh'},
cellStyle:{height:100,overflowY:'scroll',verticalAlign:'top',fontSize:'2vh',overflow:'scroll',padding:1}
}}
/>
finally the computed css

Try to style cell with cellStyle property like:
const cellStyle = {
padding: '0 14px',
};
const [state, setState] = React.useState<TableState>({
columns: [
{ title: 'ID', field: 'id', cellStyle }...
]
...
})

Related

Nested flex item divs rendered using React not restricting themselves to coequal/correct size?

I am attempting to render a custom Table component in React that will render [ "linear"-looking ] sub-tables, if and only if the values of its object rows prop are themselves of type 'object'. To do this I have my parent Table component, that renders a child TableRow component, that then does the conditional rendering of either a SubTable component or a TableInnerSquare component.
So far it works perfectly for the base condition [ when the properties of the rows object are simple ], but when I try to render sub-tables, the TableRows overflow the width of their container and I can't figure out how to stop this from happening.
Table component:
function Table(props) {
const { rows, columns, tableWidth, rowHeight } = props;
// rows here should be an array of rows containing objects w/ properties keyed by column names
// columns should just be an array of column names
return (
<div className='g-table'
style={{
display: 'flex',
width: tableWidth,
flexDirection: 'column',
margin: '5% auto',
}}
>
<div className='column-id-container'
style={{
display: 'flex',
width: tableWidth,
height: rowHeight,
}}
>
{ columns.map((column,idx) => {
return (
<div className='column-id'
style={{
backgroundColor: 'lightblue',
border: '1px solid blue',
width: '100%',
overflow: 'hidden',
padding: '2%',
}}
key={idx}
>
{ column }
</div>
);
}) }
</div>
<div className='rows-container'>
{ rows.map((row,idx) => {
return (
<TableRow
key={idx}
row={row}
rowId={idx}
tableWidth={tableWidth}
rowHeight={rowHeight}
columns={columns}
/>
);
}) }
</div>
</div>
);
};
TableRow component:
function TableRow(props) {
const { columns, row, rowId, tableWidth, rowHeight } = props;
// row should be an object with keys for each column here;
// columns should be an array
console.log('columns:');
console.log(columns);
console.log('row:');
console.log(row);
return (
<div className='table-row'
style={{
display: 'flex',
width: tableWidth,
}}
>
{ columns.map((property,idx) => {
if (typeof (row[property]) === 'object') {
return (
<SubTable
dataObject={row[property]}
rowHeight={rowHeight} // so for the SubTablesTable the row should be an object of objects
key={idx}
/>
);
} else {
return (
<TableInnerSquare
innerData={row[property]}
rowHeight={rowHeight}
key={idx}
/>
);
}
}) }
</div>
);
}
SubTable:
function SubTable(props) {
const { dataObject, rowHeight } = props;
console.log('dataObject:');
console.log(dataObject);
return (
<div className='sub-table'
style={{
width: 'auto',
display: 'flex',
flex: '1',
}}
>
{ Object.entries(dataObject).map((entry,idx) => {
return (
<div className='sub-table-inner'
style={{
display: 'flex',
overflow: 'hidden',
}}
>
<TableInnerSquare
rowHeight={rowHeight}
innerData={entry[0]}
/>
<TableInnerSquare
rowHeight={rowHeight}
innerData={entry[1]}
/>
</div>
);
}) }
</div>
);
}
TableInnerSquare:
function TableInnerSquare(props) {
const { innerData, rowHeight } = props;
return (
<div
className='table-inner-square'
style={{
backgroundColor: 'gold',
border: '1px solid red',
height: rowHeight,
overflow: 'hidden',
padding: '2%',
width: '100%',
}}
>
{ innerData }
</div>
);
}
Any help figuring out how to restrict TableRows containing SubTables to having width tableWidth [ which like I said the TableRows containing TableInnerSquares already seem to do? ] would be appreciated!
You can set overflow: hidden on a div, but its parent divs will still recognize the content as present and grow to contain it. You need to set overflow: hidden on the div with class sub-table [ in component SubTable ] here, in order for the sub-tables to hide the overflows of their inner divs.

Setting a conditional background linear gradient for getTrProps in react-table not rendering correctly

I'm trying to stylize a row in my react table so that when a certain row's data changes, the background has a linear-gradient applied to the row's background. I've tried a couple of things and looked up their documentation with examples with no luck.
I've tried a couple of things and looked up their documentation with examples with no luck.
I've made the linear gradient a variable and then plugged it into the conditional like so:
const gradient = {
linearGradient: `(to left, #27293d 99%, #344675 1%)`
}
const getTrProps = (rowInfo) => {
if (rowInfo) {
return {
style: {
height: `60px`,
padding: `5px`,
margin: `5px`,
borderRadius: `5px`,
backdropFilter: `blur(15px)`,
background: rowInfo.status === 'created' ? linearGradient : `red`
}
}
}
return {};
}
The column associated with the status in question is this:
{
Header: 'Status',
accessor: 'status',
Cell: row => (
<div style={{
width: `${row.value}%`
}}>
<span>
{
row.value === 'created' ? <GreenStatusCircle /> :
row.value === 'awaiting_driver' ? <YellowStatusCircle /> :
row.value === 'delivered' ? <RedStatusCircle /> : "None Found"
}
</span>
</div>
)
},
It should be rendering with the linear-gradient, but instead all of my rows are red. I've looked at the example shown here:
https://codesandbox.io/s/k3q43jpl47
And it seems I'm doing it just fine, but it won't render correctly.
You could pass it as HTML attribute but not as style
Instead of:
const gradient = {
linearGradient: `(to left, #27293d 99%, #344675 1%)`
}
Change it to
const gradient = {
someColor: 'linear-gradient(to left, #27293d 99%, #344675 1%)'
}
and us it as
const getTrProps = (rowInfo) => {
if (rowInfo) {
return {
style: {
height: `60px`,
padding: `5px`,
margin: `5px`,
borderRadius: `5px`,
backdropFilter: `blur(15px)`,
background: rowInfo.status === 'created' ? gradient.someColor: `red`
}
}
}
return {};
}
Example: https://codesandbox.io/s/react-table-gettrprops-h0oht

How to removing the padding for card in and design?

I am using ant design to react UI components. I need to remove the padding given for the ant design card.
So I need to remove the padding given for the classes .ant-card-wider-padding and .ant-card-body.I am using JSS for styling the UI components.
cardStyle: {
marginTop: '30px',
boxShadow: '0px 1px 10px rgba(0,1,1,0.15)',
backgroundColor: '#ffffff',
borderStyle: 'solid',
outline: 'none',
width: '100%',
},
i am using cardStyle class to styling ant design card.Now i need to remove the padding in that card.
From the documentation of Ant Design
You need to override the style in bodyStyle not cardStyle
bodyStyle: Inline style to apply to the card content
<Card title="Card title" bodyStyle={{padding: "0"}}>Card content</Card>
use fullWidth props for removing padding..,
<Card.Section fullWidth>
<ResourceList
items={[
{
id: 341,
url: 'customers/341',
name: 'Mae Jemison',
location: 'Decatur, USA',
}
]}
renderItem={
(item) => {
const {id, url, name, location} = item;
const defaultImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj48cGF0aCBkPSJNLS4wMi0uMDFoMTAwdjEwMGgtMTAweiIgZmlsbD0iI2ZmZTBjMyIvPjxwYXRoIGZpbGw9IiNmZjk2N2QiIGQ9Ik0wIDBoNjkuNDF2MTAwSDB6Ii8+PHBhdGggZD0iTTY5LjkyIDB2NDQuMzJMNTEuMzQgNTV2NDVIMTAwVjB6IiBmaWxsPSIjZmZlMGMzIi8+PHBhdGggZmlsbD0iIzMyY2FjNiIgZD0iTTM5LjMyIDc2YTExLjg1IDExLjg1IDAgMCAwIDEyIDExLjYyVjc2Ii8+PHBhdGggZmlsbD0iIzAwOTc5NiIgZD0iTTM5LjMyIDc2YTEyIDEyIDAgMCAxIDEyLTExLjgyVjc2Ii8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjZmZmIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxMCIgc3Ryb2tlLXdpZHRoPSI1IiBkPSJNNDMuNzQgMTkuODNhMTIuODIgMTIuODIgMCAxIDEtMjUuNjQgMCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iI2ZmZiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbWl0ZXJsaW1pdD0iMTAiIHN0cm9rZS13aWR0aD0iNCIgZD0iTTI3LjM5IDMxLjZsLTEuNTggNS45Nm05LjM3LTUuNzJsMi41NSA1LjQ3bTQuMjYtOS44NWwzLjUzIDQuNW0tMjUuNDMtNC41bC0zLjUzIDQuNSIvPjwvc3ZnPgo=" ;
const media = <Thumbnail source={defaultImage} size="small" name={name} />;
return (
<ResourceList.Item id={id} url={url} media={media}>
<Stack alignment="center">
<Stack.Item fill>
<TextStyle>{name}</TextStyle>
</Stack.Item>
<Stack.Item>
<TextStyle>Last changed</TextStyle>
</Stack.Item>
<Stack.Item>
<Button>Edit Giffy</Button>
</Stack.Item>
</Stack>
</ResourceList.Item>
);
}
}
/>
</Card.Section>
very simple just add bodyStyle in Card Component
<Card bodyStyle={{ padding: "0"}}>
You can use this:
.cardStyle {
padding: 0;
}
If didn't work, use this:
.cardStyle {
padding: 0 !important;
}
I'm not too familiar with JSS but if your other styles are being applied then I assume the issue is not with that.
I was able to remove the padding from the card with the following code.
//style.less
.panelcard { ... }
.panelcard .ant-card-body {
padding: 0;
}
// panelCard.js
import { Card } from 'antd';
require('./style.less');
const PanelCard = ({ children }) => {
return (
<Card className='panelcard'>
{children} // <p>Some Child Component(s)</p>
</Card>
);
}
// invocation
<PanelCard label='Panel Title'>
<p>Some Child Component(s)</p>
</PanelCard>
This gave me the following output (card is the white box):
I am not sure if this is the preferred way of customizing antd's components but I didn't really find too much on antd's website about overriding styles, only on extending components.
Try using :global in you scss/less
div { // or any parent element/class
:global {
.ant-card-body {
passing: <number>px; // number can be 0 onwards
}
}
}

React style width based on length of props

I have a react component:
const TeamRow = props => {
const items = props.team.map((item, index) => {
const con_width = 100 / props.team.length;
const Style = {
team_img: {
width: "50px",
height: "auto"
},
member_container: {
float: "left",
width: `{con_width}%`
}
};
return (
<div style={Style.member_container}>
<img style={Style.team_img} src={logo} />
<p>{item.name}</p>
<p>{item.bio}</p>
</div>
);
});
return <div>{items}</div>;
};
The idea is to have a set of divs next to one another horizontally. The width of each div should equal 100% divided by the number of divs.
{con_width} is calculated correctly, but in the web inspector, there is no 'width' style at all. It just gets ignored by react. What am I doing wrong?
Change
width: `{con_width}%`
To
width: `${con_width}%`
You need to use ${} in template literals to print the value

How do I control style of material-ui SelectField

I am using material-ui with react "material-ui": "^0.19.2" and am trying to place a SelectField in a table. This much is working fine but the margin and/or padding applied to the select component is doing 2 unwanted things. First is the table rows now adjust to the height of the select which is making the size of the entire row too high. Secondly the alignment of everything else in the row is off.
My code:
<SelectField
value={ props.value }
hintText="Select location.."
style={ {
width: 150,
padding: 0,
margin: 0
} }
onChange={ (event, key, payload) => this.updateLocation(event, payload, props.index, props.original.something) }>
{ ::this.createMenu() }
</SelectField>
createMenu = () => {
const { locations } = this.props.list;
return locations.map((location, i) => {
return <MenuItem key={ i } value={ location } primaryText={ location } />
});
}
If I could control the styling around the component(Textfield??) that is rendered I would try and make it fit properly but the style prop on the SelectField only seems to style the dropdown list.
Another option for me here could be to go with a popover for the field and render a custom component to attach it to - but I haven't had much luck when trying use that.
The following props on the SelectField give me what I want:
<SelectField
value={props.value}
hintText="Select location.."
style={{
width: 150,
height: 15,
lineHeight: 15,
position: 'relative',
textAlign: 'center',
fontSize: '0.9em'
}}
menuStyle={{
position: 'absolute',
top: -12.5,
}}
underlineStyle={{
position: 'relative',
top: 20,
}}
onChange={(event, key, payload)=>this.updateLocation(event,payload,props.index,props.original.something)}
>
{
::this.createMenu()
}
</SelectField>
if anyone knows a better way please add an answer but for now this will answer my question.
<b>Just add maxHeight={200} inside SelectField</b>
< SelectField
multiple
fullWidth = {true}
value={this.state.candidate_profile_arr}
onChange = {this.handleChange}
maxHeight = {200}
>
{this.state.profile.map((profile, i) =>
<MenuItem value={profile.id} key={i} primaryText={profile.name} />)
}
</SelectField>

Resources