@volverjs/ui-vue 0.0.6-beta.3 → 0.0.6-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,22 +4,32 @@
4
4
 
5
5
  const props = defineProps(VvNavProps)
6
6
  const emit = defineEmits(VvNavEvents)
7
- const { modifiers } = toRefs(props)
7
+ const { modifiers, items } = toRefs(props)
8
8
  const activeItem: Ref<string | null> = ref(null)
9
9
 
10
10
  // bem css classes
11
11
  const bemCssClasses = useModifiers('vv-nav', modifiers)
12
12
 
13
+ const localItems = computed(() => {
14
+ return items.value.map((item, index) => {
15
+ return {
16
+ ...item,
17
+ id: item.id || `nav-item_${index}`,
18
+ }
19
+ })
20
+ })
21
+
13
22
  /**
14
23
  * Triggers when the item is clicked.
15
24
  * @private
16
25
  * @event click
17
- * @param [NavItem, number] item, id - the clicked item
26
+ * @param [NavItem, string] item - the clicked item
18
27
  */
19
- function onClick(item: NavItem, id: string) {
28
+ function onClick(item: NavItem) {
20
29
  if (!item.disabled) {
21
30
  emit('click', item)
22
- activeItem.value = id
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
+ activeItem.value = item.id!
23
33
  }
24
34
  }
25
35
  </script>
@@ -28,8 +38,8 @@
28
38
  <nav :class="bemCssClasses">
29
39
  <ul class="vv-nav__menu" role="menu" aria-busy="true">
30
40
  <li
31
- v-for="(navItem, index) in items"
32
- :key="`nav-item_${index}`"
41
+ v-for="navItem in localItems"
42
+ :key="navItem.id"
33
43
  class="vv-nav__item"
34
44
  role="presentation"
35
45
  >
@@ -41,12 +51,12 @@
41
51
  tabindex: 0,
42
52
  }"
43
53
  :class="{
44
- current: activeItem == `nav-item_${index}`,
54
+ current: activeItem == navItem.id,
45
55
  disabled: navItem.disabled,
46
56
  }"
47
57
  class="vv-nav__item-label"
48
58
  v-on="navItem.on || {}"
49
- @click="onClick(navItem, `nav-item_${index}`)"
59
+ @click="onClick(navItem)"
50
60
  >
51
61
  {{ navItem.title }}
52
62
  </VvAction>
@@ -1,6 +1,7 @@
1
1
  import { ModifiersProps } from '@/props'
2
2
 
