vasturiano / react-kapsule

React wrapper for kapsule-style web components
MIT License
8 stars 2 forks source link

[Question] Listening to an event without consuming it #3

Closed WhoAteDaCake closed 4 years ago

WhoAteDaCake commented 4 years ago

Is there a way we have a function onClick on a component, without actually overriding logic of the component?

Example : codesandbox. Here we add onClick, but that removes the zoom functionality of the chart

WhoAteDaCake commented 4 years ago

I guess this is more of a kapsule question. My current approach was to add

    useImperativeHandle(ref, () => {
      return {
        getCore() {
          return comp;
        },
      };
    });

And doing ref.current.getCore().focusOnNode(entry) from the onClick handler

vasturiano commented 4 years ago

@WhoAteDaCake you don't have to go so far. If you register the react kapsule with a list of methods:

const Chart = fromKapsule(chartKapsule, {
  methodNames: ['focusOnNode']
})

the wrapper will do the useImperativeHandle work for you and expose the method, accessible like so:

ref.current.focusOnNode(...)

Then you can add that to the onClick handler if you wish.