Troubleshooting the issue with React Material-UI Chip's onDelete function within the Select component

After referencing the Chips example, I attempted to render chips as the renderValue of the Select component. However, when I added onDelete to a chip to enable one-click deletion, I encountered an issue where the delete event was not being invoked due to the menu of the Select displaying.

https://material-ui.com/components/selects/

<Select
        multiple
        value={selectedProducts}
        onChange={handleProductsSearchChange}
        renderValue={selected => (
            <div className={classes.chips}>
            {selected.map(value => (
                <Chip key={value} label={find(FAKE_PRODUCTS, {id: value}).name}
                        onDelete={() => handleDeleteSearchProduct(value)} 
                        className={classes.chip} />
                ))}
            </div>)}

        MenuProps={{ PaperProps: {
            style: {
                maxHeight: 48 * 4.5 + 8,
                width: 250,
            }
        }}}

>
{menuItems}
</Select>

When moving the chip outside of the select, the delete event functions properly. How can I prevent the behavior of the menu opening on click while still allowing for chip deletions within the select? Any suggestions are greatly appreciated!

Thank you for your help!

Answer №1

When using the <code>Select component, remember to include an event listener for the mousedown event:

onMouseDown={(event) => {
  event.stopPropagation();
}}

This code should be added to your Chip component.

Answer №2

Achieved success! Halting propagation within chip component

onMouseDown={(event) => {
 event.stopPropagation();
}}

To maintain focus in select, a reference can be established

selectReference = React.createRef();
and then invoke selectReference.current.focus(); to reset the focus on the component

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 my Laravel application experiencing caching issues preventing real-time updates?

As I dive into learning AngularJS, I've encountered a strange issue where changes made in my JS files don't always seem to take effect. Even after seeing the GET request to the file in the console, the content remains unchanged. I can delete all ...

Conflicts between Bootstrap Validator and Ajax.BeginForm in Partial Views of MVC

My current issue involves using Ajax.BeginForm to post data on a form without refreshing the entire page. The goal is to validate a textbox - if it has a value, then the data should be posted; otherwise, a validation message should be displayed. However, I ...

Is there a way to incorporate Actions into the tool-bar on the Dashboard of Jupyter Notebook?

I am currently in the process of developing a customized front-end extension for Jupyter Notebook. I plan to have buttons on both the Notebook Dashboard and Editor that will trigger various actions. Similar to the existing "Move" and "Duplicate" buttons, ...

Trouble with X-editable: Unable to view values when editing and setting values using J

When using X-editable to modify a form with data, I encounter an issue. Initially, the values are loaded from the database to the HTML, but when users try to edit by clicking on the "Edit" button, all values appear as "Empty" instead of their actual cont ...

Performing numerous HTTP POST requests within a single function in Angular

$scope.observer_vel_data = function(){ $scope.showOverlay('loadRefPubVel'); $http({ //Making the first http post request method:'POST', url:'/api/observer_vel_data', ...

I'm curious as to why `Console.log(req.body.post)` shows the correct ID, but when passed into `Post.findById

Why is it that when I use req.body.post in Post.findById, it returns undefined, but if I directly use the id in Post.findById, it works perfectly fine? Console.log(req.body.post) displays the correct id, yet using it in Post.findById results in undefined. ...

Why aren't jQuery change event handlers triggering on Bootstrap radio buttons?

My Bootstrap radio buttons are not triggering the jQuery change event handlers. How can I fix this issue? Please note that I specifically do not want to add event handlers to input or input[type=radio], but rather by using ID selectors. Here is a link to ...

having difficulty iterating through the data using map function

When attempting to use the map function in React Material UI to display an array of data in a select box, I encountered the error TypeError: Cannot read properties of undefined (reading 'map') while using the following code. Can anyone help me id ...

Strange Actions with JQuery Drag-and-Drop Functionality

Apologies for my limited experience with JQuery UI, but I am in the process of creating a web-based chess engine for 2 players using JavaScript. Instead of point and click, I have decided to implement a user-friendly drag and drop feature for non-mobile us ...

Incorporate jQuery on elements that are dynamically loaded after the page has finished loading

I need to determine if a dynamically added button is enabled or disabled. How can this be achieved? My goal is to display a message when the button is disabled and remove it when the button becomes enabled. This is my current code: jQuery(document).read ...

Installing Eclipse for PHP and JavaScript on your computer is a simple process. Here

Currently, I am working on a web project that consists mostly of PHP and JavaScript files, as well as some HTML and CSS files. I have decided to use Eclipse as my Integrated Development Environment (IDE) for this project. However, upon visiting eclipse.org ...

Leveraging Server Response Codes Across Different Domains Using JavaScript

Before anything else, I must acknowledge the cross-domain issues that may arise with JavaScript. The fact that I am seeing both 404 and 200 response codes in my console has made me reconsider this possibility. The scenario... I am currently developing a w ...

The controller failed to return a value when utilizing the factory

I am attempting to pass a value from my view to the controller using a function within the ng-click directive. I want to then use this value to send it to my factory, which will retrieve data from a REST API link. However, the value I am sending is not ret ...

Angular encountering issue with HTTP service not functioning as expected

I have been encountering issues while trying to retrieve data using JSONPlaceholder in my service. Despite my efforts, I have not been successful in fetching any data. I would greatly appreciate any assistance in resolving this matter. user.component.html ...

What is the best way to swap out the if else statement with a Ternary operator within a JavaScript function?

Is there a way to replace the if else statement in the function using a Ternary operator in JavaScript? private getProductName(productType: string): string { let productName = 'Product not found'; this.deal.packages.find(p => p.isSele ...

Setting the z-index property in CSS allows one element to stack above another

My website has a hamburger menu that causes an issue when I navigate to a specific page with a card element. Whenever I open the menu while on this page, the card element ends up overlapping with the menu inexplicably. Despite trying various solutions, I c ...

Using AJAX with jQuery to extract a Date & Time field from a SharePoint List. Why is the formatting coming out so funky?

As I am collecting data from various SharePoint lists within a site to showcase on a central landing page, one of the fields I am extracting to present in an HTML table is a Date & Time field named "Date Submitted". However, upon populating it on the table ...

jQuery's z-index feature is malfunctioning

When I hover over my menu, a box fades in. However, there is a small icon behind this box that I want to move to the front so it can be visible during hover. To see an example of my menu, click here: Navigation I attempted to address this by using jQuer ...

Converting CommonJS default exports into named exports / Unable to load ES module

I've encountered an issue while creating a Discord bot with discord.js using TypeScript. When attempting to run the resulting JavaScript code, I'm facing an error that states: import { Client, FriendlyError, SQLiteProvider } from 'discord.js ...

Parsing JSON data in JavaScript

I am facing an issue with parsing JSON using JavaScript. After the completion of the loop, my variable 'text' is not getting the expected result. Can someone please explain how I can correctly parse this JSON? var xmlr = null; var text = '& ...