Search code examples
inputvue.jsbulmabuefy

How to clear Buefy input after event (VueJS)?


I am trying to clear Buefy input with an event but nothing is happening. However this code work with basic input.

Here is my HTML:

<b-field>
  <b-input
    id="itemForm"
    placeholder="label"
    @keyup.enter.native="addItem">
  </b-input>
</b-field>

Here is my script:

methods: {
  addItem () {
    var input = document.getElementById('itemForm')

    if (input.value !== '') {
      this.items.push({
        name: input.value
      })
      input.value = ''
    }
  }
}

Solution

  • I tried, and i am not sure but the only way to use @keyup.enter using buefy is: @keyup.native.enter

    However i think you want something like this: see it in action

    <div id="app" class="container">
    
      <ul>
        <li v-for="section in sections" :key="section.id">
          {{section.name}}
        </li>
      </ul>
    
        <section >
            <b-field label="Name">
                <b-input v-model.trim="name" @keyup.native.enter="addItem()" placeholder="Write and press enter"></b-input>
            </b-field>
        </section>
    
    </div>
    

    And the script:

    Vue.use(Buefy.default)
    
    
        const example = {
            data() {
                return {
                  sections: [],
                  name: ''
                }
            },
          methods: {
            addItem () {
              this.sections.push({
                name: this.name,
                id: Date.now()
              })
    
              this.name = ''
            }
          }
        }
    
    
    const app = new Vue(example)
    
    app.$mount('#app')