Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI: How to specify maxWidth of Chip?

My chips might contain very long strings, so I would like to limit the width of the chip, and show the full text in a Tooltip.

However when I try to change the maxWidth, it only ends up changing the width of the grey, pill-shaped part of the Chip -- the label continues to overflow, and the "delete" button also gets out of place and appears outside the chip.

Normal: enter image description here

After applying maxWidth: enter image description here

I have tried using inline style={{}} prop, as well as trying the "withStyles" approach to create my own custom styled Chip, but both have the same effect.

I modified the Chip example from Material-UI docs to demonstrate the issue: https://codesandbox.io/s/material-demo-zt72h

Edit: If I also adjust the 'overflow' styling, it's almost there, but all I see is a truncated section from the middle of the string, no ellipsis, and the delete button has disappeared:

enter image description here

  chip: {
    maxWidth: 100,
    whiteSpace: "nowrap",
    overflow: "hidden",
    textOverflow: "ellipsis"
  }
like image 326
Rick Barkhouse Avatar asked Sep 20 '25 12:09

Rick Barkhouse


1 Answers

You need to provide your custom implementation to label prop (that's just an example, you might need to modify it to better suit your case):

const CHIP_MAX_WIDTH =  100;
const CHIP_ICON_WIDTH = 30;

const EllipsisText = (props) => {
  const { children } = props

  return (
    <div style={{
      whiteSpace: "nowrap",
      overflow: "hidden",
      textOverflow: "ellipsis",
      maxWidth: CHIP_MAX_WIDTH - CHIP_ICON_WIDTH
      }}>
      {children}
    </div>
  )
}

And than use it in Chip:

<Chip
   size="small"
   key={data.key}
   icon={icon}
   label={<EllipsisText>{data.label}</EllipsisText>}
   onDelete={data.label === "React" ? undefined : handleDelete(data)}
   className={classes.chip}
/>

Edit Invisible Backdrop

like image 60
Ido Avatar answered Sep 23 '25 05:09

Ido