paper-dropdown-menu icon indicating copy to clipboard operation
paper-dropdown-menu copied to clipboard

Pre-select item on <paper-dropdown-menu> doesn't works after a form reset

Open vmozhara opened this issue 9 years ago • 12 comments

I'm using <paper-dropdown-menu> inside the iron-form. So, it works properly after the initial loading of a form and shows pre-selected value. But after the form reset - this value disappears. Moreover I can not select it again. 2 Here's the code to replicate that behavior:

<dom-module id="test-form">
  <template>
    <form is="iron-form" id="form">
      <paper-dropdown-menu label="Dropdown">
        <paper-menu selected="1" class="dropdown-content">
          <paper-item>Option A</paper-item>
          <paper-item>Option B</paper-item>
        </paper-menu>
      </paper-dropdown-menu>
      <paper-button on-tap="reset">Reset</paper-button>
    </form>
  </template>
</dom-module>

<script>
  (function () {
    Polymer({
      is: 'test-form',

      reset: function () {
        this.$.form.reset();
      }
    });
  })();
</script>

vmozhara avatar Jan 13 '16 12:01 vmozhara

+1

SandeepSuddala avatar Jan 28 '16 08:01 SandeepSuddala

Thanks, looks like this is causing a fundamental incompatibility between paper-dropdown-menu and iron-form. Resetting value needs to somehow affect the paper-menu.

cdata avatar Jan 29 '16 23:01 cdata

/cc @notwaldorf

cdata avatar Jan 29 '16 23:01 cdata

It also, in this same set up, doesn't validate after a form reset.

Edit: it looks like setting the paper-menu.selected to undefined on reset fixes both of these issues, but I wouldn't know how to put that into the paper-dropdown-menu code.

mrherndon avatar May 03 '16 14:05 mrherndon

I've just watched some of you at the final panel @ Polymer Summit 2016. So, I somehow doubt this is gonna get seen soon. But... I have this in my TODO for tomorrow. Where should I start poking to get this fixed? Any hints will be most welcome, will provide a PR if I am successful!

mercmobily avatar Oct 18 '16 16:10 mercmobily

OK, I looked into this (pushed it to 2AM!) paper-dropdown-menu contains a paper-input which is also registered with the form, and reset. When it's reset, it's set to ''. For some VERY STRANGE reason, once that happens you are then unable to re-set it with the OLD value.

At least now I can reproduce it with a simple tap... more on this once (and if) I've figured out why specifically the old value is somehow blacklisted.

mercmobily avatar Oct 18 '16 17:10 mercmobily

OF COURSE! _onIronSelect in paper-dropdown-menu ISN'T fired, because even though the selection has disappeared from paper-dropdown-menu, the contained paper-listbox still "thinks" that it has the item selected, therefore NOT firing the event.

I am going to bed, but at this point it's a matter of making sure either that paper-listbox knows that the selection is changed, or... or I don't know. Will look into it tomorrow. But, at least now we know what's causing the problem...

mercmobily avatar Oct 18 '16 18:10 mercmobily

Here is the rundown of the problem. iron-form does this:

        var value = this._customElementsInitialValues[i];
        if (value === undefined) {
          value = null;
        }
        el.value = value;

The last line is the important one. The issue here is that for paper-dropdown-menu the value property is read-only, and it depends on what's been selected. Ideally, I would put an observer on value in paper-dropdown-menu. However, that won't work because value is read-only.

The only solution I've come up with is the following:

  1. Change iron-form so that it deals properly with read-only values. In this case:
        var value = this._customElementsInitialValues[i];
        if (value === undefined) {
          value = null;
        }
        if( el._setValue ) {
          el._setValue( value );
        } else {
          el.value = value;
        }
  1. Add an observer to paper-dropdown-menu so that when value is changed using _setValue(), the contentElement is set:
    observers: [
      '_selectedItemChanged(selectedItem)',
      '_valueChanged(value)'
    ],

    _valueChanged: function( v ){
      // Handle reset. The "if" here is not strictly necessary
      if( v === null ){
          this.contentElement.selected = v;
      }
    },

Before I spam you -- and the iron-form guys -- with pull requests, please let me know if I missed something. I spent a while on this, and I really tried my best to come up with the most elegant, less far-fetching solution...

mercmobily avatar Oct 19 '16 03:10 mercmobily

I forgot to mention that another possible solution would be to make iron-selector fire an event EVEN if the selected item hasn't changed. I tried that route, but -- frankly -- I didn't even manage to make it happen!

mercmobily avatar Oct 19 '16 03:10 mercmobily

Any updates on this one?

orenagiv avatar Mar 27 '17 08:03 orenagiv

I was able to fix the problem by using a paper-listbox inside the paper-dropdown-menu instead of a paper-menu. Every time the form is reset, I selected the paper-listbox by id and set its selected to null.

this.$.formWithDropDown.reset(); this.$.dropDownListbox.selected = null;

middletonsp avatar Apr 14 '17 18:04 middletonsp

I think another way to fix this is to bind the value used in selected on the listbox to the value of the paper dropdown menu. This causes iron form reset to function as normal.

<paper-dropdown-menu value="{{val}}">
    <paper-listbox selected="{{val}}"></paper-listbox>
</paper-dropdown-menu>

kaseyhinton avatar May 22 '18 22:05 kaseyhinton