I'm trying to make a simple multi-step form for my Rails 5 app using Vue.js. It's my first time using Vue so I am a bit stuck on how to make things work properly.
Right now I am trying to do the following:
- Click a Next button and set the
lielements from step 1 to step N to have a classactive. - Click a Previous button and remove class
activefrom step N.
Quite simple. Here's what I've got so far but I don't know where to go from here:
import Vue from 'vue/dist/vue.esm'
document.addEventListener('DOMContentLoaded', () => {
Vue.component('step-item', {
props: ['step'],
template: '<li :class="{active: isActive}">{{ step.text }}</li>',
data: function() {
return {
isActive: true // Right now I set them all to true
}
}
})
Vue.component('step-button', {
props: ['name'],
template: "<input :name='name' :value='name' @click='counter += 1' type='button' class='btn btn-secondary' />"
})
const progress = new Vue({
el: '#vue-listing',
data: {
counter: 0,
stepList: [
{id: 0, text: 'Basics'},
{id: 1, text: 'Location'},
{id: 2, text: 'Images'},
{id: 3, text: 'Other'}
]
},
methods: {
addProgress: function() {return true}, // todo
delProgress: function() {return true} // todo
}
})
})
then I have my form
<div id="vue-listing">
<!-- Display the progress through the form -->
<ul class="text-center" id="listing-progressbar">
<step-item v-for="item in stepList" :step="item" :key="item.id"></step-item>
</ul>
<fieldset>
Step 1
<step-button name="Next"></step-button>
</fieldset>
<fieldset>
Step 2
<step-button name="Previous"></step-button>
<step-button name="Next"></step-button>
</fieldset>
</div>
Right now I'm stuck on how to get the next and previous buttons to add or remove active from the current step.
Eventually I will also need to hide and display the <fieldset> elements depending on the step.
Any help is much appreciated!
Thanks