|
| 1 | +import { Meta, Story, Canvas, ArgsTable } from "@storybook/addon-docs"; |
| 2 | +import { ref } from 'vue'; |
| 3 | +import { Card, Tabs, Badge, CardSection } from "@/polaris-vue"; |
| 4 | +import dedent from "ts-dedent"; |
| 5 | + |
| 6 | +<Meta |
| 7 | + title="Components / Navigation / Tabs" |
| 8 | + component={Tabs} |
| 9 | + argTypes={{ |
| 10 | + default: { |
| 11 | + table: { |
| 12 | + disable: true, |
| 13 | + }, |
| 14 | + }, |
| 15 | + selected: { |
| 16 | + control: { disable: true }, |
| 17 | + }, |
| 18 | + tabs: { |
| 19 | + table: { |
| 20 | + type: { summary: 'TabDescriptor[]' }, |
| 21 | + }, |
| 22 | + control: { disable: true }, |
| 23 | + }, |
| 24 | + select: { |
| 25 | + table: { type: { |
| 26 | + summary: '(selectedTabIndex: number) => void', |
| 27 | + }}, |
| 28 | + }, |
| 29 | + '#tab-id': { |
| 30 | + description: 'Slot to show content of the tab. This slot will override the `content` prop of tab.', |
| 31 | + table: { |
| 32 | + category: 'slots', |
| 33 | + type: { summary: null }, |
| 34 | + }, |
| 35 | + } |
| 36 | + }} |
| 37 | +/> |
| 38 | + |
| 39 | +# Tabs |
| 40 | + |
| 41 | +Use to alternate among related views within the same context. |
| 42 | + |
| 43 | +<br/> |
| 44 | + |
| 45 | +export const Template = (args) => ({ |
| 46 | + components: { Tabs, CardSection, Badge, Card }, |
| 47 | + setup() { |
| 48 | + const selectedTab = ref(0); |
| 49 | + const handleTabChange = (selectedTabIndex) => { |
| 50 | + selectedTab.value = selectedTabIndex; |
| 51 | + }; |
| 52 | + const tabs = [ |
| 53 | + { |
| 54 | + id: 'all-customers-1', |
| 55 | + content: 'All', |
| 56 | + accessibilityLabel: 'All customers', |
| 57 | + panelID: 'all-customers-content-1', |
| 58 | + }, |
| 59 | + { |
| 60 | + id: 'accepts-marketing-1', |
| 61 | + content: 'Accepts marketing', |
| 62 | + panelID: 'accepts-marketing-content-1', |
| 63 | + }, |
| 64 | + { |
| 65 | + id: 'repeat-customers-1', |
| 66 | + content: 'Repeat customers', |
| 67 | + panelID: 'repeat-customers-content-1', |
| 68 | + }, |
| 69 | + ] |
| 70 | + return { args, selectedTab, handleTabChange, tabs }; |
| 71 | + }, |
| 72 | + template: ` |
| 73 | + <Card> |
| 74 | + <Tabs :tabs="tabs" :selected="selectedTab" @select="handleTabChange" v-bind="args"> |
| 75 | + <template #all-customers-1> |
| 76 | + <span>All <Badge status="new">10+</Badge></span> |
| 77 | + </template> |
| 78 | + <CardSection :title="tabs[selectedTab].content"> |
| 79 | + <p v-if="selectedTab === 1">This is specific content of second tab</p> |
| 80 | + <p v-else>Tab {{ selectedTab }} selected</p> |
| 81 | + </CardSection> |
| 82 | + </Tabs> |
| 83 | + </Card> |
| 84 | + `, |
| 85 | +}); |
| 86 | + |
| 87 | +<Canvas> |
| 88 | + <Story |
| 89 | + name="Tabs" |
| 90 | + height="200px" |
| 91 | + args={{ |
| 92 | + fitted: false, |
| 93 | + }} |
| 94 | + parameters={{ |
| 95 | + docs: { |
| 96 | + source: { |
| 97 | + state: 'close', |
| 98 | + code: dedent` |
| 99 | + <Card> |
| 100 | + <Tabs :tabs="tabs" :selected="selectedTab" @select="handleTabChange"> |
| 101 | + <template #all-customers-1> |
| 102 | + <span>All <Badge status="new">10+</Badge></span> |
| 103 | + </template> |
| 104 | + <CardSection :title="tabs[selectedTab].content"> |
| 105 | + <p v-if="selectedTab === 1">This is specific content of second tab</p> |
| 106 | + <p v-else>Tab {{ selectedTab }} selected</p> |
| 107 | + </CardSection> |
| 108 | + </Tabs> |
| 109 | + </Card>\n |
| 110 | + <script setup> |
| 111 | + const selectedTab = ref(0); |
| 112 | + const handleTabChange = (selectedTabIndex) => { |
| 113 | + selectedTab.value = selectedTabIndex; |
| 114 | + }; |
| 115 | + const tabs = [ |
| 116 | + { |
| 117 | + id: 'all-customers-1', |
| 118 | + content: 'All', |
| 119 | + accessibilityLabel: 'All customers', |
| 120 | + panelID: 'all-customers-content-1', |
| 121 | + }, |
| 122 | + { |
| 123 | + id: 'accepts-marketing-1', |
| 124 | + content: 'Accepts marketing', |
| 125 | + panelID: 'accepts-marketing-content-1', |
| 126 | + }, |
| 127 | + { |
| 128 | + id: 'repeat-customers-1', |
| 129 | + content: 'Repeat customers', |
| 130 | + panelID: 'repeat-customers-content-1', |
| 131 | + }, |
| 132 | + ]; |
| 133 | + </script> |
| 134 | + `, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }} |
| 138 | + > |
| 139 | + {Template.bind({})} |
| 140 | + </Story> |
| 141 | +</Canvas> |
| 142 | + |
| 143 | +<ArgsTable story="Tabs" /> |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +<br/> |
| 148 | + |
| 149 | +### **TabDescriptor Props** |
| 150 | + |
| 151 | +```js |
| 152 | +/** A unique identifier for the tab */ |
| 153 | +id: string; |
| 154 | +/** A destination to link to */ |
| 155 | +url?: string; |
| 156 | +/** Content for the tab */ |
| 157 | +content: string; |
| 158 | +/** A unique identifier for the panel */ |
| 159 | +panelID?: string; |
| 160 | +/** Visually hidden text for screen readers */ |
| 161 | +accessibilityLabel?: string; |
| 162 | +``` |
0 commit comments