2

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);
Blue
  • 22,608
  • 7
  • 62
  • 92
Ali
  • 51
  • 1
  • 5

1 Answers1

9

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.

Blue
  • 22,608
  • 7
  • 62
  • 92
  • wow! It did cross my mind and I thought what difference it would make and didn't even try... – Ali Oct 27 '18 at 20:59