Skip to content

Commit ba28e20

Browse files
Rectifying the bug in chip.js aka delete-propagation
1 parent 5679568 commit ba28e20

File tree

1 file changed

+25
-11
lines changed
  • packages/mui-material/src/Chip

1 file changed

+25
-11
lines changed

packages/mui-material/src/Chip/Chip.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
393393
const chipRef = React.useRef(null);
394394
const handleRef = useForkRef(chipRef, ref);
395395

396+
const deleteIconRef = React.useRef(null);
397+
396398
const handleDeleteIconClick = (event) => {
397399
// Stop the event from bubbling up to the `Chip`
398400
// Allow event to bubble so ClickAwayListener and other handlers work correctly.
@@ -401,6 +403,15 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
401403
}
402404
};
403405

406+
// This ref helps us detect if the click target is inside the delete icon.
407+
408+
const deleteIconProps = {};
409+
deleteIconProps.ref = deleteIconRef;
410+
deleteIconProps.onClick = handleDeleteIconClick;
411+
412+
413+
414+
404415
const handleKeyDown = (event) => {
405416
// Ignore events from children of `Chip`.
406417
if (event.currentTarget === event.target && isDeleteKeyboardEvent(event)) {
@@ -454,18 +465,21 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
454465
}
455466
: {};
456467

457-
let deleteIcon = null;
458-
if (onDelete) {
459-
deleteIcon =
460-
deleteIconProp && React.isValidElement(deleteIconProp) ? (
461-
React.cloneElement(deleteIconProp, {
462-
className: clsx(deleteIconProp.props.className, classes.deleteIcon),
463-
onClick: handleDeleteIconClick,
468+
// Prepare the delete icon by merging className, ref, and onClick handlers.
469+
// Supports both custom deleteIcon elements and the default <CancelIcon />.
470+
471+
let deleteIcon = null;
472+
473+
if (onDelete) {
474+
deleteIcon =
475+
deleteIconProp && React.isValidElement(deleteIconProp)
476+
? React.cloneElement(deleteIconProp, {
477+
...deleteIconProps,
478+
className: clsx(deleteIconProp.props.className, deleteIconProps.className),
464479
})
465-
) : (
466-
<CancelIcon className={classes.deleteIcon} onClick={handleDeleteIconClick} />
467-
);
468-
}
480+
: <CancelIcon {...deleteIconProps} />;
481+
}
482+
469483

470484
let avatar = null;
471485
if (avatarProp && React.isValidElement(avatarProp)) {

0 commit comments

Comments
 (0)