forked from element-plus/element-plus
/
tabs.tsx
226 lines (200 loc) Β· 5.78 KB
/
tabs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {
defineComponent,
nextTick,
provide,
reactive,
ref,
renderSlot,
watch,
} from 'vue'
import {
buildProps,
definePropType,
isNumber,
isString,
} from '@element-plus/utils'
import { EVENT_CODE, UPDATE_MODEL_EVENT } from '@element-plus/constants'
import ElIcon from '@element-plus/components/icon'
import { Plus } from '@element-plus/icons-vue'
import { tabsRootContextKey } from '@element-plus/tokens'
import { useNamespace } from '@element-plus/hooks'
import TabNav from './tab-nav'
import type { TabNavInstance } from './tab-nav'
import type { TabsPaneContext } from '@element-plus/tokens'
import type { ExtractPropTypes } from 'vue'
import type { Awaitable } from '@element-plus/utils'
export type TabPanelName = string | number
export const tabsProps = buildProps({
type: {
type: String,
values: ['card', 'border-card', ''],
default: '',
},
activeName: {
type: [String, Number],
default: '',
},
closable: Boolean,
addable: Boolean,
modelValue: {
type: [String, Number],
default: '',
},
editable: Boolean,
tabPosition: {
type: String,
values: ['top', 'right', 'bottom', 'left'],
default: 'top',
},
beforeLeave: {
type: definePropType<
(
newName: TabPanelName,
oldName: TabPanelName
) => Awaitable<void | boolean>
>(Function),
default: () => true,
},
stretch: Boolean,
} as const)
export type TabsProps = ExtractPropTypes<typeof tabsProps>
const isPanelName = (value: unknown): value is string | number =>
isString(value) || isNumber(value)
export const tabsEmits = {
[UPDATE_MODEL_EVENT]: (name: TabPanelName) => isPanelName(name),
'tab-click': (pane: TabsPaneContext, ev: Event) => ev instanceof Event,
'tab-change': (name: TabPanelName) => isPanelName(name),
edit: (paneName: TabPanelName | undefined, action: 'remove' | 'add') =>
['remove', 'add'].includes(action),
'tab-remove': (name: TabPanelName) => isPanelName(name),
'tab-add': () => true,
}
export type TabsEmits = typeof tabsEmits
export default defineComponent({
name: 'ElTabs',
props: tabsProps,
emits: tabsEmits,
setup(props, { emit, slots, expose }) {
const ns = useNamespace('tabs')
const nav$ = ref<TabNavInstance>()
const panes = reactive<Record<number, TabsPaneContext>>({})
const currentName = ref(props.modelValue || props.activeName || '0')
const changeCurrentName = (value: TabPanelName) => {
currentName.value = value
emit(UPDATE_MODEL_EVENT, value)
emit('tab-change', value)
}
const setCurrentName = async (value: TabPanelName) => {
// should do nothing.
if (currentName.value === value) return
try {
const canLeave = await props.beforeLeave?.(value, currentName.value)
if (canLeave !== false) {
changeCurrentName(value)
// call exposed function, Vue doesn't support expose in typescript yet.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
nav$.value?.removeFocus?.()
}
} catch {}
}
const handleTabClick = (
tab: TabsPaneContext,
tabName: TabPanelName,
event: Event
) => {
if (tab.props.disabled) return
setCurrentName(tabName)
emit('tab-click', tab, event)
}
const handleTabRemove = (pane: TabsPaneContext, ev: Event) => {
if (pane.props.disabled) return
ev.stopPropagation()
emit('edit', pane.props.name, 'remove')
emit('tab-remove', pane.props.name)
}
const handleTabAdd = () => {
emit('edit', undefined, 'add')
emit('tab-add')
}
watch(
() => props.activeName,
(modelValue) => setCurrentName(modelValue)
)
watch(
() => props.modelValue,
(modelValue) => setCurrentName(modelValue)
)
watch(currentName, async () => {
await nextTick()
// call exposed function, Vue doesn't support expose in typescript yet.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
nav$.value?.scrollToActiveTab()
})
{
const registerPane = (pane: TabsPaneContext) => (panes[pane.uid] = pane)
const unregisterPane = (uid: number) => delete panes[uid]
provide(tabsRootContextKey, {
props,
currentName,
registerPane,
unregisterPane,
})
}
expose({
currentName,
})
return () => {
const newButton =
props.editable || props.addable ? (
<span
class={ns.e('new-tab')}
tabindex="0"
onClick={handleTabAdd}
onKeydown={(ev: KeyboardEvent) => {
if (ev.code === EVENT_CODE.enter) handleTabAdd()
}}
>
<ElIcon class={ns.is('icon-plus')}>
<Plus />
</ElIcon>
</span>
) : null
const header = (
<div class={[ns.e('header'), ns.is(props.tabPosition)]}>
{newButton}
<TabNav
ref={nav$}
currentName={currentName.value}
editable={props.editable}
type={props.type}
panes={Object.values(panes)}
stretch={props.stretch}
onTabClick={handleTabClick}
onTabRemove={handleTabRemove}
/>
</div>
)
const panels = (
<div class={ns.e('content')}>{renderSlot(slots, 'default')}</div>
)
return (
<div
class={[
ns.b(),
ns.m(props.tabPosition),
{
[ns.m('card')]: props.type === 'card',
[ns.m('border-card')]: props.type === 'border-card',
},
]}
>
{...props.tabPosition !== 'bottom'
? [header, panels]
: [panels, header]}
</div>
)
}
},
})