3
3
  export type NavItem = {
4
+ id?: string
4
5
  title: string
5
6
  to?: string | { [key: string]: unknown }
6
7
  href?: string
@@ -0,0 +1,53 @@
1
+ <script setup lang="ts">
2
+ import { VvTabProps, VvTabEvents } from '@/components/VvTab'
3
+ import type { NavItem } from '@/components/VvNav'
4
+ import VvNav from '@/components/VvNav/VvNav.vue'
5
+
6
+ const props = defineProps(VvTabProps)
7
+ const emit = defineEmits(VvTabEvents)
8
+ const { modifiers, items } = toRefs(props)
9
+ const activeTab: Ref<string | null> = ref(null)
10
+
11
+ // bem css classes
12
+ const bemCssClasses = useModifiers('vv-tab', modifiers)
13
+
14
+ const localItems = computed(() => {
15
+ return items.value.map((item, index) => {
16
+ return {
17
+ ...item,
18
+ id: item.id || `tab-item_${index}`,
19
+ }
20
+ })
21
+ })
22
+
23
+ /**
24
+ * Triggers when the item is clicked.
25
+ * @private
26
+ * @event click
27
+ * @param [NavItem, string] item - the clicked item
28
+ */
29
+ function onClick(item: NavItem) {
30
+ if (!item.disabled) {
31
+ emit('click', item)
32
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
33
+ activeTab.value = item.id!
34
+ }
35
+ }
36
+ </script>
37
+
38
+ <template>
39
+ <div :class="bemCssClasses">
40
+ <VvNav :items="localItems" modifiers="tabs full" @click="onClick" />
41
+ <!-- #region tab content -->
42
+ <article
43
+ v-for="item in localItems"
44
+ :id="item.id"
45
+ :key="item.id"
46
+ :class="{ target: activeTab === item.id }"
47
+ class="vv-tab__panel"
48
+ >
49
+ <slot :name="`${item.id}`" />
50
+ </article>
51
+ <!-- #endregion tab content -->
52
+ </div>
53
+ </template>
@@ -0,0 +1,13 @@
1
+ import { ModifiersProps } from '@/props'
2
+ import type { NavItem } from '../VvNav'
3
+
4
+ export const VvTabProps = {
5
+ ...ModifiersProps,
6
+ items: {
7
+ type: Array<NavItem>,
8
+ required: true,
9
+ default: () => [],
10
+ },
11
+ }
12
+
13
+ export const VvTabEvents = ['click']
@@ -0,0 +1,41 @@
1
+ import { VvTabProps } from '@/components/VvTab'
2
+
3
+ export const defaultArgs = {
4
+ ...propsToObject(VvTabProps),
5
+ items: [
6
+ {
7
+ title: 'Item 1',
8
+ href: 'javascript:void(0)',
9
+ },
10
+ {
11
+ title: 'Item 2',
12
+ to: 'about',
13
+ },
14
+ {
15
+ id: 'tab-3',
16
+ title: 'Item 3',
17
+ to: 'contacts',
18
+ on: {
19
+ click: () => {
20
+ // eslint-disable-next-line no-console
21
+ console.log('clicked Item 3')
22
+ },
23
+ },
24
+ },
25
+ ],
26
+ }
27
+
28
+ export const defaultArgTypes = {
29
+ tabId: {
30
+ description: 'Slot by tab-id',
31
+ control: {
32
+ type: 'text',
33
+ },
34
+ table: {
35
+ category: 'Slots',
36
+ type: {
37
+ summary: 'html',
38
+ },
39
+ },
40
+ },
41
+ }
@@ -0,0 +1,65 @@
1
+ import { Meta, Story, Canvas } from '@storybook/addon-docs'
2
+ import VvTab from '@/components/VvTab/VvTab.vue'
3
+ import { defaultArgs, defaultArgTypes } from './Tab.settings'
4
+ import { defaultTest } from './Tab.test'
5
+
6
+ <Meta
7
+ title="Components/Tab"
8
+ component={VvTab}
9
+ args={defaultArgs}
10
+ argTypes={defaultArgTypes}
11
+ />
12
+
13
+ export const Template = (args) => ({
14
+ components: { VvTab },
15
+ setup() {
16
+ return { args }
17
+ },
18
+ template: /* html */ `
19
+ <div class="m-md w-1/2">
20
+ <vv-tab v-bind="args" data-testId="element">
21
+ <template #tab-item_0>
22
+ <span class="text-20 font-semibold">Tab 1</span>
23
+ <p>
24
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
25
+ vitae elit libero, a pharetra augue. Nullam id dolor id nibh
26
+ ultricies vehicula ut id elit. Morbi leo risus, porta ac
27
+ consectetur ac, vestibulum at eros. Praesent commodo cursus
28
+ magna, vel scelerisque nisl consectetur et. Donec sed odio dui.
29
+ Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae
30
+ elit libero, a pharetra augue.
31
+ </p>
32
+ </template>
33
+ <template #tab-item_1>
34
+ <span class="text-20 font-semibold">Tab 2</span>
35
+ <p>
36
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
37
+ vitae elit libero, a pharetra augue. Nullam id dolor id nibh
38
+ ultricies vehicula ut id elit. Morbi leo risus, porta ac
39
+ consectetur ac, vestibulum at eros. Praesent commodo cursus
40
+ magna, vel scelerisque nisl consectetur et. Donec sed odio dui.
41
+ Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae
42
+ elit libero, a pharetra augue.
43
+ </p>
44
+ </template>
45
+ <template #tab-3>
46
+ <span class="text-20 font-semibold">Tab 3</span>
47
+ <p>
48
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
49
+ vitae elit libero, a pharetra augue. Nullam id dolor id nibh
50
+ ultricies vehicula ut id elit. Morbi leo risus, porta ac
51
+ consectetur ac, vestibulum at eros. Praesent commodo cursus
52
+ magna, vel scelerisque nisl consectetur et. Donec sed odio dui.
53
+ Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae
54
+ elit libero, a pharetra augue.
55
+ </p>
56
+ </template>
57
+ </vv-tab>
58
+ </div>`,
59
+ })
60
+
61
+ <Canvas>
62
+ <Story name="Default" play={defaultTest}>
63
+ {Template.bind()}
64
+ </Story>
65
+ </Canvas>
@@ -0,0 +1,37 @@
1
+ import type { PlayAttributes } from '@/test/types'
2
+ import { expect } from '@/test/expect'
3
+ import { within, userEvent } from '@storybook/testing-library'
4
+
5
+ export async function defaultTest({ canvasElement, args }: PlayAttributes) {
6
+ const element = (await within(canvasElement).findByTestId(
7
+ 'element',
8
+ )) as HTMLElement
9
+
10
+ if (!args.items || !args.items?.length) {
11
+ throw new Error('No items passed')
12
+ }
13
+
14
+ // check children number the same of items prop
15
+ const childrenEls = element.getElementsByClassName('vv-nav')[0]
16
+ expect(childrenEls?.children[0].children.length).toEqual(args.items?.length)
17
+
18
+ // take firse and second elements
19
+ const firstEl = await element.getElementsByTagName('a')?.[0]
20
+ const secondEl = await element.getElementsByTagName('router-link')?.[1]
21
+ // check they have not current class
22
+ const secondElHasCurrentClass = secondEl.classList.contains('current')
23
+ const firstElHasCurrentClass = firstEl.classList.contains('current')
24
+
25
+ await expect(firstElHasCurrentClass).toBe(false)
26
+ await expect(secondElHasCurrentClass).toBe(false)
27
+ // click first item and check "current" css class
28
+ await userEvent.click(firstEl)
29
+ await expect(firstEl).toHaveClass('current')
30
+
31
+ // check tab content to include "Tab 1"
32
+ const tabPanelEl = element.getElementsByClassName('vv-tab__panel')?.[0]
33
+ await expect(tabPanelEl.innerHTML.includes('Tab 1')).toBe(true)
34
+
35
+ // check accessibility
36
+ await expect(element).toHaveNoViolations()
37
+ }