I have a TableHead Component with multiple columns. One of them is a <Select> so that you can filter the content of the table (the value is one of 4 statuses in my case). I want to add an ellipsis at the end of the options' string if it's longer than it's container.
My problem is that material-ui's styles are wrapped around the Select component so whether I do it withStyles or by inserting styles inside the tag, it doesn't work.
I checked and with material-UI you can have noWrap and textOverflow: "ellipsis" should work but I can't quite figure out the right combination.
My expected result is that if the string is larger than it's parent, it displays the Ellipsis. So instead of this:
I'd like a simple "Contrib..." (The svg from Material-UI Select component overlays the text...)
I tried many different options but none of them do anything so clearly I must be doing something wrong. I tried changing colors to see if my styles were being affected which they are.
<Grid item sm={2} lg={2} style={{display: 'flex', alignItems:'center'}}>
<Select native style={{ textOverflow: "ellipsis", overflow: "hidden", whiteSpace: "pre" }}
value={this.state.status}
onChange={this.handleChange.bind(this, Filter.Status)}>
{Object.keys(StatusFilter).filter(key => !isNaN(Number(StatusFilter[key]))).map(item => {
return (<option key={item} value={StatusFilter[item]}>{this.getStatus(StatusFilter[item])}</option>);
})}
</Select>
</Grid>
Two things here, first a native select has no way to add ellipsis since text-overflow is only for block elements. See Is it possible to make text-overflow:ellipsis for select with css only?
In case you want to explore that way, there is also inputProps that is an object you can use to pass properties to the underlying input. So if you want to add a style to the native input you can:
<Select native inputProps={{ style: { width: 100 } }}>{...}</Select>
On the other hand, if you remove the native property the ellipsis should be applied by default if the select box is smaller than the text.
In my case ellipses were missing even on non native Select component, and that was because display: flex style was applied over 'MuiSelect-root' component.
Replace that - i.e. with display:block - and ellipsis will be there for you.
<FormControl
variant="outlined"
margin="dense"
className={classes.formControl}
error={touched.test && Boolean(errors.test)}
>
<InputLabel id="demo-simple-select-outlined-label">Test</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={course}
name="test"
onChange={handleChange}
label="Test"
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={1}>1</MenuItem>
<MenuItem value={2}>2</MenuItem>
<MenuItem value={3}>3</MenuItem>
<MenuItem value={4}>4</MenuItem>
</Select>
<FormHelperText error={touched.test && Boolean(errors.test)}>{errors.test}</FormHelperText>
</FormControl>
And add class:
formControl: {
width: "100%",
},
Related
I have a Material UI component, and I am trying to custom style it using the CSS.
Following is the code:
<IconButton className="my-class">
<Close />
</IconButton>
CSS:
.my-class {
float: right;
margin-left: auto;
margin-right: 0;
}
But I am not able to style it, when I tried the following, it works:
<IconButton style={{ float: 'right', marginLeft: 'auto', marginRight: '0' }}>
<Close />
</IconButton>
Why I am not able to style the Material UI components using regular CSS?
Most CSS-in-JS solutions inject their styles at the bottom of the HTML <head>, which gives MUI precedence over your custom styles.
You need to use the StyledEngineProvider exported from #mui/material/styles with the injectFirst option, in order for the CSS injection order to be correct. It is explained here.
So something like this shoud work:
<StyledEngineProvider injectFirst>
<IconButton className="my-class">
<CloseIcon></CloseIcon>
</IconButton>
</StyledEngineProvider>
You can style MUI components using several ways like GlobalStyles API, sx, styled or even normal way.
If you are going to style the particular component like IconButton, and if you have a look at the document for the API, you can find the class names for the component.
Here's couple of references.
https://mui.com/customization/how-to-customize/#main-content
How to give conditional style to MUI TextField?
I have a problem in displaying the values from the TextField in my React app.
I'm using material UI and material-ui-phone-number as its packages. As you can see, the values after i click the flag, it is displaying on the back. I believe this is on the zIndex. Pls modify only the dialog2.js
Pls check my codesandbox here
CLICK HERE
Your MuiPhoneNumber utilizes a MUI modal for the country selection & its default z-index is 1300. It does not look like they expose a prop to alter its css properties so you can just target #country-menu (the id of the modal) using any preferred styling solutions
<div>
<style type="text/css">
{`
#country-menu {
z-index: 1801;
}
`}
</style>
<DialogContent style={{ width: 300, height: 500 }}>
<MuiPhoneNumber
name="MobileNo"
label="Mobile No."
variant="outlined"
fullWidth
defaultCountry={"vn"}
value={selectedValue}
onChange={(e) => setSelectedValue(e)}
/>
</DialogContent>
</div>
This can be achieved by doing the following changes:
Remove all CSS z-index defined
Put the Dialog 2 in Dialog 1 content
Here is the working CSB Link: https://codesandbox.io/s/nested-dialog-menuitem-dropdown-forked-wymm0?file=/dialog1.js
I have a very simple problem. I've tried to put marginButtom on the TextFields. I don't have any idea why it isn't putting any margin on it.
Pls check this codesandbox link
CLICK HERE
CODE
<label>Schedule</label>
<TextField
fullWidth
name="schedule"
type="date"
variant="outlined"
className={classes.marginButtom}
/>
That's because you have a typo in your styles object.
marginButtom: {
marginButtom: "10px" // It should be marginBottom instead of marginButtom
},
Is there any way to override a Material UI components styling without having to create a whole new component using withStyles()?
For instance, say I am rendering the following and I just want to change the color of the "Delete" label:
<div style={styles.rowFooter}>
<FormControlLabel
control={<ClearIcon />}
label="Clear"
title="Clear all fields."
onClick={clearFields}
/>
<FormControlLabel
control={<DeleteIcon />}
label="Delete"
title="Delete this row."
onClick={deleteRow}
/>
</div>
To do this, I'd usually have to:
Create a new styles function that returns { color: "maroon" }.
Create a new component to render the "Delete" button.
Wrap my new component withStyles(newStylesFn)(MyComponent).
But I don't want to do all of that. Is there any way to avoid it?
Update:
One way that I know of is to just pass a CSS className. I was looking for something besides that because it doesn't even work in this situation to override the nested element.
What I'd really like to be able to do is just pass style={{ color: "maroon" }}, but that only changes the color of the icon, not the actual label text...
You could use the classes prop to override styles provided by Material UI instead of className.
<FormControlLabel
control={<DeleteIcon />}
label="Delete"
title="Delete this row."
classes={{
label: 'labelStyle'
}}
/>
styles.css
.labelStyle {
color: maroon !important;
}
Although it's Not the perfect solution, it still does the job without using withStyles().
I am working with react-bootstrap-table and I am facing problems with formatting it. Main issues are:
the headers with long names should be presented with 2 lines of text, instead there is one and "..." like in the picture below:
Moreover in no way I could set the height of a single row of a table. Each text has big padding therefore the table is not too condensed:
And the code goes here:
<BootstrapTable
data={this.props.sales}
version="4"
striped
hover
pagination
keyField="Type"
>
{tableHeaders.map((header, index) => (
<TableHeaderColumn key={index} dataField={header.id} style={{ height: 10 }}>
{header.name}
</TableHeaderColumn>
))}
</BootstrapTable>
According to docs you can do all of the customizations you need.
First issue: To remove dots you can use thStyle property which you can pass to TableHeaderColumn component and override CSS white-space property.
<TableHeaderColumn thStyle={{ whiteSpace: 'normal' }} {...anotherProps} />
Second issue: You can handle height of your row using trClassName property. You can either pass string or function to handle each or make conditional class depends on row. See more here.
For example:
<BootstrapTable trClassName="customClass" {...anotherProps} />
And define your customClass:
.customClass {
// override padding or height whatever you want
padding: 3px;
}
Good luck and have fun!