37 lines
904 B
TypeScript
37 lines
904 B
TypeScript
|
// stores/tabs.ts
|
||
|
import { defineStore } from 'pinia'
|
||
|
|
||
|
export interface TabItem {
|
||
|
key: string
|
||
|
label: string
|
||
|
to: string
|
||
|
componentName: string
|
||
|
}
|
||
|
|
||
|
export const useTabsStore = defineStore('tabs', {
|
||
|
state: () => ({
|
||
|
activeTab: '' as string,
|
||
|
tabs: [] as { key: string; label: string; to: string; componentName: string }[]
|
||
|
}),
|
||
|
actions: {
|
||
|
addTab(tab: TabItem) {
|
||
|
if (!this.tabs.find(t => t.key === tab.key)) {
|
||
|
this.tabs.push(tab)
|
||
|
}
|
||
|
this.activeTab = tab.key
|
||
|
},
|
||
|
removeTab(key: string) {
|
||
|
const idx = this.tabs.findIndex(t => t.key === key)
|
||
|
if (idx !== -1) {
|
||
|
this.tabs.splice(idx, 1)
|
||
|
if (this.activeTab === key && this.tabs.length) {
|
||
|
this.activeTab = this.tabs[Math.max(idx - 1, 0)].key
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
setActiveTab(key: string) {
|
||
|
this.activeTab = key
|
||
|
}
|
||
|
}
|
||
|
})
|