|
1 | | -import { |
2 | | - defineComponent, |
3 | | - onBeforeMount, |
4 | | - onMounted, |
5 | | - onUpdated, |
6 | | - PropType, |
7 | | - provide, |
8 | | - reactive, |
9 | | - ref, |
10 | | - Slot |
11 | | -} from 'vue' |
12 | | -import './tabs.scss' |
| 1 | +import { defineComponent, onBeforeMount, onMounted, onUpdated, PropType, provide, reactive, ref, Slot } from 'vue'; |
| 2 | +import './tabs.scss'; |
13 | 3 |
|
14 | | -export type Active = string | number | null |
15 | | -export type TabsType = 'tabs' | 'pills' | 'options' | 'wrapped' | 'slider' |
| 4 | +export type Active = string | number | null; |
| 5 | +export type TabsType = 'tabs' | 'pills' | 'options' | 'wrapped' | 'slider'; |
16 | 6 | export interface Tabs { |
17 | | - state: TabsState |
| 7 | + state: TabsState; |
18 | 8 | } |
19 | 9 | interface TabsState { |
20 | | - data?: any[] |
21 | | - showContent: boolean |
22 | | - active: string |
23 | | - slots: Slot[] |
| 10 | + data?: any[]; |
| 11 | + showContent: boolean; |
| 12 | + active: string; |
| 13 | + slots: Slot[]; |
24 | 14 | } |
25 | 15 | export default defineComponent({ |
26 | 16 | name: 'DTabs', |
27 | 17 | props: { |
28 | 18 | modelValue: { |
29 | 19 | type: [String, Number], |
30 | | - default: null |
| 20 | + default: null, |
31 | 21 | }, |
32 | 22 |
|
33 | 23 | type: { |
34 | 24 | type: String as () => TabsType, |
35 | | - default: 'tabs' |
| 25 | + default: 'tabs', |
36 | 26 | }, |
37 | 27 | showContent: { |
38 | 28 | type: Boolean, |
39 | | - default: true |
| 29 | + default: true, |
40 | 30 | }, |
41 | 31 | vertical: { |
42 | 32 | type: Boolean, |
43 | | - default: false |
| 33 | + default: false, |
44 | 34 | }, |
45 | 35 | reactivable: { |
46 | 36 | type: Boolean, |
47 | | - default: true |
| 37 | + default: true, |
48 | 38 | }, |
49 | 39 | customWidth: { |
50 | 40 | type: String, |
51 | | - default: '' |
| 41 | + default: '', |
52 | 42 | }, |
53 | 43 | cssClass: { |
54 | 44 | type: String, |
55 | | - default: '' |
| 45 | + default: '', |
56 | 46 | }, |
57 | 47 | beforeChange: { |
58 | 48 | type: Function as PropType<(id: Active) => boolean>, |
59 | | - default: null |
60 | | - } |
| 49 | + default: null, |
| 50 | + }, |
61 | 51 | }, |
62 | 52 |
|
63 | | - emits: ['update:modelValue', 'activeTabChange'], |
| 53 | + emits: ['update:modelValue', 'active-tab-change'], |
64 | 54 | setup(props, { emit, slots }) { |
65 | | - const tabsEle = ref(null) |
66 | | - const data = reactive({ offsetLeft: 0, offsetWidth: 0, id: null }) |
| 55 | + const tabsEle = ref(null); |
| 56 | + const data = reactive({ offsetLeft: 0, offsetWidth: 0, id: null }); |
67 | 57 | const state: TabsState = reactive({ |
68 | 58 | data: [], |
69 | 59 | active: props.modelValue, |
70 | 60 | showContent: props.showContent, |
71 | | - slots: [] |
72 | | - }) |
| 61 | + slots: [], |
| 62 | + }); |
73 | 63 | provide<Tabs>('tabs', { |
74 | | - state |
75 | | - }) |
| 64 | + state, |
| 65 | + }); |
76 | 66 |
|
77 | 67 | const canChange = function (currentTab: Active) { |
78 | | - let changeResult = Promise.resolve(true) |
| 68 | + let changeResult = Promise.resolve(true); |
79 | 69 | if (typeof props.beforeChange === 'function') { |
80 | | - const result: any = props.beforeChange(currentTab) |
| 70 | + const result: any = props.beforeChange(currentTab); |
81 | 71 | if (typeof result !== 'undefined') { |
82 | 72 | if (result.then) { |
83 | | - changeResult = result |
| 73 | + changeResult = result; |
84 | 74 | } else { |
85 | | - console.log(result) |
86 | | - changeResult = Promise.resolve(result) |
| 75 | + changeResult = Promise.resolve(result); |
87 | 76 | } |
88 | 77 | } |
89 | 78 | } |
90 | 79 |
|
91 | | - return changeResult |
92 | | - } |
| 80 | + return changeResult; |
| 81 | + }; |
93 | 82 | const activeClick = function (item, tabEl?) { |
94 | 83 | if (!props.reactivable && props.modelValue === item.id) { |
95 | | - return |
| 84 | + return; |
96 | 85 | } |
97 | 86 | canChange(item.id).then((change) => { |
98 | 87 | if (!change) { |
99 | | - return |
| 88 | + return; |
100 | 89 | } |
101 | | - const tab = state.data.find((itemOption) => itemOption.id === item.id) |
| 90 | + const tab = state.data.find((itemOption) => itemOption.id === item.id); |
102 | 91 | if (tab && !tab.disabled) { |
103 | | - state.active = item.id |
104 | | - emit('update:modelValue', tab.id) |
| 92 | + state.active = item.id; |
| 93 | + emit('update:modelValue', tab.id); |
105 | 94 | if (props.type === 'slider' && tabEl && tabsEle) { |
106 | | - this.offsetLeft = |
107 | | - tabEl.getBoundingClientRect().left - |
108 | | - this.tabsEle.nativeElement.getBoundingClientRect().left |
109 | | - this.offsetWidth = tabEl.getBoundingClientRect().width |
| 95 | + this.offsetLeft = tabEl.getBoundingClientRect().left - this.tabsEle.nativeElement.getBoundingClientRect().left; |
| 96 | + this.offsetWidth = tabEl.getBoundingClientRect().width; |
110 | 97 | } |
111 | | - emit('activeTabChange', tab.id) |
| 98 | + emit('active-tab-change', tab.id); |
112 | 99 | } |
113 | | - }) |
114 | | - } |
115 | | - const ulClass: string[] = [props.type] |
116 | | - props.cssClass && ulClass.push(props.cssClass) |
117 | | - props.vertical && ulClass.push('devui-nav-stacked') |
| 100 | + }); |
| 101 | + }; |
| 102 | + const ulClass: string[] = [props.type]; |
| 103 | + props.cssClass && ulClass.push(props.cssClass); |
| 104 | + props.vertical && ulClass.push('devui-nav-stacked'); |
118 | 105 | onUpdated(() => { |
119 | 106 | if (props.type === 'slider') { |
120 | 107 | // 延时等待active样式切换至正确的tab |
121 | 108 | setTimeout(() => { |
122 | | - const tabEle = tabsEle.value.querySelector('#' + props.modelValue + '.active') |
| 109 | + const tabEle = tabsEle.value.querySelector('#' + props.modelValue + '.active'); |
123 | 110 | if (tabEle) { |
124 | | - data.offsetLeft = |
125 | | - tabEle.getBoundingClientRect().left - tabsEle.value.getBoundingClientRect().left |
126 | | - data.offsetWidth = tabEle.getBoundingClientRect().width |
| 111 | + data.offsetLeft = tabEle.getBoundingClientRect().left - tabsEle.value.getBoundingClientRect().left; |
| 112 | + data.offsetWidth = tabEle.getBoundingClientRect().width; |
127 | 113 | } |
128 | | - }) |
| 114 | + }); |
129 | 115 | } |
130 | | - }) |
| 116 | + }); |
131 | 117 | onBeforeMount(() => { |
132 | 118 | if (props.type !== 'slider' && props.modelValue === undefined && state.data.length > 0) { |
133 | | - activeClick(state.data[0]) |
| 119 | + activeClick(state.data[0]); |
134 | 120 | } |
135 | | - }) |
| 121 | + }); |
136 | 122 | onMounted(() => { |
137 | | - if ( |
138 | | - props.type === 'slider' && |
139 | | - props.modelValue === undefined && |
140 | | - state.data.length > 0 && |
141 | | - state.data[0] |
142 | | - ) { |
143 | | - activeClick(state.data[0].tabsEle.value.getElementById(state.data[0].tabId)) |
| 123 | + if (props.type === 'slider' && props.modelValue === undefined && state.data.length > 0 && state.data[0]) { |
| 124 | + activeClick(state.data[0].tabsEle.value.getElementById(state.data[0].tabId)); |
144 | 125 | } |
145 | | - }) |
| 126 | + }); |
146 | 127 | return () => { |
147 | 128 | return ( |
148 | 129 | <div> |
149 | | - <ul |
150 | | - ref={tabsEle} |
151 | | - role='tablist' |
152 | | - class={`devui-nav devui-nav-${ulClass.join(' ')}`} |
153 | | - id='devuiTabs11' |
154 | | - > |
| 130 | + <ul ref={tabsEle} role='tablist' class={`devui-nav devui-nav-${ulClass.join(' ')}`} id='devuiTabs11'> |
155 | 131 | {state.data.map((item, i) => { |
156 | 132 | return ( |
157 | 133 | <li |
158 | 134 | role='presentation' |
159 | 135 | onClick={() => { |
160 | | - activeClick(item) |
| 136 | + activeClick(item); |
161 | 137 | }} |
162 | | - class={ |
163 | | - (props.modelValue === (item.id || item.tabId) ? 'active' : '') + |
164 | | - ' ' + |
165 | | - (item.disabled ? 'disabled' : '') |
166 | | - } |
167 | | - id={item.id || item.tabId} |
168 | | - > |
169 | | - <a |
170 | | - role='tab' |
171 | | - data-toggle={item.id} |
172 | | - aria-expanded={props.modelValue === (item.id || item.tabId)} |
173 | | - > |
| 138 | + class={(props.modelValue === (item.id || item.tabId) ? 'active' : '') + ' ' + (item.disabled ? 'disabled' : '')} |
| 139 | + id={item.id || item.tabId}> |
| 140 | + <a role='tab' data-toggle={item.id} aria-expanded={props.modelValue === (item.id || item.tabId)}> |
174 | 141 | {state.slots[i] ? state.slots[i]() : <span>{item.title}</span>} |
175 | 142 | </a> |
176 | 143 | </li> |
177 | | - ) |
| 144 | + ); |
178 | 145 | })} |
179 | 146 | <div |
180 | 147 | class={`devui-nav-${props.type}-animation`} |
181 | 148 | style={{ |
182 | 149 | left: data.offsetLeft + 'px', |
183 | | - width: data.offsetWidth + 'px' |
184 | | - }} |
185 | | - ></div> |
| 150 | + width: data.offsetWidth + 'px', |
| 151 | + }}></div> |
186 | 152 | </ul> |
187 | 153 | {slots.default()} |
188 | 154 | </div> |
189 | | - ) |
190 | | - } |
191 | | - } |
192 | | -}) |
| 155 | + ); |
| 156 | + }; |
| 157 | + }, |
| 158 | +}); |
0 commit comments