vue.js wrapping components which have v-models












2















I have a 3rd party input component (a vuetify v-text-field).



For reasons of validation i prefer to wrap this component in my own.



my TextField.vue



<template>
<v-text-field
:label="label"
v-model="text"
@input="onInput"
@blur="onBlur"
:error-messages="this.getErrors(this.validation, this.errors)"
></v-text-field>
</template>

<script>
import VTextField from "vuetify/es5/components/VTextField";
import {vuelidateErrorsMixin} from '~/plugins/common.js';
export default {
name: "TextField",
props: ['label', 'value', 'validation', 'errors'],
mixins: [vuelidateErrorsMixin], //add vuelidate
data: function() {
return {
'text': this.value
}
},
components: {
VTextField
},
methods : {
onInput: function(value) {
this.$emit('input', value);
this.validation.$touch();
},
onBlur: function() {
this.validation.$touch();
}
},
watch: {
value: {
immediate: true,
handler: function (newValue) {
this.text = newValue
}
}
}
}
</script>


which is used in another component



<template> 
...
<TextField v-model="personal.email" label="Email"
:validation="$v.personal.email" :errors=""/>
...
</template>
<script>
...imports etc.
export default { ...
data: function() {
return {
personal: {
email: '',
name: ''
}
}
},
components: [ TextField ]
}
</script>


This works fine but i wonder if there is a much more cleaner approach than to replicate the whole v-model approach again. As now my data is duplicated in 2 places + all the extra (non needed) event handling...



I just want to pass the reactive data directly through to the v-text-field from the original temlate. My TextField doesn't actually need access to that data at all - ONLY notified that the text has changed (done via the @input, @blur handlers). I do not wish to use VUEX as this has it's own problems dealing with input / forms...



Something more close to this...



<template>
<v-text-field
:label="label"
v-model="value" //?? SAME AS 'Mine'
@input="onNotify"
@blur="onNotify"
:error-messages="this.getErrors(this.validation, this.errors)"
></v-text-field>
</template>

<script>
import VTextField from "vuetify/es5/components/VTextField";
import {vuelidateErrorsMixin} from '~/plugins/common.js';
export default {
name: "TextField",
props: ['label', 'validation', 'errors'], //NO VALUE HERE as cannot use props...
mixins: [vuelidateErrorsMixin], //add vuelidate
components: {
VTextField
},
methods : {
onNotify: function() {
this.validation.$touch();
}
},
}
</script>


I cannot find anything that would do this.



Using props + v-model wrapping is what i do.










