-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from nethesis/components-fixes
Add NeTabs component and various fixes
- Loading branch information
Showing
10 changed files
with
216 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<!-- | ||
Copyright (C) 2024 Nethesis S.r.l. | ||
SPDX-License-Identifier: GPL-3.0-or-later | ||
--> | ||
|
||
<script setup lang="ts"> | ||
import { ref, onMounted, watch } from 'vue' | ||
|
||
export interface Tab { | ||
name: string | ||
label: string | ||
} | ||
|
||
export interface Props { | ||
tabs: Tab[] | ||
selected?: string | ||
srSelectTabLabel?: string | ||
srTabsLabel?: string | ||
} | ||
|
||
const props = withDefaults(defineProps<Props>(), { | ||
tabs: () => [], | ||
selected: '', | ||
srSelectTabLabel: 'Select a tab', | ||
srTabsLabel: 'Tabs' | ||
}) | ||
|
||
let currentTab = ref('') | ||
|
||
onMounted(() => { | ||
selectTabFromSelectedProp() | ||
}) | ||
|
||
watch(currentTab, () => { | ||
emit('selectTab', currentTab.value) | ||
}) | ||
|
||
watch( | ||
() => [props.selected], | ||
() => { | ||
selectTabFromSelectedProp() | ||
} | ||
) | ||
|
||
function selectTabFromSelectedProp() { | ||
let selectedTab = props.tabs[0] | ||
|
||
if (props.selected) { | ||
let selectedTabFound = props.tabs.find((tab: any) => tab.name === props.selected) | ||
|
||
if (selectedTabFound) { | ||
selectedTab = selectedTabFound | ||
} | ||
} | ||
currentTab.value = selectedTab.name | ||
} | ||
|
||
function selectTab(tabName: any) { | ||
currentTab.value = tabName | ||
} | ||
|
||
const emit = defineEmits(['selectTab']) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<!-- mobile tabs --> | ||
<div class="sm:hidden"> | ||
<label for="tabs_select" class="sr-only">{{ srSelectTabLabel }}</label> | ||
<select | ||
id="tabs_select" | ||
v-model="currentTab" | ||
name="tabs_select" | ||
class="block w-full rounded-md border-gray-300 bg-white py-2 pl-3 pr-10 text-base text-gray-700 focus:border-primary-500 focus:outline-none focus:ring-primary-500 dark:border-gray-700 dark:bg-gray-950 dark:text-gray-50 dark:focus:border-primary-500 dark:focus:ring-primary-500 sm:text-sm" | ||
> | ||
<option | ||
v-for="tab in props.tabs" | ||
:key="tab.name" | ||
:selected="currentTab === tab.name" | ||
:value="tab.name" | ||
> | ||
{{ tab.label }} | ||
</option> | ||
</select> | ||
</div> | ||
<!-- desktop tabs --> | ||
<div class="hidden sm:block"> | ||
<div class="border-b border-gray-200 dark:border-gray-700"> | ||
<nav class="-mb-px flex space-x-8" :aria-label="srTabsLabel"> | ||
<a | ||
v-for="tab in props.tabs" | ||
:key="tab.name" | ||
:class="[ | ||
currentTab === tab.name | ||
? 'border-primary-600 text-primary-700 dark:border-primary-400 dark:text-primary-500' | ||
: 'border-transparent text-gray-600 hover:border-gray-300 hover:text-gray-700 dark:text-gray-400 dark:hover:border-gray-700 dark:hover:text-gray-100', | ||
'cursor-pointer whitespace-nowrap border-b-2 px-1 py-4 text-sm font-medium' | ||
]" | ||
:aria-current="currentTab === tab.name ? 'page' : undefined" | ||
@click="selectTab(tab.name)" | ||
>{{ tab.label }}</a | ||
> | ||
</nav> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (C) 2024 Nethesis S.r.l. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import type { Meta, StoryObj } from '@storybook/vue3' | ||
|
||
import { NeTabs } from '../src/main' | ||
|
||
const meta = { | ||
title: 'Visual/NeTabs', | ||
component: NeTabs, | ||
argTypes: {}, | ||
args: { | ||
tabs: [ | ||
{ name: 'firstTab', label: 'First tab' }, | ||
{ name: 'secondTab', label: 'Second tab' }, | ||
{ name: 'thirdTab', label: 'Third tab' } | ||
], | ||
selected: 'secondTab' | ||
} // default values | ||
} satisfies Meta<typeof NeTabs> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
const template = '<NeTabs v-bind="args" />' | ||
|
||
export const Default: Story = { | ||
render: (args) => ({ | ||
components: { NeTabs }, | ||
setup() { | ||
return { args } | ||
}, | ||
template: template | ||
}), | ||
args: {} | ||
} |