Search code examples
dartdart-polymer

How to Query shadowRoot content elements in Polymer dart


How to query shadowRoot Content Elements. I create a custom form element, this form have children provided outside the shadowRoot, FormInput, also custom elements. I need to get all children from the shadowRoot. If I use shadowRoot.query or shadowRoot.queryAll, has the same effect from outside the component, I can't view the children, is obfuscated by shadowRoot. I'm trying to use shadowRoot.query("content").getDistributedNodes(); but what hell is this text element in the node list, my children is only one.

@CustomTag("v-form")
class Form extends VaderComponent {
  bool isValid(){
    bool valid = true;
    var nodes  = shadowRoot.query("content").getDistributedNodes();
    nodes.forEach((element) {
      window.console.debug(element);
      element.queryAll("v-input").forEach((elementTarget){
        if(elementTarget is FormItem){
          window.console.info("É um item de formulário");
          if(elementTarget.xtag.isValid() == false){
            valid = false;
          }
        }

      });

    });
    return valid;
  }
}

<polymer-element name="v-form">
    <template>
        <form>
            <content></content>
        </form>
    </template>
    <script type="application/dart" src="Form.dart"></script>
</polymer-element>


 <v-form id="form">
            <fieldset>
                <legend>Fieldset</legend>
                <div class="row">
                    <div class="large12 columns">

                        <v-input title="Password Example" type="password" placeholder="large-12.columns" />
                    </div>
                </div>
                <div class="row">
                    <div class="large-12 columns">
                        <v-input title="Mask Input" mask="999" type="tel" value="Valor" placeholder="large-12.columns"></v-input>
                    </div>
                </div>

                <div class="row">
                    <div clas="large-4 columns">

                        <v-input title="Input Label" type="date" placeholder="large-4.columns"></v-input>
                    </div>
                    <div class="large-4 columns">

                        <v-input title="Input Label" type="text" allowEpty="false" placeholder="Not Allow Empty Value"></v-input>
                    </div>
                    <div class="large-4 columns">
                        <div class="row collapse">

                            <div class="small-9 columns">
                                <v-input title="Input Label" type="text" placeholder="small-9.columns"></v-input>
                            </div>
                            <div class="small-3 columns">
                                <span class="postfix">.com</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="large-12 columns">
                        <label>Textarea Label</label>
                        <textarea placeholder="small-12.columns"></textarea>
                    </div>
                </div>

            </fieldset>
        </v-form>

The error on call isValid:

#text
 undefined:1
Uncaught Error: Class 'Text' has no instance method 'queryAll'.

NoSuchMethodError : method not found: 'queryAll'
Receiver: Instance of 'Text'
Arguments: ["v-input"] 

If I just output the item in console.debug I get the follow:

#text
<fieldset>​…​</fieldset>​
#text

Is there a better method? this code is ugly (two forEach (n^2))


Solution

  • shadowRoot and the are different. Can be queried as normal because is part of lightdom. The principal code become:

    var nodes  = this.queryAll("v-input");
    nodes.forEach((element){
    if(elementTarget is FormItem){
          window.console.info("É um item de formulário");
          if(elementTarget.xtag.isValid() == false){
            valid = false;
          }
    }
    });
    return valid;
    

    This code works as expected