Using single-line ellipsis with the Material-UI select and option components

I have a TableHead Component with multiple columns. Among them is a <Select> element that allows users to filter the table content based on one of four statuses. I am trying to implement an ellipsis at the end of the option string if it exceeds its container's width.

The issue I'm facing is that material-ui's styles are affecting the Select component, making it difficult to apply custom styles such as adding ellipsis text overflow.

While researching, I found that material-UI supports the `noWrap` property and `textOverflow: "ellipsis"`, but I'm struggling to find the right combination for my specific case.

The desired outcome is to display an ellipsis if the text surpasses its parent container size. For example, instead of showing the full text like "Contribution...", I would like it to be truncated to "Contrib...". However, the Material-UI Select component SVG icon overlays the text.

Despite trying various options and methods, I haven't been able to achieve the expected result. I've experimented with changing colors to check if the styles are affecting the component, 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>

Answer №1

There are two key points to consider here: firstly, a native select element does not have the capability to display ellipsis as the text-overflow property is designed for block elements only. For further information on this, please refer to Is it possible to make text-overflow:ellipsis for select with css only?

If you wish to pursue this approach, there is also the use of the inputProps attribute which allows you to pass properties to the underlying input element. By applying a style to the native input element as shown below:

   <Select native inputProps={{ style: { width: 100 } }}>{...}</Select>

Alternatively, if you remove the native attribute, the ellipsis effect should be automatically applied when the text within the select box exceeds its size.

Answer №2

After investigating, I discovered that the ellipses were not showing up on the non-native Select component due to the display: flex style being applied to the 'MuiSelect-root' component.

To resolve this issue, simply change it to display:block and the ellipsis should appear as intended.

Answer №3

            <FormControl
              variant="outlined"
              margin="dense"
              className={classes.formControl}
              error={touched.question && Boolean(errors.question)}
              >
              <InputLabel id="demo-simple-select-outlined-label">Question</InputLabel>
              <Select
                labelId="demo-simple-select-outlined-label"
                id="demo-simple-select-outlined"
                value={course}
                name="question"
                onChange={handleChange}
                label="Question"
              >
                <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.question && Boolean(errors.question)}>{errors.question}</FormHelperText>
            </FormControl>

And add class: formControl: { width: "100%", },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to animate CSS Grid components?

Is it possible to animate a re-ordered set of elements in an array that are arranged using a CSS Grid layout? Check out this quick boilerplate ...

Creating responsive images using Bootstrap CSS framework

I have been focusing on optimizing the banner below the navigation menu on this website. My goal is to make it responsive so that the player in the banner remains visible even when scaling down to iPhone size portrait. You can find the code for this secti ...

Having trouble importing Formik properly

Utilizing react-native, I installed formik in my project using the command: "npm add formik yup @material-ui/core @material-ui/icons" on vs code. However, upon reloading my project after creating a form, an error message appeared: "formik c ...

Having Trouble Aligning Contents in a Row Horizontally using Bootstrap 5

My Images are refusing to align in a horizontal row no matter what I try. Output: https://i.sstatic.net/echGV.png .img-size-square { width: 100%; height: auto; max-width: 400px !important; max-height: 400px !important; } .container-f ...

Positioning Tool Tips with Material Design Lite

While utilizing MDL tooltips (), the tooltip typically displays just below the <div> it is linked to. I am aiming for the tooltip to show up to the right of the referenced element, rather than beneath it. My attempt to adjust the appearance in styl ...

Encountering a hydration issue with an SVG element embedded within a Link component in Next

I'm encountering a hydration error related to an icon within a link, Here is the error message: Error: Hydration failed because the initial UI does not match what was rendered on the server. Warning: Expected server HTML to contain a matching <svg ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

Responsive design challenge

I'm currently working on a WordPress theme with a fixed header and footer on the homepage, and a vertical slider in between that includes content and images. I've made the website responsive, but I'm facing an issue where the content in the ...

Typescript Next.js Project with Custom Link Button Type Definition

I have a project that includes a custom Button component and a NextLink wrapper. I want to merge these two components for organization purposes, but when I combine the props for each, I encounter an issue with spreading the rest in the prop destructuring s ...

Having trouble running a React application from GitHub on VS Code

Sorry for the beginner question. I recently downloaded a React application from GitHub. However, when I attempt to run it locally on VS Code, I encounter some dependency errors. Here are the dependencies listed in the JSON file: "dependencies": { ...

The wrapper is failing to extend the full height of the entire page

Initially, I thought setting the height of my wrapping div to 100% would be a quick task that I could complete in five minutes. However, it ended up taking me hours, so now I'm here hoping someone can assist me with this commonly asked question. The ...

Send a triggering function to two separate components

My objective is as follows: render() { return ( <div> <Child onTrigger={xxx} /> <button onClick={xxx} /> </div> ) } Upon clicking the button, I wish for some action to take place in ...

Selection of a map in React Native using a checkbox

When mapping through an array of objects called abilities in React Native that I retrieve from the backend, I am trying to individually select each one of them. However, when I select one checkbox, it selects all of them. How can I effectively select a s ...

Tips for creating text that adjusts to the size of a div element

I'm currently working on developing my e-commerce website. Each product is showcased in its own separate div, but I've encountered a challenge. The issue arises when the product description exceeds the limits of the div, causing it to overflow ou ...

How come the console is displaying an error message saying that React is unable to identify the startAdornment property on a DOM element?

I've been attempting to utilize InputAdornment from MUI, but I keep encountering an issue where the console displays that React doesn't recognize the prop startAdornment on the DOM element. import { Stack, TextField } from "@mui/material&quo ...

React JS - In order to display multiple children, consider utilizing an array rather than individual elements

Currently, I am employing React for my application and utilizing an API located at http://localhost:8080/api/suppliers/supplier/list to fetch data. Upon inspecting the Google Chrome console, this is the format of the data structure I am receiving: 0:{ ...

Issue with color display inconsistency in webgrid across various segments

https://i.sstatic.net/UW17M.png My goal is to display section status colors in a webgrid and divide them percentage-wise, but I am facing issues with some of the rows. Here is the cshtml script for the webgrid section status: webGrid.Column(header: "Sec ...

Exciting Update: Previously, webpack version 5 did not automatically include polyfills for node.js core modules (such as React JS, TypeScript, and JWT)!

Having trouble verifying the jwt token in React with TypeScript and encountering this error, how can I fix it? ` const [decodedToken, setDecodedToken] = useState<null | JwtPayload | string>(null); const verifyToken = (token: string) => { t ...

Is it possible to animate the change in background color dynamically using jQuery?

I am struggling to implement a change/animate feature for the background color. When I place the background script within the form submit event but outside the ajax call, I can briefly see the color change. However, once the remote content loads, the colo ...

What is the process for including a field specific to a date in a form?

I need the user to select a month and year. In one column, there will be the days of the month they selected [e.g. 1-30]. Users can then add habits in other columns. I want users to be able to input an 'X' for each habit row on a specific date [e ...