Material-UI chips - How to put the delete button on the left side of the chip?
The onDelete property on the Material-UI chip adding the delete button on the right by default, is there a way I can keep that on the left side?
1 Answer
4 years ago by cody
There is no out of the box property for that, however you can build your own chip with the icon on the left side of the Chip.
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import PropTypes from 'prop-types';
import React from 'react';
import IconButton from '@material-ui/core/IconButton';
import CancelRoundedIcon from '@material-ui/icons/CancelRounded';
const styles = theme => ({
root: {
margin: '4px',
backgroundColor: '#e0e0e0',
display: 'inline-flex',
boxSizing: 'border-box',
borderRadius: '16px',
},
label: {
padding: 10,
margin: 0,
},
cancelButton: {
cursor: 'pointer',
margin: "7px 5px 0 -5px",
color: 'rgba(0, 0, 0, 0.26)'
}
});
class CustomChip extends React.Component {
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<Typography variant="body2" gutterBottom className={classes.label}>
{this.props.label || ''}
</Typography>
{this.props.onDelete &&
<CancelRoundedIcon
className={classes.cancelButton}
onClick={() => {
this.props.onDelete(this.props.label)
}}
/>
}
</div>
);
}
}
CustomChip.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(CustomChip);
4 years ago by Karthik Divi