[ClickAwayListener] Triggers on Select (and more)

See original GitHub issue

This is best explained using code.

The following example is constructed by copy/pasting code form the docs and combining ClickAwayListener and Select.

When an option in Select is chosen the ClickAwayListener triggers the callback, despite Select being its descendant.

<ClickAwayListener onClickAway={this.handleClickAway}>
    <Paper className={classes.paper}>
        <Select
            multiple
            value={this.state.selected}
            onChange={this.handleChange}
            input={<Input id="select-multiple" />}
            MenuProps={MenuProps}>
            {names.map(name => (
                <MenuItem key={name} value={name}>
                    {name}
                </MenuItem>
            ))}
        </Select>
    </Paper>
</ClickAwayListener>
  • This is a v1.x issue (v0.x is no longer maintained).
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Select should normally close with the chosen value being the current in the Select and the click-away trigger shouldn’t fire.

Current Behavior

When an option in Select is chosen the ClickAwayListener triggers the callback, despite Select being its descendant.

Note that this doesn’t happen if select is in native mode with options instead of MenuItems. The same behaviour is exhibited in both multi- and regular Selects.

Steps to Reproduce (for bugs)

See codesandbox: https://codesandbox.io/s/3qq7l6478m, forked from the one in docs: https://codesandbox.io/s/6llvjywwq3

Context

I have a Select in a Drawer that contains a form. The Drawer has ClickAwayListener enveloping its content, including Select.

When a value is chosen in Select, the click-away trigger is executed (and it closes the Drawer in my app).

Your Environment

Tech Version
Material-UI v1.?.?
React 16.4.1
browser Chrome
etc

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

80reactions
NPatel10commented, Apr 28, 2021

Setting mouseEvent property to 'onMouseUp' in ClickAwayListener worked for me.

17reactions
Joycechochocommented, Mar 5, 2020

https://github.com/mui-org/material-ui/issues/13216#issuecomment-429527839 This works for me. Also, if you are using TextField

 <TextField
        select
        label={"Name"}
        value={value}
        onChange={selectValue}
            SelectProps={{
                MenuProps: { disablePortal: true }
            }}
   >
        {[1, 2, 3].map((value, index) =>
              <MenuItem key={index} value={value}>
                      {value}
               </MenuItem>
         )}
 </TextField>
Read more comments on GitHub >

github_iconTop Results From Across the Web

[ClickAwayListener] Triggers on Select (and more) #12034
When a value is chosen in Select , the click-away trigger is executed (and it closes the Drawer in my app). Your Environment....
Read more >
Is there a way to control where a nested Material UI select in a ...
So your issue is not with the Popper , but with the Select (which is rendered under the body instead of inside the...
Read more >
Detect click outside React component - Material UI - MUI
The Click-Away Listener component detects when a click event happens outside of its child element.
Read more >
@material-ui/core ClickAwayListener JavaScript Examples
This page shows JavaScript code examples of @material-ui/core ClickAwayListener.
Read more >
ClickAwayListener, Check the render method of ClickAwayListener ...
Learn more about the props and the CSS customization points. ... When an option in Select is chosen the ClickAwayListener triggers the callback, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found