materialize icon indicating copy to clipboard operation
materialize copied to clipboard

getSelectedValues only return previous clicked values

Open weasteam opened this issue 5 years ago • 6 comments

Expected Behavior

https://materializecss.com/select.html Say we have 3 options as described in the above link. Here are my actions and return value

# Click Option clicked Expected value return from instance.getSelectedValues() return from e.target.value
1 Option 1 Option 1 `` Option 1
2 Option 2 Option 2 Option 1 Option 2
3 Option 3 Option 3 Option 2 Option 3
4 Option 1 Option 1 Option 3 Option 1

When instance.getSelectedValues() is called, it always return previous clicked values.

It should return the current clicked value

Current Behavior

See the return from column 4

Steps to Reproduce (for bugs)

Add the below code to the select onChange

        console.log(instance.getSelectedValues());
        console.log(e.target.value);

Context

Your Environment

  • Version used: v1.0.0
  • Browser Name and version: Chrome
  • Operating System and version (desktop or mobile): Mac desktop

weasteam avatar Mar 25 '20 06:03 weasteam

if multiple is provided, getSelectedValues () works correctly.

weasteam avatar Mar 25 '20 06:03 weasteam

This is still an issue for me on Firefox as well. And as @weasteam mentioned, multiple seems to work but obviously defeats the purpose when multiple isn't wanted.

It appears that _setSelectedState() is not being called at the appropriate time.

For instance, if you call instance._setSelectedState() before instance.getSelectedValues(), then the correct value is returned as expected.

earwole1 avatar Jun 05 '20 19:06 earwole1

Same problem still. Is there any workaround to get it working without using multiple? I guess I could parse the values generated by the instance in options to get the originals (the ones I set), but it doesn't seem clean.

jlires avatar Jul 03 '20 17:07 jlires

Same problem still. Is there any workaround to get it working without using multiple? I guess I could parse the values generated by the instance in options to get the originals (the ones I set), but it doesn't seem clean.

See above: if you call instance._setSelectedState() before instance.getSelectedValues() then the correct value is returned, even when not using multiple.

earwole1 avatar Jul 29 '20 17:07 earwole1

I found a fix for this on Stack Overflow. It's pretty dirty. Are you ready?

document.addEventListener('DOMContentLoaded', function() {
  
    var elems = document.querySelectorAll('select');
  
    var options = {
      classes:'beatles-select',
      dropdownOptions:{
        onCloseStart:function(){
          instances = M.FormSelect.init(elems,options); // Bugfix
          console.log(instances[0].getSelectedValues());       
        }
      }
    }
    
    var instances = M.FormSelect.init(elems,options);
  
  });

https://codepen.io/doughballs/pen/yLeNMyM

So, re-initialising every time we select, which I guess is pretty much the same as calling _setSelectedState()

doughballs avatar Jul 29 '20 18:07 doughballs

Same problem still. Is there any workaround to get it working without using multiple? I guess I could parse the values generated by the instance in options to get the originals (the ones I set), but it doesn't seem clean.

See above: if you call instance._setSelectedState() before instance.getSelectedValues() then the correct value is returned, even when not using multiple.

I get:

<a class='gotoLine' href='#67:29'>67:29</a> Uncaught TypeError: instance._setSelectedState is not a function

Any idea why?

solroi avatar Nov 07 '22 19:11 solroi