React prevent MUI Select from changing width in MUI Grid - css

I have an MUI grid setup with a variable number of columns. Currently, if the user selects a long string for the Select box, the Select grows, increasing the size of the entire column. However, I want each column to remain the same size, no matter what is selected (and truncate what's in the Select box).
Please see the images below for a better understanding. In these examples, there are 8 columns:
Unfilled: empty fields
Filled: field filled, clearly the long text in the select box resized the whole column
Code:
{this.state.sampleData.map((obj, idx) => {
return (
<Grid item xs={true}> //Ensures the columns are the correct size (more columns = smaller widths).
<Grid container spacing={1}>
<Grid item xs={12}>
<Typography>#{obj["#"]}</Typography>
</Grid>
<Grid item xs={12}>
<TextField
className={classes.smallTextField}
inputProps={{
...
}}
variant="outlined"
label="Name"
name="name"
value={
this.state.sampleData[idx].name || ""
}
onChange={(event) => {
this.handleChange(
event.target.value,
idx,
"name"
);
}}
/>
</Grid>
<Grid item xs={12}>
<FormControl
variant="filled"
fullwidth
size="small"
className={classes.selectFieldFormControl}
>
<InputLabel
sx={{ ... }}
>
Position
</InputLabel>
<Select
sx={...}
name="position"
value={
this.state.sampleData[idx].position || ""
}
onChange={(event) => {
this.handleChange(
event.target.value,
idx,
"position"
);
}}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={"manager"}>
manager
</MenuItem>
<MenuItem value={"factory"}>
factory
</MenuItem>
<MenuItem value={"assistant to the regional manager"}>
assistant to the regional manager
</MenuItem>
</Select>
</FormControl>
</Grid>
</Grid>
);})}
How can I get the box to truncate the text inside it so that the column does not get resized? I figure I'll need to add some style in either the Select box itself or the parent grid item.
Thanks!

I had the same problem, adding a maxWidth of 100% to the grid item that contains the select, and making the width of the select 100% solved it for me:
<Grid item xs={12} lg={4} sx={{ maxWidth: "100% !important" }}>
<h3 id="select-theme-label">Themes</h3>
<Select
id="select-theme"
sx={{ width: "100%" }}
></Select>
</Grid>
Or probably more realistically, adding the right maxWidth responsively:
sx={{ maxWidth: { xs: "100% !important", md: "28% !important" } }}

Related

How to fit 2 products in the screen when it's in mobile?

I've been stuck on this part, and I've been struggling how can I manage to make it row, I tried to make it flexWrap, but it's going down instead of going its side.
<Box sx={{display: {xs: 'flex'},
gap: {xs: '0px', md:'20px',
flexWrap: 'wrap',
alignItems: 'center' }}}
>
{products.map((product) => (
<CardProduct key={product._id} product={product}/>
))}
</Box>
outcome
this is I want to achieve, the first and second product must be align, and then have a gap between or spacing, then the 3rd product will automatically wrap... I tried to call the
flexWrap:'wrap'
but it failed. should I just the height ? or is it because the button is too big?
want to achieve
This is what it looks like when it only have two products
two images
Try using a Grid:
<Grid container spacing={2}>
{products.map((product) => (
<Grid item xs={6} key={product._id}>
<CardProduct product={product} />
</Grid>
))}
</Grid>;
To make 2 columns you can use flex-basis property, flex-basis: 50%; , do remove any padding or margin before.
with flex-basis property you can set any width or get number of columns.
<Box sx={{display: {xs: 'flex'},
gap: {xs: '0px', md:'20px',
flex-basis: '50%',
alignItems: 'center' }}}
>
{products.map((product) => (
<CardProduct key={product._id} product={product}/>
))}
There is on more property in css that helps us to achieve any number of coumns without using flexbox.
.div {
column-count: 2;
}
you can try Grid. Material-UI has a very good and clear example exactly as you want.
link
<Grid container spacing={2}>
<Grid item xs={12}>
<Grid container justifyContent="center" spacing={spacing}>
{[0, 1, 2].map((value) => (
<Grid key={value} item>
<Paper className={classes.paper} />
</Grid>
))}
</Grid>
</Grid>

How do I move the Delete and Add icon to the right of the input fields using Grid?

Here's what I am able to create
My UI
And here is what I want to achieve:
The aspiration
Here's my code:
<Grid container spacing={3}>
<Grid item md={4} xs={15}></Grid>
<Grid item md={4} xs={15}></Grid>
<Grid item md={4} xs={15}></Grid>
<Grid item md={3} xs={15}><TrashIcon> <AddIcon></Grid>
</Grid>
Additional (Edited)
How do I align the grid fields in the bottom to the ones on top rows?
new confusion but have a limit on questions per hour
You can use auto-layout feature of the Grid:
The Auto-layout makes the items equitably share the available space.
That also means you can set the width of one item and the others will
automatically resize around it.
<Grid container spacing={3}>
<Grid item md xs={12}>
<input style={{ width: "100%" }} />
</Grid>
<Grid item md xs={12}>
<input style={{ width: "100%" }} />
</Grid>
<Grid item md xs={12}>
<input style={{ width: "100%" }} />
</Grid>
<Grid item md="auto" xs={12}>
<TrashIcon /> <AddIcon />
</Grid>
</Grid>
Working example

Dynamic Image List from MUI do not respect max height of the React container

I am trying to create a dynamic image list where I can change the number of images that are being displayed, where the images have to occupy the max space in the component without change the image ratio and also without make the component scrollable. The following images show the result that is expected:
Although what really happens now is, in the first case, with only 2 images, it overflows the parent component due to the spacing added between the flexbox elements.
For the 6 images example, it goes worst:
It overflows the parent even more because it ignores the component's max height and always tries to occupy the max width possible.
To reach the correct behaviour, I'm hard coding the flex-basis of the image list for each case because it is a flex item, and only that way does it not grow more than it should. Although, that is not desirable because I want to make the images grow as much as possible without depending on the screen size.
I do not understand why they ignore the parent's max height.
I do not understand why they do ignore the max height of the parent.
Main component:
<>
<Grid
container
direction="column"
wrap="nowrap"
rowSpacing={1}
sx={{ alignSelf: 'flex-end', margin: 0, height: '100%', justifyContent: 'flex-end' }}
>
<Grid
container
wrap="nowrap"
item
sx={{
alignSelf: 'flex-start',
flex: '1 0 auto',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<CustomImageList ref={ref} />
</Grid>
<Grid item>
<CustomPagination onClick={keyMap.move} />
</Grid>
<Grid width="100%" item>
<Stack
spacing={1}
direction="row"
sx={{
overflowX: 'auto',
justifyContent: 'space-between',
'& .MuiFormControl-root': { marginTop: '1px' },
}}
divider={<Divider orientation="vertical" flexItem />}
>
{Buttons.map((button) => (
<Stack spacing={1} flex={1} key={button.buttonId}>
<Stack direction="row" spacing={1}>
{button.text ? (
<ButtonText button={button} />
) : (
<Button />
)}
</Stack>
<Stack spacing={1} direction="row">
<ButtonBox button={button} />
</Stack>
</Stack>
))}
</Stack>
</Grid>
</Grid>
</>
The component CustomImageList is sketch like the next code:
<>
<Stack justifyContent="center">
<IconButton
ref={leftArrowRef}
size="large"
onClick={(e) => {
e.preventDefault();
handleClick({
key: 'ArrowLeft',
keyCode: 37,
});
}}
>
<ArrowBackIosNewIcon />
</IconButton>
</Stack>
<ImageList
sx={{
height: '100%',
alignContent: 'center'
}}
cols={imageColumnCount}
>
{images.map((image, index) =>
createOrdinaryListItem(image, index)
)}
</ImageList>
<Stack justifyContent="center">
<IconButton
onClick={(e) => {
e.preventDefault();
handleClick({
key: 'ArrowRight',
keyCode: 39,
});
}}
ref={rightArrowRef}
size="large"
>
<ArrowForwardIosIcon />
</IconButton>
</Stack>
</>
The ImageList component is a MUI ImageList
What would be the best approach to create the scenario shown in the first and second figures? I have already tried to create my component. It was a flexbox where I used the breakpoint from the grid to distribute the columns across the images. I had the same behaviour that I presented here.
Sorry about the verbose post and kinda "hard to understand" question, but I was not capable of creating a runnable script to show what is aforementioned

How to center align one item and right align another using MUI v5?

I would like to center 3 navigation tabs in the middle of my page and have a dropdown on the far right which is used for sorting. I was able to get the dropdown on the far right although can't figure out how to perfectly center the 3 navigation tabs inside the <Box> <Box/> component.
Here is a picture of what I am trying to do:
Here is a code sandbox: https://codesandbox.io/s/45159071-how-to-align-a-component-to-the-center-right-forked-0y60s3?file=/demo.js
<Grid
container
sx={{ marginTop: 3, marginBottom: 3, alignItems: 'center' }}
>
<Box
sx={{ marginRight: 'auto' }}
>
<Tabs
value={activeMenuTab}
onChange={handleChangeTab}
>
<Tab label="Tab 1" />
<Tab label="Tab 2" />
<Tab label="Tab 3" />
</Tabs>
</Box>
<Grid
item
xs
style={{ flexGrow: 0 }}
>
<FormControl
sx={{ minWidth: 150 }}
size="small"
>
<InputLabel id="sort-by">Sort By</InputLabel>
<Select
labelId="sort-by"
value={selectedValue}
label="Sort By"
onChange={handleSorting}
>
<MenuItem value="oldest">Oldest</MenuItem>
</Select>
</FormControl>
</Grid>
</Grid>
I found a similar post which uses pure CSS although struggled to integrate it within the MUI components: Center one and right/left align other flexbox element
Try this solution, I only used the Box component instead of Grid to reproduce just like in the post you pointed to with grid.
<Box
display="grid"
gridTemplateColumns="1fr 1fr 1fr"
gridAutoRows="auto"
rowGap={2}
alignItems="center"
marginY={3}
>
<Box gridColumn="2">
<Tabs centered value={'null'} onChange={handleChangeTab}>
<Tab label="Tab 1" />
<Tab label="Tab 2" />
<Tab label="Tab 3" />
</Tabs>
</Box>
<Box
gridColumn={{ xs: '1 / -1', sm: '3' }}
gridRow={{ xs: '2', sm: '1' }}
justifySelf={{ xs: 'center', sm: 'end' }}
>
<FormControl sx={{ minWidth: 150 }} size="small">
<InputLabel id="sort-by">Sort By</InputLabel>
<Select labelId="sort-by" value="Some Value" label="Sort By" onChange={handleSorting}>
<MenuItem value="oldest">Oldest</MenuItem>
<MenuItem value="newest">Newest</MenuItem>
</Select>
</FormControl>
</Box>
</Box>;

Dropdown Menu Item in React Material UI

I have a MenuItem in my Dialog2 in my React app. I have a problem displaying the MenuItem. I already put a zIndex: 1900 that is higher than the two dialogs. How come it doesn't appear on the front. It is hiding still on the back of the dialogs?
Pls check my codesandbox here:
CLICK HERE
<DialogContent style={{ width: 300 }}>
<TextField variant="outlined" select label="Gender" fullWidth>
{genders.map((gender, index) => (
<MenuItem key={index} value={gender} style={{ zIndex: 1900 }}>
{gender}
</MenuItem>
))}
</TextField>
</DialogContent>
You've to target the Menu popover z-index
const useStyles = makeStyles({
customMenuPopover: {
// take note of !important because default z-index is applied inline
zIndex: "1900 !important"
}
});
<TextField
SelectProps={{
MenuProps: {
PopoverClasses: {
root: classes.customMenuPopover
}
}
}}
variant="outlined"
select
label="Gender"
fullWidth
>
{genders.map((gender, index) => (
<MenuItem key={index} value={gender} style={{ zIndex: 1900 }}>
{gender}
</MenuItem>
))}
</TextField>

Resources