Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 7064e95

Browse files
committed
refactor(tabs): use aattrs api
1 parent 22d5e25 commit 7064e95

File tree

3 files changed

+83
-96
lines changed

3 files changed

+83
-96
lines changed

.storybook/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { configure, addDecorator, addParameters } from '@storybook/vue';
22
import Vue from 'vue'
33
import VueLive from 'vue-live'
4+
import Lorem from 'vue-lorem-ipsum'
45
import Chakra, { CThemeProvider, CColorModeProvider, CReset } from '../packages/chakra-ui-core/src'
56
import Canvas from './components/Canvas.vue'
67
import theme from '../packages/chakra-ui-core/src/lib/theme'
@@ -84,6 +85,8 @@ addDecorator(() => ({
8485
// For playground
8586
Vue.use(VueLive)
8687

88+
Vue.component('Lorem', Lorem)
89+
8790
function loadStories() {
8891
const req = require.context('../packages/chakra-ui-core/src', true, /\.stories\.(js|mdx)$/);
8992
req.keys().forEach(filename => req(filename));

packages/chakra-ui-core/src/CTabs/CTabs.js

Lines changed: 62 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CTabs/CTabs.js
88
*/
99

10-
import { baseProps } from '../config'
11-
import { useVariantColorWarning, isDef, useId, forwardProps, cleanChildren, cloneVNodeElement } from '../utils'
12-
import styleProps from '../config/props'
13-
14-
import CFlex from '../CFlex'
15-
import CBox from '../CBox'
16-
import CPseudoBox from '../CPseudoBox'
10+
import { useVariantColorWarning, isDef, useId, cleanChildren, cloneVNodeElement, createStyledAttrsMixin } from '../utils'
1711
import { useTabListStyle, useTabStyle } from './utils/tabs.styles'
1812

1913
/**
@@ -25,10 +19,8 @@ import { useTabListStyle, useTabStyle } from './utils/tabs.styles'
2519
* @see Docs https://vue.chakra-ui.com/tabs
2620
*/
2721
const CTabs = {
28-
name: 'CTabs',
29-
inject: ['$chakraTheme', '$chakraColorMode'],
22+
mixins: [createStyledAttrsMixin('CTabs')],
3023
props: {
31-
...baseProps,
3224
index: Number,
3325
defaultIndex: Number,
3426
isManual: Boolean,
@@ -87,12 +79,6 @@ const CTabs = {
8779
set: this.set
8880
}
8981
},
90-
theme () {
91-
return this.$chakraTheme()
92-
},
93-
colorMode () {
94-
return this.$chakraColorMode()
95-
},
9682
isControlled () {
9783
return isDef(this.index)
9884
},
@@ -180,11 +166,10 @@ const CTabs = {
180166
}
181167
},
182168
render (h) {
183-
return h(CBox, {
184-
props: forwardProps(this.$props),
185-
attrs: {
186-
'data-chakra-component': 'CTabs'
187-
}
169+
return h(this.as, {
170+
class: [this.className],
171+
attrs: this.computedAttrs,
172+
on: this.computedListeners
188173
}, this.$slots.default)
189174
}
190175
}
@@ -198,9 +183,8 @@ const CTabs = {
198183
* @see Docs https://vue.chakra-ui.com/tabs
199184
*/
200185
const CTabList = {
201-
name: 'CTabList',
202-
props: baseProps,
203-
inject: ['$TabContext', '$chakraTheme', '$chakraColorMode'],
186+
mixins: [createStyledAttrsMixin('CTabList')],
187+
inject: ['$TabContext'],
204188
data () {
205189
return {
206190
allNodes: {},
@@ -212,20 +196,17 @@ const CTabList = {
212196
context () {
213197
return this.$TabContext()
214198
},
215-
theme () {
216-
return this.$chakraTheme()
217-
},
218-
colorMode () {
219-
return this.$chakraColorMode()
220-
},
221-
tabListStyleProps () {
199+
componentStyles () {
222200
const { align, variant, orientation } = this.context
223-
return useTabListStyle({
224-
theme: this.theme,
225-
align,
226-
orientation,
227-
variant
228-
})
201+
return {
202+
display: 'flex',
203+
...useTabListStyle({
204+
theme: this.theme,
205+
align,
206+
orientation,
207+
variant
208+
})
209+
}
229210
},
230211
enabledSelectedIndex () {
231212
const { selectedIndex } = this.context
@@ -329,17 +310,15 @@ const CTabList = {
329310
.map((child, index) => (child.componentOptions.propsData.isDisabled === true ? null : index))
330311
.filter(index => index != null)
331312

332-
return h(CFlex, {
313+
return h(this.as, {
314+
class: [this.className],
333315
attrs: {
334316
role: 'tablist',
335317
'aria-orientation': orientation,
336-
'data-chakra-component': 'CTabList'
337-
},
338-
props: {
339-
...this.tabListStyleProps,
340-
...forwardProps()
318+
...this.computedAttrs
341319
},
342-
nativeOn: {
320+
on: {
321+
...this.computedListeners,
343322
keydown: this.handleKeyDown
344323
}
345324
}, clones)
@@ -355,46 +334,40 @@ const CTabList = {
355334
* @see Docs https://vue.chakra-ui.com/tabs
356335
*/
357336
const CTab = {
358-
name: 'CTab',
359-
inject: ['$chakraTheme', '$chakraColorMode', '$TabContext'],
337+
mixins: [createStyledAttrsMixin('CTab', true)],
338+
inject: ['$TabContext'],
360339
props: {
361-
...styleProps,
362340
isSelected: Boolean,
363341
isDisabled: Boolean,
364-
id: String
342+
id: String,
343+
as: {
344+
type: [String, Object],
345+
default: 'button'
346+
}
365347
},
366348
computed: {
367349
context () {
368350
return this.$TabContext()
369351
},
370-
tabStyleProps () {
352+
componentStyles () {
371353
const { color, isFitted, orientation, size, variant } = this.context
372-
const styles = useTabStyle({
373-
colorMode: this.colorMode,
374-
theme: this.theme,
375-
color,
376-
isFitted,
377-
orientation,
378-
size,
379-
variant
380-
})
381-
return styles
382-
},
383-
theme () {
384-
return this.$chakraTheme()
385-
},
386-
colorMode () {
387-
return this.$chakraColorMode()
354+
return {
355+
outline: 'none',
356+
...useTabStyle({
357+
colorMode: this.colorMode,
358+
theme: this.theme,
359+
color,
360+
isFitted,
361+
orientation,
362+
size,
363+
variant
364+
})
365+
}
388366
}
389367
},
390368
render (h) {
391-
return h(CPseudoBox, {
392-
props: {
393-
outline: 'none',
394-
as: 'button',
395-
...this.tabStyleProps,
396-
...forwardProps(this.$props)
397-
},
369+
return h(this.as, {
370+
class: [this.className],
398371
attrs: {
399372
role: 'tab',
400373
tabIndex: this.isSelected ? 0 : -1,
@@ -404,8 +377,9 @@ const CTab = {
404377
'aria-disabled': this.isDisabled,
405378
'aria-selected': this.isSelected,
406379
'aria-controls': `panel:${this.id}`,
407-
'data-chakra-component': 'CTab'
408-
}
380+
...this.computedAttrs
381+
},
382+
on: this.computedListeners
409383
}, this.$slots.default)
410384
}
411385
}
@@ -419,25 +393,25 @@ const CTab = {
419393
* @see Docs https://vue.chakra-ui.com/tabs
420394
*/
421395
const CTabPanel = {
422-
name: 'CTabPanel',
396+
mixins: [createStyledAttrsMixin('CTabPanel')],
423397
props: {
424-
...baseProps,
425398
isSelected: Boolean,
426399
selectedPanelNode: Object,
427400
id: String
428401
},
429402
render (h) {
430-
return h(CBox, {
431-
props: forwardProps(this.$props),
403+
return h(this.as, {
404+
class: [this.className],
432405
attrs: {
433406
role: 'tabpanel',
434407
tabIndex: -1,
435408
'aria-labelledby': `tab:${this.id}`,
436409
hidden: !this.isSelected,
437410
id: `panel:${this.id}`,
438411
outline: 0,
439-
'data-chakra-component': 'CTabPanel'
440-
}
412+
...this.computedAttrs
413+
},
414+
on: this.computedListeners
441415
}, this.$slots.default)
442416
}
443417
}
@@ -451,9 +425,8 @@ const CTabPanel = {
451425
* @see Docs https://vue.chakra-ui.com/tabs
452426
*/
453427
const CTabPanels = {
454-
name: 'CTabPanels',
428+
mixins: [createStyledAttrsMixin('CTabPanels')],
455429
inject: ['$TabContext'],
456-
props: baseProps,
457430
computed: {
458431
context () {
459432
return this.$TabContext()
@@ -470,20 +443,22 @@ const CTabPanels = {
470443
const validChildren = cleanChildren(this.$slots.default)
471444

472445
const clones = validChildren.map((child, index) => {
446+
const isSelected = isManual ? index === manualIndex : index === selectedIndex
473447
return cloneVNodeElement(child, {
474448
props: {
475-
isSelected: isManual ? index === manualIndex : index === selectedIndex,
449+
isSelected,
476450
id: `${id}-${index}`
477451
}
478452
}, h)
479453
})
480454

481-
return h(CBox, {
482-
props: forwardProps(this.$props),
455+
return h(this.as, {
456+
class: [this.className],
483457
attrs: {
484458
tabIndex: -1,
485-
'data-chakra-component': 'CTabPanels'
486-
}
459+
...this.computedAttrs
460+
},
461+
on: this.computedListeners
487462
}, clones)
488463
}
489464
}

packages/chakra-ui-core/src/CTabs/CTabs.stories.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ storiesOf('UI | Tabs', module)
55
.add('Basic Usage', () => ({
66
components: { CBox, CTabs, CTabList, CTab, CTabPanels, CTabPanel },
77
template: `
8-
<CBox w="600px">
8+
<CBox w="600px" h="500px">
99
<CTabs>
1010
<CTabList>
1111
<CTab>One</CTab>
@@ -14,14 +14,23 @@ storiesOf('UI | Tabs', module)
1414
</CTabList>
1515
1616
<CTabPanels>
17-
<CTabPanel>
18-
<p>one!</p>
19-
</CTabPanel>
20-
<CTabPanel>
21-
<p>two!</p>
22-
</CTabPanel>
23-
<CTabPanel>
24-
<p>three!</p>
17+
<CTabPanel p="4">
18+
<p v-chakra font-size="xl" font-weight="bold">
19+
<lorem add="2w" />
20+
</p>
21+
<lorem add="10s" />
22+
</CTabPanel>
23+
<CTabPanel p="4">
24+
<p v-chakra font-size="xl" font-weight="bold">
25+
<lorem add="3w" />
26+
</p>
27+
<lorem add="5s" />
28+
</CTabPanel>
29+
<CTabPanel p="4">
30+
<p v-chakra font-size="xl" font-weight="bold">
31+
<lorem add="4w" />
32+
</p>
33+
<lorem add="8s" />
2534
</CTabPanel>
2635
</CTabPanels>
2736
</CTabs>

0 commit comments

Comments
 (0)