Prevent menu close on multi select paper-dropdown-menu. Polymer 1.0

六眼飞鱼酱① 提交于 2019-12-11 01:51:42

问题


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;


来源:https://stackoverflow.com/questions/36268725/prevent-menu-close-on-multi-select-paper-dropdown-menu-polymer-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!