plotly / plotly/plotly.js

plotly_click handler return value is ignored, no way to cancel event processing from custom handler

Ouverte
#5,477 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug P3
Langage dominant
JavaScript
Étoiles
18.3k
Forks
2k
Merge moyen
2 j 12 h
PR mergées (30 j)
28

Description

I'm currently using a plotly_click custom handler (via react-plotly onClick prop) on a treemap, where I would like to cancel the default zoom-in / zoom-out behaviour if the user is pressing control.

For plotly_legendclick for example, the convention is to return false from the custom handler, which will stop default event processing. See https://community.plotly.com/t/disable-legend-click-functionality-hiding-traces/1345/4 for @alexcjohnson 's reply in this regard.

However, for the plotly_click event I've traced the code into this little section in plotlyjs attachFxHandlers.onClick():

        var clickVal = Events.triggerHandler(gd, 'plotly_' + trace.type + 'click', typeClickEvtData);

        if(clickVal !== false && fullLayoutNow.hovermode) {
            gd._hoverdata = [makeEventData(pt, traceNow, opts.eventDataKeys)];
            Fx.click(gd, d3.event);
        }

// 2 lines ommitted

        // if custom handler returns false, we're done!
        if(clickVal === false) return;

So the event for plotly_treemapclick in this case will honour the convention as can be seen from the clickVal = ..., but the standard plotly_click handler is invoked by the Fx.click() invocation, which ignores the handler return values.

Unfortunately, the call chain from Fx.click() is quite deep: onClick() -> click() -> emitClick() -> plotObj.emit() -> emit() -> emitOne() -> customHandler. To bring back the return value is going to be quite the adventure. Also, at some spots along the chain, return values are used for a different purpose.

Alternatively, the code in the uppermost onClick() could for example check event.defaultPrevented -- this is not quite the Plotly convention, but then again that convention is not very well documented at all, and this would be far more straight-forward to implement rather than bringing back the custom handler return value. Furthermore, many javascript programmers know to look for preventDefault.

In addition, this fix would immediately also work from React.

What do people think about this?

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par attachFxHandlers.onClick(), puis suivez la chaîne documentée de Fx.click() à travers click(), emitClick(), plotObj.emit(), emit() et emitOne(). Comparez la gestion de la valeur de retour de plotly_treemapclick avec le chemin standard plotly_click et déterminez comment l’annulation doit se propager. C’est terminé lorsqu’un gestionnaire personnalisé qui renvoie false ou qui est pris en charge d’une autre manière peut empêcher le traitement par défaut du clic, y compris via React.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, react
Domaine
data-visualization, frontend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
32/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.