Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v-expansion-panel): converted to typescript #4609

Merged
merged 7 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import '../../stylus/components/_expansion-panel.styl'

import { VExpansionPanelContent } from '.'

import Themeable from '../../mixins/themeable'
import { provide as RegistrableProvide } from '../../mixins/registrable'

import mixins from '../../util/mixins'
import { VNode } from 'vue'
import { PropValidator } from 'vue/types/options'

type VExpansionPanelContentInstance = InstanceType<typeof VExpansionPanelContent>

/* @vue/component */
export default {
export default mixins(Themeable, RegistrableProvide('expansionPanel')).extend({
name: 'v-expansion-panel',

mixins: [Themeable, RegistrableProvide('expansionPanel')],

provide () {
provide (): object {
return {
expansionPanel: this
}
Expand All @@ -25,16 +31,16 @@ export default {
value: {
type: [Number, Array],
default: () => null
}
} as any as PropValidator<number | number[]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is any needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it being done in the code somewhere, and simply as PropValidator<number | number[]> threw errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that's fine then, just checking.

},

data: () => ({
items: [],
open: []
items: [] as VExpansionPanelContentInstance[],
open: [] as boolean[]
}),

computed: {
classes () {
classes (): object {
return {
'v-expansion-panel--focusable': this.focusable,
'v-expansion-panel--popout': this.popout,
Expand All @@ -45,11 +51,11 @@ export default {
},

watch: {
expand (val) {
let openIndex
if (!val) {
expand (v: boolean) {
let openIndex = -1
if (!v) {
// Close all panels unless only one is open
const openCount = this.open.reduce((acc, val) => acc + val, 0)
const openCount = this.open.reduce((acc, val) => val ? acc + 1 : acc, 0)
const open = Array(this.items.length).fill(false)

if (openCount === 1) {
Expand All @@ -63,9 +69,9 @@ export default {
this.open = open
}

this.$emit('input', val ? this.open : (openIndex > -1 ? openIndex : null))
this.$emit('input', v ? this.open : (openIndex > -1 ? openIndex : null))
},
value (v) {
value (v: number | number[]) {
this.updateFromValue(v)
}
},
Expand All @@ -75,7 +81,7 @@ export default {
},

methods: {
updateFromValue (v) {
updateFromValue (v: number | number[]) {
if (Array.isArray(v) && !this.expand) return

let open = Array(this.items.length).fill(false)
Expand All @@ -87,17 +93,17 @@ export default {

this.updatePanels(open)
},
updatePanels (open) {
updatePanels (open: boolean[]) {
this.open = open
for (let i = 0; i < this.items.length; i++) {
const active = open && open[i]
this.items[i].toggle(active)
}
},
panelClick (uid) {
panelClick (uid: number) {
const open = this.expand ? this.open.slice() : Array(this.items.length).fill(false)
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].uid === uid) {
if (this.items[i]._uid === uid) {
open[i] = !this.open[i]
!this.expand && this.$emit('input', open[i] ? i : null)
}
Expand All @@ -106,21 +112,21 @@ export default {
this.updatePanels(open)
if (this.expand) this.$emit('input', open)
},
register (uid, toggle) {
this.items.push({ uid, toggle })
register (content: VExpansionPanelContentInstance) {
this.items.push(content)
this.open.push(false)
},
unregister (uid) {
const index = this.items.findIndex(i => i.uid === uid)
unregister (content: VExpansionPanelContentInstance) {
const index = this.items.findIndex(i => i._uid === content._uid)
this.items.splice(index, 1)
this.open.splice(index, 1)
}
},

render (h) {
render (h): VNode {
return h('ul', {
staticClass: 'v-expansion-panel',
class: this.classes
}, this.$slots.default)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,39 @@ import { VExpandTransition } from '../transitions'
import Bootable from '../../mixins/bootable'
import Toggleable from '../../mixins/toggleable'
import Rippleable from '../../mixins/rippleable'
import { inject as RegistrableInject } from '../../mixins/registrable'
import { Registrable, inject as RegistrableInject } from '../../mixins/registrable'

import VIcon from '../VIcon'
import VExpansionPanel from '.'

import mixins, { ExtractVue } from '../../util/mixins'
import Vue, { VNode } from 'vue'

import { consoleWarn } from '../../util/console'

/* @vue/component */
export default {
name: 'v-expansion-panel-content',
type VExpansionPanelInstance = InstanceType<typeof VExpansionPanel>

mixins: [Bootable, Toggleable, Rippleable, RegistrableInject('expansionPanel', 'v-expansion-panel-content', 'v-expansion-panel')],
interface options extends Vue {
expansionPanel: VExpansionPanelInstance
}

inject: ['expansionPanel'],
export default mixins<options &
/* eslint-disable indent */
ExtractVue<
typeof Bootable,
typeof Toggleable,
typeof Rippleable,
Registrable<'expansionPanel'>
>
/* eslint-enable indent */
>(
Bootable,
Toggleable,
Rippleable,
RegistrableInject('expansionPanel', 'v-expansion-panel-content', 'v-expansion-panel')
/* @vue/component */
).extend({
name: 'v-expansion-panel-content',

props: {
disabled: Boolean,
Expand All @@ -36,33 +56,33 @@ export default {
}),

computed: {
containerClasses () {
containerClasses (): object {
return {
'v-expansion-panel__container--active': this.isActive,
'v-expansion-panel__container--disabled': this.isDisabled
}
},
isDisabled () {
isDisabled (): boolean {
return this.expansionPanel.disabled || this.disabled
},
isReadonly () {
isReadonly (): boolean {
return this.expansionPanel.readonly || this.readonly
}
},

mounted () {
this.expansionPanel.register(this._uid, this.toggle)
this.expansionPanel.register(this)

// Can be removed once fully deprecated
if (typeof this.value !== 'undefined') consoleWarn('v-model has been deprecated', this)
},

beforeDestroy () {
this.expansionPanel.unregister(this._uid)
this.expansionPanel.unregister(this)
},

methods: {
onKeydown (e) {
onKeydown (e: KeyboardEvent) {
// Ensure element is the activeElement
if (
e.keyCode === 13 &&
Expand All @@ -79,29 +99,28 @@ export default {
directives: [{
name: 'show',
value: this.isActive
}]
}] as any
}, this.showLazyContent(this.$slots.default))
},
genHeader () {
const children = [...this.$slots.header]

if (!this.hideActions) children.push(this.genIcon())

return this.$createElement('div', {
staticClass: 'v-expansion-panel__header',
directives: [{
name: 'ripple',
value: this.ripple
}],
}] as any,
on: {
click: this.onHeaderClick
}
}, [
this.$slots.header,
this.genIcon()
])
}, children)
},
genIcon () {
if (this.hideActions) return null

const icon = this.$slots.actions ||
this.$createElement(VIcon, this.expandIcon)
[this.$createElement(VIcon, this.expandIcon)]

return this.$createElement('transition', {
attrs: { name: 'fade-transition' }
Expand All @@ -111,11 +130,11 @@ export default {
directives: [{
name: 'show',
value: !this.isDisabled
}]
}, [icon])
}] as any
}, icon)
])
},
toggle (active) {
toggle (active: boolean) {
if (active) this.isBooted = true

// We treat bootable differently
Expand All @@ -124,7 +143,7 @@ export default {
}
},

render (h) {
render (h): VNode {
const children = []

this.$slots.header && children.push(this.genHeader())
Expand All @@ -141,4 +160,4 @@ export default {
}
}, children)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export { VExpansionPanel, VExpansionPanelContent }

/* istanbul ignore next */
VExpansionPanel.install = function install (Vue) {
Vue.component(VExpansionPanel.name, VExpansionPanel)
Vue.component(VExpansionPanelContent.name, VExpansionPanelContent)
Vue.component(VExpansionPanel.options.name, VExpansionPanel)
Vue.component(VExpansionPanelContent.options.name, VExpansionPanelContent)
}

export default VExpansionPanel
4 changes: 2 additions & 2 deletions src/mixins/bootable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export default Vue.extend<ExtractVue<Toggleable>>().extend({
},

methods: {
showLazyContent (content: VNode[]): VNode[] | null {
return this.hasContent ? content : null
showLazyContent (content: VNode[]): VNode[] | undefined {
return this.hasContent ? content : undefined
}
}
})
4 changes: 2 additions & 2 deletions test/unit/components/VExpansionPanel/VExpansionPanel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('VExpansionPanel.js', ({ mount, compileToFunctions }) => {
it('should show content on v-model change', async () => {
const wrapper = mount(VExpansionPanel, {
slots: {
default: VExpansionPanelContent
default: VExpansionPanelContent.options
}
})

Expand All @@ -75,7 +75,7 @@ test('VExpansionPanel.js', ({ mount, compileToFunctions }) => {
value: 0
},
slots: {
default: VExpansionPanelContent
default: VExpansionPanelContent.options
}
})

Expand Down
4 changes: 2 additions & 2 deletions test/unit/mixins/bootable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('bootable.js', ({ mount }) => {
expect(wrapper.vm.isBooted).toBe(true)
})

it('should be return lazy content', async () => {
it('should return lazy content', async () => {
const wrapper = mount({
mixins: [ Bootable ],
render: h => h('div')
Expand All @@ -38,7 +38,7 @@ test('bootable.js', ({ mount }) => {
}
})

expect(wrapperLazy.vm.showLazyContent('content')).toBe(null)
expect(wrapperLazy.vm.showLazyContent('content')).toBe(undefined)
wrapperLazy.vm.isActive = true
await wrapper.vm.$nextTick()
expect(wrapperLazy.vm.showLazyContent('content')).toBe('content')
Expand Down