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

TypeScript Rollout Tier 5 - Navbar #347

Merged
merged 4 commits into from
Jan 5, 2025
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
1 change: 0 additions & 1 deletion packages/buefy-next/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const JS_COMPONENTS = [
'menu',
'message',
'modal',
'navbar',
'notification',
'numberinput',
'pagination',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import BNavbar from '@components/navbar/Navbar'
import type { VueWrapper } from '@vue/test-utils'
import BNavbar from '@components/navbar/Navbar.vue'

describe('BNavbar', () => {
let wrapper
let wrapper: VueWrapper<InstanceType<typeof BNavbar>>
beforeEach(() => {
wrapper = mount(BNavbar)
})
Expand Down Expand Up @@ -46,7 +48,7 @@ describe('BNavbar', () => {
})

it('manage closing the menu as expected', async () => {
wrapper.vm.emitUpdateParentEvent = jest.fn()
wrapper.vm.emitUpdateParentEvent = vi.fn()

await wrapper.setProps({
modelValue: true,
Expand All @@ -65,7 +67,7 @@ describe('BNavbar', () => {
})

it('set body classes as expected', async () => {
wrapper.vm.setBodyClass = jest.fn(() => wrapper.vm.setBodyClass)
wrapper.vm.setBodyClass = vi.fn(() => wrapper.vm.setBodyClass)

await wrapper.setProps({ fixedTop: true })
expect(wrapper.vm.setBodyClass).toHaveBeenCalled()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { shallowMount, mount } from '@vue/test-utils'
import BNavbarBurger from '@components/navbar/NavbarBurger'
import type { VueWrapper } from '@vue/test-utils'
import BNavbarBurger from '@components/navbar/NavbarBurger.vue'

describe('BNavbarBurger', () => {
let wrapper
let wrapper: VueWrapper<InstanceType<typeof BNavbarBurger>>
beforeEach(() => {
wrapper = shallowMount(BNavbarBurger)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue'
import { mount, shallowMount } from '@vue/test-utils'
import BNavbarItem from '@components/navbar/NavbarItem'
import type { VueWrapper } from '@vue/test-utils'
import BNavbarItem from '@components/navbar/NavbarItem.vue'

let wrapper
let wrapper: VueWrapper<InstanceType<typeof BNavbarItem>>

const stubBNavBar = {
const stubBNavBar = defineComponent({
data() {
return {
_isNavBar: true
}
},
methods: {
closeMenu: vi.fn()
},
template: `
<div>
<slot />
</div>`
}
})

describe('BNavbarItem', () => {
const tag = 'div'
Expand All @@ -36,7 +42,7 @@ describe('BNavbarItem', () => {
})

it('emit event from tag and out', async () => {
const testStub = jest.fn()
const testStub = vi.fn()

const emitWrap = shallowMount(BNavbarItem, {
props: {
Expand All @@ -60,11 +66,12 @@ describe('BNavbarItem', () => {
default: BNavbarItem
}
}).findComponent(BNavbarItem)
wrapper.vm.$parent.closeMenu = jest.fn()
stubBNavBar.methods!.closeMenu.mockClear()
const event = new KeyboardEvent('keyup', { key: 'Escape' })
wrapper.vm.keyPress({})
wrapper.vm.keyPress(event)
expect(wrapper.vm.$parent.closeMenu).toHaveBeenCalledTimes(1)
expect(stubBNavBar.methods!.closeMenu).toHaveBeenCalledTimes(1)
stubBNavBar.methods!.closeMenu.mockClear()
})

it('manage click as expected', () => {
Expand All @@ -73,12 +80,13 @@ describe('BNavbarItem', () => {
default: BNavbarItem
}
}).findComponent(BNavbarItem)
wrapper.vm.$parent.closeMenu = jest.fn()
stubBNavBar.methods!.closeMenu.mockClear()
const event = new MouseEvent('click')
wrapper.vm.handleClickEvent({
...event,
target: { localName: 'a' }
})
expect(wrapper.vm.$parent.closeMenu).toHaveBeenCalledTimes(1)
expect(stubBNavBar.methods!.closeMenu).toHaveBeenCalledTimes(1)
stubBNavBar.methods!.closeMenu.mockClear()
})
})
38 changes: 22 additions & 16 deletions packages/buefy-next/src/components/navbar/Navbar.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script>
<script lang="ts">
import {
defineComponent,
h as createElement,
resolveComponent,
resolveDirective,
withDirectives
} from 'vue'
import type { PropType, StyleValue, VNode } from 'vue'

import NavbarBurger from './NavbarBurger.vue'
import clickOutside from '../../directives/clickOutside'
import type { VueClassAttribute } from '../../utils/config'

const FIXED_TOP_CLASS = 'is-fixed-top'
const BODY_FIXED_TOP_CLASS = 'has-navbar-fixed-top'
Expand All @@ -17,9 +20,9 @@ const BODY_FIXED_BOTTOM_CLASS = 'has-navbar-fixed-bottom'
const BODY_SPACED_FIXED_BOTTOM_CLASS = 'has-spaced-navbar-fixed-bottom'
const BODY_CENTERED_CLASS = 'has-navbar-centered'

const isFilled = (str) => !!str
const isFilled = (str?: StyleValue) => !!str

export default {
export default defineComponent({
name: 'BNavbar',
components: {
NavbarBurger
Expand All @@ -28,7 +31,7 @@ export default {
clickOutside
},
props: {
type: [String, Object],
type: [String, Object] as PropType<VueClassAttribute>,
transparent: {
type: Boolean,
default: false
Expand All @@ -50,7 +53,7 @@ export default {
default: false
},
wrapperClass: {
type: [String, Array, Object]
type: [String, Array, Object] as PropType<StyleValue>
},
closeOnClick: {
type: Boolean,
Expand All @@ -63,7 +66,10 @@ export default {
spaced: Boolean,
shadow: Boolean
},
emits: ['update:modelValue'],
emits: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
'update:modelValue': (_value: boolean) => true
},
data() {
return {
internalIsActive: this.modelValue,
Expand Down Expand Up @@ -118,12 +124,12 @@ export default {
emitUpdateParentEvent() {
this.$emit('update:modelValue', this.internalIsActive)
},
setBodyClass(className) {
setBodyClass(className: string) {
if (typeof window !== 'undefined') {
document.body.classList.add(className)
}
},
removeBodyClass(className) {
removeBodyClass(className: string) {
if (typeof window !== 'undefined') {
document.body.classList.remove(className)
}
Expand Down Expand Up @@ -153,7 +159,7 @@ export default {

return this.genNavbarSlots([navWrapper])
},
genNavbarSlots(slots) {
genNavbarSlots(slots: VNode[]) {
const vnode = createElement(
'nav',
{
Expand Down Expand Up @@ -184,7 +190,7 @@ export default {
{
isOpened: this.isOpened,
onClick: this.toggleActive,
onKeyup: (event) => {
onKeyup: (event: KeyboardEvent) => {
if (event.keyCode !== 13) return
this.toggleActive()
}
Expand All @@ -193,7 +199,7 @@ export default {

const hasBurgerSlot = !!this.$slots.burger
return hasBurgerSlot
? this.$slots.burger({
? this.$slots.burger!({
isOpened: this.isOpened,
toggleActive: this.toggleActive
})
Expand All @@ -210,16 +216,16 @@ export default {
]
)
},
genMenuPosition(positionName) {
genMenuPosition(positionName: 'start' | 'end') {
return createElement(
'div',
{ class: `navbar-${positionName}` },
this.$slots[positionName] != null
? this.$slots[positionName]()
? this.$slots[positionName]!()
: []
)
},
setBodyFixedTopClass(isSet) {
setBodyFixedTopClass(isSet: boolean) {
this.checkIfFixedPropertiesAreColliding()
if (isSet) {
// TODO Apply only one of the classes once PR is merged in Bulma:
Expand All @@ -231,7 +237,7 @@ export default {
this.removeBodyClass(BODY_SPACED_FIXED_TOP_CLASS)
}
},
setBodyFixedBottomClass(isSet) {
setBodyFixedBottomClass(isSet: boolean) {
this.checkIfFixedPropertiesAreColliding()
if (isSet) {
// TODO Apply only one of the classes once PR is merged in Bulma:
Expand Down Expand Up @@ -264,5 +270,5 @@ export default {
render() {
return this.genNavbar()
}
}
})
</script>
8 changes: 5 additions & 3 deletions packages/buefy-next/src/components/navbar/NavbarBurger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
</a>
</template>

<script>
export default {
<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
name: 'NavbarBurger',
props: {
isOpened: {
type: Boolean,
default: false
}
}
}
})
</script>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { shallowMount } from '@vue/test-utils'
import BNavbarDropdown from '@components/navbar/NavbarDropdown'
import type { VueWrapper } from '@vue/test-utils'
import BNavbarDropdown from '@components/navbar/NavbarDropdown.vue'

let wrapper
let wrapper: VueWrapper<InstanceType<typeof BNavbarDropdown>>

describe('BNavbarDropdown', () => {
beforeEach(() => {
Expand Down
15 changes: 10 additions & 5 deletions packages/buefy-next/src/components/navbar/NavbarDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
</div>
</template>

<script>
<script lang="ts">
import { defineComponent } from 'vue'

import clickOutside from '../../directives/clickOutside'
import CompatFallthroughMixin from '../../utils/CompatFallthroughMixin'

export default {
export default defineComponent({
name: 'BNavbarDropdown',
directives: {
clickOutside
Expand All @@ -67,7 +69,10 @@ export default {
default: 'a'
}
},
emits: ['active-change'],
emits: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
'active-change': (_active: boolean) => true
},
data() {
return {
newActive: this.active,
Expand All @@ -91,7 +96,7 @@ export default {
showMenu() {
this.newActive = true
},
/**
/*
* See naming convetion of navbaritem
*/
closeMenu() {
Expand All @@ -106,5 +111,5 @@ export default {
}
}
}
}
})
</script>
Loading
Loading