Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop click event from propagating on popover target

I am trying to disable popover open on checkbox change, placed inside the popover target. I've tried stopping onChange and onClick events on checkbox but it doesn't work as intended.

Any help would be much appreciated.

Here's the relevant code sandbox:

import React from "react";
import ReactDOM from "react-dom";
import { Button, Checkbox, Menu, MenuItem, Popover } from "@blueprintjs/core";

import "./styles.css";
import "@blueprintjs/core/lib/css/blueprint.css";
import "@blueprintjs/icons/lib/css/blueprint-icons.css";

function Dropdown() {
  return (
    <Menu>
      <MenuItem text="Item 1" />
      <MenuItem text="Item 2" />
    </Menu>
  );
}

function MyCheckbox() {
  return (
    <Checkbox
      onClick={e => {
        e.stopPropagation();
        return false;
      }}
    />
  );
}

function App() {
  return (
    <div className="App">
      <Popover content={<Dropdown />}>
        <Button text={<MyCheckbox />} rightIcon="caret-down" />
      </Popover>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
like image 885
Ali Avatar asked Dec 07 '25 04:12

Ali


1 Answers

Wrap your checkbox in another container, and prevent that from propagating up the chain:

function MyCheckbox() {
  return (
    <div
      onClick={e => {
        e.stopPropagation();
      }}
    >
      <Checkbox />
    </div>
  );
}

See an updated sandbox here.

like image 194
ugh StackExchange Avatar answered Dec 10 '25 16:12

ugh StackExchange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!