Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Use provide/inject to register tabs for layout flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cristijora committed Nov 22, 2017
1 parent 9cf6ade commit a8d1f61
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 0 additions & 2 deletions dev-example/WizardRoute.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
<form-wizard @on-complete="onComplete"
@on-change="handleChange"
:start-index.sync="activeIndex"
layout="vertical"
steps-classes="steps-size"
color="#e74c3c">
<tab-content v-for="tab in tabs" :title="tab" :key="tab">{{tab}}</tab-content>
<transition name="fade" mode="out-in">
Expand Down
6 changes: 6 additions & 0 deletions src/components/FormWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
}
}
},
provide () {
return {
addTab: this.addTab,
removeTab: this.removeTab
}
},
data () {
return {
activeTabIndex: 0,
Expand Down
5 changes: 3 additions & 2 deletions src/components/TabContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
default: () => {}
}
},
inject: ['addTab', 'removeTab'],
data () {
return {
active: false,
Expand All @@ -57,13 +58,13 @@
}
},
mounted () {
this.$parent.addTab(this)
this.addTab(this)
},
destroyed () {
if (this.$el && this.$el.parentNode) {
this.$el.parentNode.removeChild(this.$el)
}
this.$parent.removeTab(this)
this.removeTab(this)
}
}
</script>
40 changes: 34 additions & 6 deletions test/unit/specs/FormWizard.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import VueFormWizard from './../../../src/index'
import {TabContent as WizardTab, WizardStep, FormWizard} from './../../../src/index'
import VueFormWizard, {TabContent as WizardTab, WizardStep, FormWizard} from './../../../src/index'
import {mount, createLocalVue} from 'vue-test-utils'
import sinon from 'sinon'
import Vue from "vue";
import Vue from 'vue'

const localVue = createLocalVue()
localVue.use(VueFormWizard)
Expand All @@ -28,14 +27,30 @@ const twoStepWizard = {
}
}
}

const divSlot = `<div>
<tab-content title="Personal details"
icon="ti-user">
First
</tab-content>
<tab-content title="Additional Info"
icon="ti-settings">
Second
</tab-content>
<tab-content title="Last step"
icon="ti-settings">
Third
</tab-content>
</div>`

describe('FormWizard.vue', () => {
it('contains wizard class', () => {
const wizard = mount(twoStepWizard, {localVue})
wizard.hasClass('vue-form-wizard')
})
it('renders steps', () => {
it('renders steps', (done) => {
const wizard = mount(twoStepWizard, {localVue})
Vue.nextTick(()=>{
Vue.nextTick(() => {
const steps = wizard.findAll(WizardStep)
const firsStep = steps.at(0)
expect(steps.length).to.equal(3)
Expand All @@ -44,8 +59,8 @@ describe('FormWizard.vue', () => {
expect(stepTitle.is('span')).to.equal(true)
const stepText = stepTitle.text().trim()
expect(stepText).to.equal('Personal details')
done()
})

})
it('renders tabs', () => {
const wizard = mount(twoStepWizard, {localVue})
Expand Down Expand Up @@ -81,4 +96,17 @@ describe('FormWizard.vue', () => {
expect(nextTabHandler.called).to.equal(true)
})

it('renders tab wrapped in another element', done => {
const wizard = mount(FormWizard, {
localVue,
slots: {
default: divSlot
}
})
Vue.nextTick(() => {
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
done()
})
})
})

0 comments on commit a8d1f61

Please sign in to comment.