0

So I created a simple component:

Vue.component('m-textbox', {
  template: `
   <div>
    <div class="input-field col s12 m6 l6">
      <input :id="id" type="text" :value="value" @input="$emit('input', $event.target.value)">
      <label :for="id">{{ label }}</label>
    </div>
   </div>
  `,
  props: ["id", "value", "label", "for"]
})

And I use it in my html like this:

<m-textbox v-model="full_name" id="full_name" label="Full Name"></m-textbox>

Now what I wanted to do is to make the columns settable. The default as you can see is col 12 m6 l6. Is there a way to make it dynamic? Like for example I can just do <m-textbox v-model="full_name" id="full_name" label="Full Name" classSize="col s12 m6"></m-textbox>.

Any help would be much appreciated.

2 Answers 2

1

It should be really simple hopefully. Not sure if syntax is correct as I am using webpack and single file components, so sorry if some issue with syntax.

One way:

Vue.component('m-textbox', {
  template: 
   `<div>
    <div :class="size">
      <input :id="id" type="text" :value="value" @input="$emit('input', $event.target.value)">
      <label :for="id">{{ label }}</label>
    </div>
   </div>`
  ,
  props: ["id", "value", "label", "for", 
           size: 
           { type: 'string', default: 'input-field col s12 m6 l6'}]
})

Other way:

Vue.component('m-textbox', {
  template: 
    `<div class="input-field">
      <input :id="id" type="text" :value="value" @input="$emit('input', $event.target.value)">
      <label :for="id">{{ label }}</label>
    </div>`
  ,
      props: ["id", "value", "label", "for"]
    })

and use it: <m-textbox v-model="full_name" id="full_name" label="Full Name" v-bind:class="[col, s12, m6]"></m-textbox>

Sign up to request clarification or add additional context in comments.

3 Comments

I get an invalid expression: :class="col s12 m6"
fixed. you have to use v-bind and []
accept answer than as correct to make it ok for other people finding it
1

You could remove the outer div in your component template and simply add the class attribute to your component. It will even merge your classes.

Vue.component('m-textbox', {
  template: `
    <div class="input-field col">
      <input :id="id" type="text" :value="value" @input="$emit('input', $event.target.value)">
      <label :for="id">{{ label }}</label>
    </div>
  `,
  props: ["id", "value", "label", "for"]
})

<m-textbox v-model="full_name" id="full_name" label="Full Name" class="s12 m6 l6"></m-textbox>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.