share|improve this question



























    2















    I have a 3rd party input component (a vuetify v-text-field).



    For reasons of validation i prefer to wrap this component in my own.



    my TextField.vue



    <template>
    <v-text-field
    :label="label"
    v-model="text"
    @input="onInput"
    @blur="onBlur"
    :error-messages="this.getErrors(this.validation, this.errors)"
    ></v-text-field>
    </template>

    <script>
    import VTextField from "vuetify/es5/components/VTextField";
    import {vuelidateErrorsMixin} from '~/plugins/common.js';
    export default {
    name: "TextField",
    props: ['label', 'value', 'validation', 'errors'],
    mixins: [vuelidateErrorsMixin], //add vuelidate
    data: function() {
    return {
    'text': this.value
    }
    },
    components: {
    VTextField
    },
    methods : {
    onInput: function(value) {
    this.$emit('input', value);
    this.validation.$touch();
    },
    onBlur: function() {
    this.validation.$touch();
    }
    },
    watch: {
    value: {
    immediate: true,
    handler: function (newValue) {
    this.text = newValue
    }
    }
    }
    }
    </script>


    which is used in another component



    <template> 
    ...
    <TextField v-model="personal.email" label="Email"
    :validation="$v.personal.email" :errors=""/>
    ...
    </template>
    <script>
    ...imports etc.
    export default { ...
    data: function() {
    return {
    personal: {
    email: '',
    name: ''
    }
    }
    },
    components: [ TextField ]
    }
    </script>


    This works fine but i wonder if there is a much more cleaner approach than to replicate the whole v-model approach again. As now my data is duplicated in 2 places + all the extra (non needed) event handling...



    I just want to pass the reactive data directly through to the v-text-field from the original temlate. My TextField doesn't actually need access to that data at all - ONLY notified that the text has changed (done via the @input, @blur handlers). I do not wish to use VUEX as this has it's own problems dealing with input / forms...



    Something more close to this...



    <template>
    <v-text-field
    :label="label"
    v-model="value" //?? SAME AS 'Mine'
    @input="onNotify"
    @blur="onNotify"
    :error-messages="this.getErrors(this.validation, this.errors)"
    ></v-text-field>
    </template>

    <script>
    import VTextField from "vuetify/es5/components/VTextField";
    import {vuelidateErrorsMixin} from '~/plugins/common.js';
    export default {
    name: "TextField",
    props: ['label', 'validation', 'errors'], //NO VALUE HERE as cannot use props...
    mixins: [vuelidateErrorsMixin], //add vuelidate
    components: {
    VTextField
    },
    methods : {
    onNotify: function() {
    this.validation.$touch();
    }
    },
    }
    </script>


    I cannot find anything that would do this.



    Using props + v-model wrapping is what i do.










    share|improve this question

























      2












      2








      2








      I have a 3rd party input component (a vuetify v-text-field).



      For reasons of validation i prefer to wrap this component in my own.



      my TextField.vue



      <template>
      <v-text-field
      :label="label"
      v-model="text"
      @input="onInput"
      @blur="onBlur"
      :error-messages="this.getErrors(this.validation, this.errors)"
      ></v-text-field>
      </template>

      <script>
      import VTextField from "vuetify/es5/components/VTextField";
      import {vuelidateErrorsMixin} from '~/plugins/common.js';
      export default {
      name: "TextField",
      props: ['label', 'value', 'validation', 'errors'],
      mixins: [vuelidateErrorsMixin], //add vuelidate
      data: function() {
      return {
      'text': this.value
      }
      },
      components: {
      VTextField
      },
      methods : {
      onInput: function(value) {
      this.$emit('input', value);
      this.validation.$touch();
      },
      onBlur: function() {
      this.validation.$touch();
      }
      },
      watch: {
      value: {
      immediate: true,
      handler: function (newValue) {
      this.text = newValue
      }
      }
      }
      }
      </script>


      which is used in another component



      <template> 
      ...
      <TextField v-model="personal.email" label="Email"
      :validation="$v.personal.email" :errors=""/>
      ...
      </template>
      <script>
      ...imports etc.
      export default { ...
      data: function() {
      return {
      personal: {
      email: '',
      name: ''
      }
      }
      },
      components: [ TextField ]
      }
      </script>


      This works fine but i wonder if there is a much more cleaner approach than to replicate the whole v-model approach again. As now my data is duplicated in 2 places + all the extra (non needed) event handling...



      I just want to pass the reactive data directly through to the v-text-field from the original temlate. My TextField doesn't actually need access to that data at all - ONLY notified that the text has changed (done via the @input, @blur handlers). I do not wish to use VUEX as this has it's own problems dealing with input / forms...



      Something more close to this...



      <template>
      <v-text-field
      :label="label"
      v-model="value" //?? SAME AS 'Mine'
      @input="onNotify"
      @blur="onNotify"
      :error-messages="this.getErrors(this.validation, this.errors)"
      ></v-text-field>
      </template>

      <script>
      import VTextField from "vuetify/es5/components/VTextField";
      import {vuelidateErrorsMixin} from '~/plugins/common.js';
      export default {
      name: "TextField",
      props: ['label', 'validation', 'errors'], //NO VALUE HERE as cannot use props...
      mixins: [vuelidateErrorsMixin], //add vuelidate
      components: {
      VTextField
      },
      methods : {
      onNotify: function() {
      this.validation.$touch();
      }
      },
      }
      </script>


      I cannot find anything that would do this.



      Using props + v-model wrapping is what i do.










      share|improve this question














      I have a 3rd party input component (a vuetify v-text-field).



      For reasons of validation i prefer to wrap this component in my own.



      my TextField.vue



      <template>
      <v-text-field
      :label="label"
      v-model="text"
      @input="onInput"
      @blur="onBlur"
      :error-messages="this.getErrors(this.validation, this.errors)"
      ></v-text-field>
      </template>

      <script>
      import VTextField from "vuetify/es5/components/VTextField";
      import {vuelidateErrorsMixin} from '~/plugins/common.js';
      export default {
      name: "TextField",
      props: ['label', 'value', 'validation', 'errors'],
      mixins: [vuelidateErrorsMixin], //add vuelidate
      data: function() {
      return {
      'text': this.value
      }
      },
      components: {
      VTextField
      },
      methods : {
      onInput: function(value) {
      this.$emit('input', value);
      this.validation.$touch();
      },
      onBlur: function() {
      this.validation.$touch();
      }
      },
      watch: {
      value: {
      immediate: true,
      handler: function (newValue) {
      this.text = newValue
      }
      }
      }
      }
      </script>


      which is used in another component



      <template> 
      ...
      <TextField v-model="personal.email" label="Email"
      :validation="$v.personal.email" :errors=""/>
      ...
      </template>
      <script>
      ...imports etc.
      export default { ...
      data: function() {
      return {
      personal: {
      email: '',
      name: ''
      }
      }
      },
      components: [ TextField ]
      }
      </script>


      This works fine but i wonder if there is a much more cleaner approach than to replicate the whole v-model approach again. As now my data is duplicated in 2 places + all the extra (non needed) event handling...



      I just want to pass the reactive data directly through to the v-text-field from the original temlate. My TextField doesn't actually need access to that data at all - ONLY notified that the text has changed (done via the @input, @blur handlers). I do not wish to use VUEX as this has it's own problems dealing with input / forms...



      Something more close to this...



      <template>
      <v-text-field
      :label="label"
      v-model="value" //?? SAME AS 'Mine'
      @input="onNotify"
      @blur="onNotify"
      :error-messages="this.getErrors(this.validation, this.errors)"
      ></v-text-field>
      </template>

      <script>
      import VTextField from "vuetify/es5/components/VTextField";
      import {vuelidateErrorsMixin} from '~/plugins/common.js';
      export default {
      name: "TextField",
      props: ['label', 'validation', 'errors'], //NO VALUE HERE as cannot use props...
      mixins: [vuelidateErrorsMixin], //add vuelidate
      components: {
      VTextField
      },
      methods : {
      onNotify: function() {
      this.validation.$touch();
      }
      },
      }
      </script>


      I cannot find anything that would do this.



      Using props + v-model wrapping is what i do.







      vue.js nested components v-model






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 6 '18 at 15:44









      hobbit_behobbit_be

      312




      312
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You need to forward the value prop down to the wrapped component, and forward the update event back up (see https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components for more details):



          <template>
          <wrapped-component
          :value='value'
          @input="update"
          />
          </template>

          <script>
          import wrappedComponent from 'wrapped-component'

          export default {
          components: { 'wrapped-component': wrappedComponent },
          props: ['value'],
          methods: {
          update(newValue) { this.$emit('input', newValue); }
          }
          }
          </script>


          Somewhere else:



          <my-wrapping-component v-model='whatever'/>





          share|improve this answer

































            -1














            I've create a mixin to simplify wrapping of a component.



            You can see a sample here.



            The mixin reuse the same pattern as you with "data" to pass the value and "watch" to update the value during a external change.



            export default {
            data: function() {
            return {
            dataValue: this.value
            }
            },
            props: {
            value: String
            },
            watch: {
            value: {
            immediate: true,
            handler: function(newValue) {
            this.dataValue = newValue
            }
            }
            }
            }


            But on the wraping component, you can use "attrs" and "listeners" to passthrough all attributes and listener to your child component and override what you want.



            <template>
            <div>
            <v-text-field
            v-bind="$attrs"
            solo
            @blur="onBlur"
            v-model="dataValue"
            v-on="$listeners" />
            </div>
            </template>

            <script>
            import mixin from '../mixins/ComponentWrapper.js'

            export default {
            name: 'my-v-text-field',
            mixins: [mixin],
            methods: {
            onBlur() {
            console.log('onBlur')
            }
            }
            }
            </script>





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50724477%2fvue-js-wrapping-components-which-have-v-models%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You need to forward the value prop down to the wrapped component, and forward the update event back up (see https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components for more details):



              <template>
              <wrapped-component
              :value='value'
              @input="update"
              />
              </template>

              <script>
              import wrappedComponent from 'wrapped-component'

              export default {
              components: { 'wrapped-component': wrappedComponent },
              props: ['value'],
              methods: {
              update(newValue) { this.$emit('input', newValue); }
              }
              }
              </script>


              Somewhere else:



              <my-wrapping-component v-model='whatever'/>





              share|improve this answer






























                2














                You need to forward the value prop down to the wrapped component, and forward the update event back up (see https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components for more details):



                <template>
                <wrapped-component
                :value='value'
                @input="update"
                />
                </template>

                <script>
                import wrappedComponent from 'wrapped-component'

                export default {
                components: { 'wrapped-component': wrappedComponent },
                props: ['value'],
                methods: {
                update(newValue) { this.$emit('input', newValue); }
                }
                }
                </script>


                Somewhere else:



                <my-wrapping-component v-model='whatever'/>





                share|improve this answer




























                  2












                  2








                  2







                  You need to forward the value prop down to the wrapped component, and forward the update event back up (see https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components for more details):



                  <template>
                  <wrapped-component
                  :value='value'
                  @input="update"
                  />
                  </template>

                  <script>
                  import wrappedComponent from 'wrapped-component'

                  export default {
                  components: { 'wrapped-component': wrappedComponent },
                  props: ['value'],
                  methods: {
                  update(newValue) { this.$emit('input', newValue); }
                  }
                  }
                  </script>


                  Somewhere else:



                  <my-wrapping-component v-model='whatever'/>





                  share|improve this answer















                  You need to forward the value prop down to the wrapped component, and forward the update event back up (see https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components for more details):



                  <template>
                  <wrapped-component
                  :value='value'
                  @input="update"
                  />
                  </template>

                  <script>
                  import wrappedComponent from 'wrapped-component'

                  export default {
                  components: { 'wrapped-component': wrappedComponent },
                  props: ['value'],
                  methods: {
                  update(newValue) { this.$emit('input', newValue); }
                  }
                  }
                  </script>


                  Somewhere else:



                  <my-wrapping-component v-model='whatever'/>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 14 '18 at 2:16

























                  answered Nov 13 '18 at 22:01









                  MeekohiMeekohi

                  6,16933746




                  6,16933746

























                      -1














                      I've create a mixin to simplify wrapping of a component.



                      You can see a sample here.



                      The mixin reuse the same pattern as you with "data" to pass the value and "watch" to update the value during a external change.



                      export default {
                      data: function() {
                      return {
                      dataValue: this.value
                      }
                      },
                      props: {
                      value: String
                      },
                      watch: {
                      value: {
                      immediate: true,
                      handler: function(newValue) {
                      this.dataValue = newValue
                      }
                      }
                      }
                      }


                      But on the wraping component, you can use "attrs" and "listeners" to passthrough all attributes and listener to your child component and override what you want.



                      <template>
                      <div>
                      <v-text-field
                      v-bind="$attrs"
                      solo
                      @blur="onBlur"
                      v-model="dataValue"
                      v-on="$listeners" />
                      </div>
                      </template>

                      <script>
                      import mixin from '../mixins/ComponentWrapper.js'

                      export default {
                      name: 'my-v-text-field',
                      mixins: [mixin],
                      methods: {
                      onBlur() {
                      console.log('onBlur')
                      }
                      }
                      }
                      </script>





                      share|improve this answer




























                        -1














                        I've create a mixin to simplify wrapping of a component.



                        You can see a sample here.



                        The mixin reuse the same pattern as you with "data" to pass the value and "watch" to update the value during a external change.



                        export default {
                        data: function() {
                        return {
                        dataValue: this.value
                        }
                        },
                        props: {
                        value: String
                        },
                        watch: {
                        value: {
                        immediate: true,
                        handler: function(newValue) {
                        this.dataValue = newValue
                        }
                        }
                        }
                        }


                        But on the wraping component, you can use "attrs" and "listeners" to passthrough all attributes and listener to your child component and override what you want.



                        <template>
                        <div>
                        <v-text-field
                        v-bind="$attrs"
                        solo
                        @blur="onBlur"
                        v-model="dataValue"
                        v-on="$listeners" />
                        </div>
                        </template>

                        <script>
                        import mixin from '../mixins/ComponentWrapper.js'

                        export default {
                        name: 'my-v-text-field',
                        mixins: [mixin],
                        methods: {
                        onBlur() {
                        console.log('onBlur')
                        }
                        }
                        }
                        </script>





                        share|improve this answer


























                          -1












                          -1








                          -1







                          I've create a mixin to simplify wrapping of a component.



                          You can see a sample here.



                          The mixin reuse the same pattern as you with "data" to pass the value and "watch" to update the value during a external change.



                          export default {
                          data: function() {
                          return {
                          dataValue: this.value
                          }
                          },
                          props: {
                          value: String
                          },
                          watch: {
                          value: {
                          immediate: true,
                          handler: function(newValue) {
                          this.dataValue = newValue
                          }
                          }
                          }
                          }


                          But on the wraping component, you can use "attrs" and "listeners" to passthrough all attributes and listener to your child component and override what you want.



                          <template>
                          <div>
                          <v-text-field
                          v-bind="$attrs"
                          solo
                          @blur="onBlur"
                          v-model="dataValue"
                          v-on="$listeners" />
                          </div>
                          </template>

                          <script>
                          import mixin from '../mixins/ComponentWrapper.js'

                          export default {
                          name: 'my-v-text-field',
                          mixins: [mixin],
                          methods: {
                          onBlur() {
                          console.log('onBlur')
                          }
                          }
                          }
                          </script>





                          share|improve this answer













                          I've create a mixin to simplify wrapping of a component.



                          You can see a sample here.



                          The mixin reuse the same pattern as you with "data" to pass the value and "watch" to update the value during a external change.



                          export default {
                          data: function() {
                          return {
                          dataValue: this.value
                          }
                          },
                          props: {
                          value: String
                          },
                          watch: {
                          value: {
                          immediate: true,
                          handler: function(newValue) {
                          this.dataValue = newValue
                          }
                          }
                          }
                          }


                          But on the wraping component, you can use "attrs" and "listeners" to passthrough all attributes and listener to your child component and override what you want.



                          <template>
                          <div>
                          <v-text-field
                          v-bind="$attrs"
                          solo
                          @blur="onBlur"
                          v-model="dataValue"
                          v-on="$listeners" />
                          </div>
                          </template>

                          <script>
                          import mixin from '../mixins/ComponentWrapper.js'

                          export default {
                          name: 'my-v-text-field',
                          mixins: [mixin],
                          methods: {
                          onBlur() {
                          console.log('onBlur')
                          }
                          }
                          }
                          </script>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 13 '18 at 22:37









                          Julien CroainJulien Croain

                          692




                          692






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50724477%2fvue-js-wrapping-components-which-have-v-models%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine