Prevent menu close on multi select paper-dropdown-

2019-07-07 15:23发布

问题:

I have implemented a multi-select drop-down menu in the snippet below. It seems to work fine. However, each time I select an item, the menu closes. I'd like the menu to stay open until the User clicks a 'done' button.

Strangely enough, the menu stays open if I deselect an item, but closes if I selected an item.

html, body {
    height: 100%;
}

body {
    overflow: hidden;
    margin: 0;
    font-family: RobotoDraft, 'Helvetica Neue', Helvetica, Arial;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    -webkit-touch-callout: none;
}

paper-button {
    margin: 20px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Polymer</title>
<base href="http://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>

<link rel="import" href="paper-item/paper-item.html">
<link rel="import" href="paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="paper-menu/paper-menu.html">

</head>
<body>
<foo-drop ></foo-drop>

<dom-module id="foo-drop">

<template>
    <paper-dropdown-menu label="Your favourite pastry">
        <paper-menu id="myMenu" class="dropdown-content" multi>
            <paper-item>Croissant</paper-item>
            <paper-item>Donut</paper-item>
            <paper-item>Financier</paper-item>
            <paper-item>Madeleine</paper-item>
        </paper-menu>
    </paper-dropdown-menu>

</template>
<script>
    Polymer({
        is: "foo-drop",
                ready: function() {
                    window.addEventListener('WebComponentsReady', function() {
                        var menu = document.querySelector("#myMenu");
                        menu.addEventListener("iron-select", function(e, detail){
                            console.log(menu.selected);
                            console.log(menu.selectedItems.length);
                            for (var i = 0; i < menu.selectedItems.length; i++)
                                console.log(menu.selectedItems[i].value);
                        });
                    });

                }
            });
</script>
</dom-module>

</body>
</html>

回答1:

The control of close/open of the menu in paper-dropdown-menu uses paper-menu-button. https://github.com/PolymerElements/paper-dropdown-menu/blob/master/paper-dropdown-menu.html#L150

The specification states set ignoreSelect to true. https://elements.polymer-project.org/elements/paper-menu-button

There is a bug on this https://github.com/PolymerElements/paper-menu-button/issues/58

But maybe works in your environment (works at least on Chrome and Firefox). Check if the ignore-select works in the "Paper Menu with multi selection" https://elements.polymer-project.org/elements/paper-menu-button?view=demo:demo/index.html&active=paper-menu-button

If it does, then from the paper-dropdown-menu get the underlying paper-menu-button and set ignoreSelect = true;

With

<paper-dropdown-menu id="id_my-paper-dropdown-menu">

The set ignoreSelect should be set with something like this (I mainly work in the Dart framework):

this.$.id_my-paper-dropdown-menu.$.menuButton.ignoreSelect = true;


标签: polymer-1.0