CSS to change react MUI select - css

I need to change react material UI select component UI to like this.
This is the CSS I have entered up to now
const styles = {
width:'250px',
border:'1px solid gray',
borderBottom:'none',
padding:'1rem'
}
This is the react code.
<FormControl styles={styles}>
{/* <InputLabel id='demo-simple-select-label'>Categories</InputLabel> */}
<Select
labelId='demo-simple-select-label'
id='demo-simple-select'
value={age}
onChange={handleChange}
style={styles}
>
<MenuItem value={'All'}>All</MenuItem>
{categories.map((category) => (
<MenuItem value={category} key={category}>
{category}
</MenuItem>
))}
</Select>
</FormControl>
Here I comment on the InputLable component. So now If nothing is selected the bar is empty. I want to display the text "Categories" when nothing is select using CSS. also fine-tune this code to match 100% as the given design.
How do I achieve this using CSS?
This is my full code
https://codesandbox.io/s/material-demo-forked-wcmx1?file=/demo.js:1028-1606
Any help!
Thanks in advance.

from docs : (https://material-ui.com/api/select/)
displayEmpty
If true, a value is displayed even if no items are selected.
In order to display a meaningful value, a function should be passed to the renderValue prop which returns the value to be displayed when no items are selected. You can only use it when the native prop is false (default).
renderValue
Render the selected value. You can only use it when the native prop is false (default).
Signature:
function(value: any) => ReactNode
value: The value provided to the component.
sandbox

Related

StickyHeader Row is hindering the drop down list view

I have made my row header, of material UI Table component, sticky using the stickyHeader attribute
<Table stickyHeader className={classes.table}></Table>
there are two drop-downs displayed above this table, these are implemented using the ReactSelect component
const DropDown = props => (
<div className={[props.divClasses, props.error ? 'error-class' : ''].join(' ')}>
<ReactSelect
{...props}
classNamePrefix="normal-select"
disabled={props.disabled ? props.disabled : false}
multi={props.multi}
placeholder={props.placeholder ? props.placeholder : 'Select'}
closeOnSelect={props.closeOnSelect}
clearable={props.clearable}
searchable={props.searchable}
options={props.options ? props.options : []}
value={props.simpleValue ? props.options.filter(
({ value }) => value === props.value) : props.value}
isLoading={props.isLoading}
className={` ${props.className ? props.className : ''}`}
onChange={option => props.onChange(props.property, props.simpleValue ? option?.value : option)}
onBlur={props.onBlur}/>
{props.error && <FormHelperText style={{ color: '#f44336' }}>{props.error}</FormHelperText>}
</div>
);
because of being sticky, the header of the table component is now corrupting the view of the drop downs
currently, I am getting,
the expected behavior is,
kindly help me in this regard.
The reason of this overlapping is because mui table's sticky header's default z-index value is 2 and is greater than react-select menu's default z-index value 1.
So, you need to increase z-index of react-select component's menu property to some value that is greater than 2 like this:
const customStyles = {
menu: (provided, state) => ({
...provided,
zIndex: 3
})
};
<ReactSelect
value={selectedOption}
onChange={setSelectedOption}
options={options}
styles={customStyles}
/>
You can take a look at this sandbox for a live working example of this usage.

MUI Select Component Padding (ReactJS)

I have a Material UI Select component and am trying to reduce the padding within the element. The padding appears to be a property of one of the subclasses .MuiOutlinedInput-input. However, I haven't been able to change the padding despite setting it to zero using the standard sx approach.
Edit: I was able to find a working solution, see my post in the solutions thread.
Here is the base code for the component:
import { Select, MenuItem } from '#material-ui/core';
<Select
id="time-period-select"
value={timeline}
onChange={handleTimelineChange}
variant="outlined"
>
<MenuItem value={10}>All Time</MenuItem>
<MenuItem value={20}>This Year</MenuItem>
<MenuItem value={30}>This Month</MenuItem>
<MenuItem value={40}>This Week</MenuItem>
<MenuItem value={50}>Today</MenuItem>
</Select>
Here is my attempt to remove the padding (I have tried several variations of this):
<Select
...
sx={p: 0, '& .MuiOutlinedInput-input': {p: 0}}
>
Any help would be greatly appreciated. Thanks!
Side Note:
I would like to further customize the component (ex. changing the background color) which doesn't seem to work either, so if you have a general approach for customizing the Select component that would be great :)
After tinkering around some more and looking at MUI Treasury, I was able to arrive at a solution. The following is the correct approach:
const useStyles = makeStyles(() => ({
select: {
background: '#F5F6F9',
paddingLeft: 24,
paddingTop: 14,
paddingBottom: 15,
...
},
}));
...
const classes = useStyles();
<Select
...
disableUnderline
classes={{ root: classes.select }}
>

Material-UI Select (or Select styling) with a custom Popper?

Problem:
I've been trying to figure out the right/best way to use a Material-UI Select component with a customized Popper for its dropdown menu. What I'm hoping to do is make something that looks like the following (but with Material-UI components or keeping the styling of a Material-UI Select component with the 'outlined' variant):
Options I've considered:
1. Simply customize the children of the Select component.
I've tried the following, however, it seems like the Popper component still closes if it is clicked on anywhere and I can't seem to figure out how to stop that.
import * as React from "react";
import { FormControl, InputLabel, Select } from "#mui/material";
export default function BasicSelect() {
return (
<FormControl sx={{ minWidth: 120 }}>
<InputLabel id="demo-simple-select-label">Dropdown</InputLabel>
<Select
label="Dropdown"
MenuProps={{
sx: { marginLeft: "-8px" },
onClick: (e) => { e.preventDefault(); e.stopPropagation(); }
}}
>
<div onClick={(e) => { e.preventDefault(); e.stopPropagation(); }}>
Test
</div>
</Select>
</FormControl>
);
}
2. Apply Select component css components/styles to a button or div that opens a Popper.
To start with, I tried the following but it didn't seem to apply the MUI select styling to the div:
import * as React from "react";
export default function Test() {
return (
<div className={`MuiSelect-select`}>
Test
</div>
);
}
Probably for obvious reasons that the element the class is being applied to is not a select (but I don't want it to be).
3. Write out css from scratch that tries to mimic the Select styling as close as possible.
Would be cumbersome, but potentially doable, I suppose.
What would you suggest or recommend? Thanks!

Material-UI Dropdown overflowed in Dialog

I'm trying to use a dropdown select inside of a Dialog (modal). However, the options get cut off by the bottom of the modal.
How can I get the options to continue further down past the bottom border?
I'm using MUI v5.
<Dialog open={open}>
<DialogContent>
<Autocomplete
disablePortal
id="combo-box-demo"
options={options}
// getOptionLabel={(option) => option}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Numbers" />}
/>
</DialogContent>
</Dialog>
(extreme) example: Code Sandbox
Remove or set the disablePortal prop to false in your Autocomplete. If you use portal in the dropdown list. The dropdown's real DOM element will be attached outside of the Dialog hierarchy (you can use inspect element to confirm it), so its size isn't constrained by the layout of the dialog.

Is it possible to make a Material ui element(Select) disable on hover?

I wanna do something like this when the user hover on an element:
document.getElementById("dropdownCategory").disabled = true;
<Tooltip arrow placement="right" title={props.description} >
<FormControl id="dropdownCategory" >
<InputLabel id="demo-simple-select-label" >{props.title} - {props.price}€</InputLabel>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={category[props.id]}
onChange={handleCategory}
name={props.id}
>
{numbers.map(l => (
<MenuItem value={l} key={l}>{l}</MenuItem>
))}
</Select>
</FormControl>
</Tooltip>
This is my component and I want to make it disable when the user go by mouse over it
If you want to change the enabled state of the Select based on whether or not the mouse is over it, you can toggle the disabled property based on when the mouse enters and leaves the component.
First, you would need to setup a property to track whether the mouse is over the Select or not. If you're using a functional component, you can do that with the useState hook:
const [isOver, setIsOver] = useState(false);
Then, you need to modify the Select as follows:
<Select
...otherProps
disabled={!isOver}
onMouseEnter={onEnter}
onMouseLeave={onLeave}
>
And finally, you'd need to handle the entrance and exit of the mouse:
const onEnter = (e) => { setIsOver(true); }
const onLeave = (e) => { setIsOver(false); }
You may need to do some magic to deal with the the mouse being over the MenuItems rather than over the Select, but this will give you the framework for how to toggle it.

Resources