Skip to content

Commit

Permalink
add linkComponent prop on vue
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart committed Dec 1, 2023
1 parent 5d3c8bd commit 5459dde
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
6 changes: 0 additions & 6 deletions components/DocMenu/vue/DocMenu.rootstory.tsx

This file was deleted.

19 changes: 16 additions & 3 deletions components/DocMenu/vue/DocMenu.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts" setup>
import type { DefineComponent } from 'vue'
import { NavGroup, NavItemLink } from '@cypress-design/constants-docmenu'
import DocGroup from './_DocGroup.vue'
import DocLink from './_DocLink.vue'
Expand All @@ -7,20 +8,32 @@ withDefaults(
defineProps<{
items: (NavItemLink | NavGroup)[]
collapsible?: boolean
linkComponent?: DefineComponent | 'a'
}>(),
{
linkComponent: 'a',
collapsible: true,
}
},
)
</script>

<template>
<ul class="overflow-y-hidden list-none p-0">
<template v-for="item in items">
<li v-if="'items' in item" class="relative list-none p-0">
<DocGroup :group="item" :depth="0" :collapsible="collapsible" />
<DocGroup
:group="item"
:depth="0"
:collapsible="collapsible"
:link-component="linkComponent"
/>
</li>
<DocLink v-else :item="item" :collapsible="collapsible" />
<DocLink
v-else
:item="item"
:collapsible="collapsible"
:link-component="linkComponent"
/>
</template>
</ul>
</template>
42 changes: 40 additions & 2 deletions components/DocMenu/vue/DocMenuVue.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
/// <reference types="cypress" />
import { type DefineComponent, defineComponent } from 'vue'
import { mount } from 'cypress/vue'
import assertions from '../assertions'
import type { NavGroup, NavItemLink } from '../constants'
import DocMenuStory from './DocMenu.rootstory'
import DocMenu from './DocMenu.vue'

const CustomLinkVue: DefineComponent<{ href: StringConstructor }> =
defineComponent({
props: {
href: String,
},
setup(props, { slots }) {
return () => (
<div>
{slots.default?.()} + href: {props.href}
</div>
)
},
})

describe('<DocMenu/>', () => {
it('renders', () => {
mount(() => <DocMenu items={[{ href: '/foo', label: 'Foo' }]} />)
cy.get('ul').should('exist')
})

it('renders the custom link components', () => {
mount(() => (
<DocMenu
items={[
{
label: 'Baaaaaaz',
items: [{ label: 'Bar', items: [{ href: '/foo', label: 'Foo' }] }],
},
]}
linkComponent={CustomLinkVue}
/>
))
cy.findByText('Bar').click()
cy.findByText('Foo + href: /foo', { selector: 'div' }).should('be.visible')
})

function mountStory(items: (NavItemLink | NavGroup)[] = []) {
mount(() => <DocMenuStory items={items} />)
mount(() => {
return <DocMenu items={items} />
})
}
assertions(mountStory)
})
12 changes: 10 additions & 2 deletions components/DocMenu/vue/_DocGroup.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed, Ref, ref } from 'vue'
import { computed, type DefineComponent, Ref, ref } from 'vue'
import { IconChevronDownSmall } from '@cypress-design/vue-icon'
import { NavGroup, classes } from '@cypress-design/constants-docmenu'
import DocLink from './_DocLink.vue'
Expand All @@ -9,6 +9,7 @@ const props = withDefaults(
defineProps<{
group: NavGroup
collapsible: boolean
linkComponent: DefineComponent | 'a'
depth?: number
}>(),
{
Expand Down Expand Up @@ -120,9 +121,16 @@ const Head = computed(() =>
:group="item"
:depth="depth + 1"
:collapsible="props.collapsible"
:link-component="props.linkComponent"
/>
</li>
<DocLink v-else :item="item" :depth="depth" :collapsible="collapsible" />
<DocLink
v-else
:item="item"
:depth="depth"
:collapsible="collapsible"
:link-component="props.linkComponent"
/>
</template>
</ul>
</template>
9 changes: 6 additions & 3 deletions components/DocMenu/vue/_DocLink.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script lang="ts" setup>
import type { DefineComponent } from 'vue'
import type { NavItemLink } from '@cypress-design/constants-docmenu'
withDefaults(
defineProps<{
item: NavItemLink
depth?: number
collapsible: boolean
depth?: number
linkComponent: DefineComponent | 'a'
}>(),
{
depth: -1,
Expand All @@ -20,7 +22,8 @@ withDefaults(
'pl-[16px]': depth >= 0,
}"
>
<a
<component
:is="linkComponent"
class="group relative block w-full pl-[24px]"
:class="{
'text-indigo-500': item.active,
Expand All @@ -40,6 +43,6 @@ withDefaults(
}"
/>
{{ item.label }}
</a>
</component>
</li>
</template>

0 comments on commit 5459dde

Please sign in to comment.