Skip to content

Commit b5c0864

Browse files
committed
refactor: improve polymorphic components
1 parent c064719 commit b5c0864

File tree

106 files changed

+532
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+532
-348
lines changed

packages/coreui-vue/src/components/alert/CAlertHeading.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export const CAlertHeading = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'h4',
1212
},
1313
},
1414
setup(props, { slots }) {
1515
return () =>
1616
h(
17-
props.component,
17+
props.as,
1818
{
1919
class: 'alert-heading',
2020
},

packages/coreui-vue/src/components/alert/__tests__/CAlertHeading.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'h2',
15+
as: 'h2',
1616
},
1717
slots: {
1818
default: 'Default slot',

packages/coreui-vue/src/components/badge/CBadge.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const CBadge = defineComponent({
1414
/**
1515
* Component used for the root node. Either a string to use a HTML element or a component.
1616
*/
17-
component: {
17+
as: {
1818
type: String,
1919
default: 'span',
2020
},
@@ -56,7 +56,7 @@ const CBadge = defineComponent({
5656
setup(props, { slots }) {
5757
return () =>
5858
h(
59-
props.component,
59+
props.as,
6060
{
6161
class: [
6262
'badge',

packages/coreui-vue/src/components/button/CButton.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ export const CButton = defineComponent({
99
* Toggle the active state for the component.
1010
*/
1111
active: Boolean,
12-
/**
13-
* Sets the color context of the component to one of CoreUI’s themed colors.
14-
*
15-
* @values 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light'
16-
*/
17-
color: Color,
1812
/**
1913
* Component used for the root node. Either a string to use a HTML element or a component.
2014
*/
21-
component: {
15+
as: {
2216
type: String,
2317
default: 'button',
2418
},
19+
/**
20+
* Sets the color context of the component to one of CoreUI’s themed colors.
21+
*
22+
* @values 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light'
23+
*/
24+
color: Color,
2525
/**
2626
* Toggle the disabled state for the component.
2727
*/
@@ -79,7 +79,7 @@ export const CButton = defineComponent({
7979
'click',
8080
],
8181
setup(props, { emit, slots }) {
82-
const component = props.href ? 'a' : props.component
82+
const component = props.href ? 'a' : props.as
8383
const handleClick = (event: Event) => {
8484
if (props.disabled) {
8585
return

packages/coreui-vue/src/components/button/__tests__/CButton.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const defaultWrapper = mount(Component, {
1313
const customWrapper = mount(Component, {
1414
propsData: {
1515
active: true,
16+
as: 'div',
1617
color: 'warning',
17-
component: 'div',
1818
disabled: true,
1919
href: '/bazinga',
2020
shape: 'rounded-pill',
@@ -28,8 +28,8 @@ const customWrapper = mount(Component, {
2828

2929
const customWrapperTwo = mount(Component, {
3030
propsData: {
31+
as: 'a',
3132
color: 'warning',
32-
component: 'a',
3333
disabled: true,
3434
},
3535
slots: {

packages/coreui-vue/src/components/card/CCardHeader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const CCardHeader = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'div',
1212
},
1313
},
1414
setup(props, { slots }) {
15-
return () => h(props.component, { class: 'card-header' }, slots.default && slots.default())
15+
return () => h(props.as, { class: 'card-header' }, slots.default && slots.default())
1616
},
1717
})
1818

packages/coreui-vue/src/components/card/CCardImage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const CCardImage = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'img',
1212
},
@@ -25,7 +25,7 @@ const CCardImage = defineComponent({
2525
setup(props, { slots }) {
2626
return () =>
2727
h(
28-
props.component,
28+
props.as,
2929
{
3030
class: `card-img${props.orientation ? `-${props.orientation}` : ''}`,
3131
},

packages/coreui-vue/src/components/card/CCardSubtitle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const CCardSubtitle = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'h6',
1212
},
1313
},
1414
setup(props, { slots }) {
15-
return () => h(props.component, { class: 'card-subtitle' }, slots.default && slots.default())
15+
return () => h(props.as, { class: 'card-subtitle' }, slots.default && slots.default())
1616
},
1717
})
1818
export { CCardSubtitle }

packages/coreui-vue/src/components/card/CCardText.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const CCardText = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'p',
1212
},
1313
},
1414
setup(props, { slots }) {
15-
return () => h(props.component, { class: 'card-text' }, slots.default && slots.default())
15+
return () => h(props.as, { class: 'card-text' }, slots.default && slots.default())
1616
},
1717
})
1818

packages/coreui-vue/src/components/card/CCardTitle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const CCardTitle = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'h5',
1212
},
1313
},
1414
setup(props, { slots }) {
15-
return () => h(props.component, { class: 'card-title' }, slots.default && slots.default())
15+
return () => h(props.as, { class: 'card-title' }, slots.default && slots.default())
1616
},
1717
})
1818

packages/coreui-vue/src/components/card/__tests__/CCardHeader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'span',
15+
as: 'span',
1616
},
1717
slots: {
1818
default: 'Default slot',

packages/coreui-vue/src/components/card/__tests__/CCardImage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'a',
15+
as: 'a',
1616
orientation: 'bottom',
1717
},
1818
slots: {

packages/coreui-vue/src/components/card/__tests__/CCardSubtitle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'h4',
15+
as: 'h4',
1616
},
1717
slots: {
1818
default: 'Default slot',

packages/coreui-vue/src/components/card/__tests__/CCardText.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'h4',
15+
as: 'h4',
1616
},
1717
slots: {
1818
default: 'Default slot',

packages/coreui-vue/src/components/card/__tests__/CCardTitle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'h2',
15+
as: 'h2',
1616
},
1717
slots: {
1818
default: 'Default slot',

packages/coreui-vue/src/components/dropdown/CDropdownHeader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ const CDropdownHeader = defineComponent({
66
/**
77
* Component used for the root node. Either a string to use a HTML element or a component.
88
*/
9-
component: {
9+
as: {
1010
type: String,
1111
default: 'h6',
1212
},
1313
},
1414
setup(props, { slots }) {
1515
return () =>
1616
h(
17-
props.component,
17+
props.as,
1818
{
1919
class: 'dropdown-header',
2020
},

packages/coreui-vue/src/components/dropdown/CDropdownItem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const CDropdownItem = defineComponent({
1212
/**
1313
* Component used for the root node. Either a string to use a HTML element or a component.
1414
*/
15-
component: {
15+
as: {
1616
type: String,
1717
default: 'a',
1818
},
@@ -32,7 +32,7 @@ const CDropdownItem = defineComponent({
3232
{
3333
class: 'dropdown-item',
3434
active: props.active,
35-
component: props.component,
35+
as: props.as,
3636
disabled: props.disabled,
3737
href: props.href,
3838
},

packages/coreui-vue/src/components/dropdown/CDropdownMenu.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const CDropdownMenu = defineComponent({
1212
*
1313
* @values 'div', 'ul'
1414
*/
15-
component: {
15+
as: {
1616
type: String,
1717
default: 'div',
1818
},
@@ -34,7 +34,7 @@ const CDropdownMenu = defineComponent({
3434
{
3535
default: () =>
3636
h(
37-
props.component,
37+
props.as,
3838
{
3939
...attrs,
4040
class: [
@@ -49,7 +49,7 @@ const CDropdownMenu = defineComponent({
4949
...(dark && { 'data-coreui-theme': 'dark' }),
5050
ref: dropdownMenuRef,
5151
},
52-
props.component === 'ul'
52+
props.as === 'ul'
5353
? slots.default && slots.default().map((vnode) => h('li', {}, vnode))
5454
: slots.default && slots.default(),
5555
),

packages/coreui-vue/src/components/dropdown/CDropdownToggle.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import type { Triggers } from '../../types'
1818
const CDropdownToggle = defineComponent({
1919
name: 'CDropdownToggle',
2020
props: {
21+
/**
22+
* Component used for the root node. Either a string to use a HTML element or a component.
23+
*/
24+
as: {
25+
type: String,
26+
default: 'button',
27+
},
2128
/**
2229
* Sets the color context of the component to one of CoreUI’s themed colors.
2330
*
@@ -31,13 +38,6 @@ const CDropdownToggle = defineComponent({
3138
type: Boolean,
3239
default: true,
3340
},
34-
/**
35-
* Component used for the root node. Either a string to use a HTML element or a component.
36-
*/
37-
component: {
38-
type: String,
39-
default: 'button',
40-
},
4141
/**
4242
* Create a custom toggler which accepts any content.
4343
*/
@@ -162,35 +162,35 @@ const CDropdownToggle = defineComponent({
162162
}),
163163
)
164164
: dropdownVariant === 'nav-item' && props.navLink
165-
? h(
166-
'a',
167-
{
168-
href: '#',
169-
...togglerProps.value,
170-
role: 'button',
171-
ref: dropdownToggleRef,
172-
},
173-
{ default: () => slots.default && slots.default() },
174-
)
175-
: h(
176-
CButton,
177-
{
178-
...togglerProps.value,
179-
color: props.color,
180-
component: props.component,
181-
disabled: props.disabled,
182-
shape: props.shape,
183-
size: props.size,
184-
variant: props.variant,
185-
ref: (el) => {
186-
togglerRef.value = el
165+
? h(
166+
'a',
167+
{
168+
href: '#',
169+
...togglerProps.value,
170+
role: 'button',
171+
ref: dropdownToggleRef,
187172
},
188-
},
189-
() =>
190-
props.split
191-
? h('span', { class: 'visually-hidden' }, 'Toggle Dropdown')
192-
: slots.default && slots.default(),
193-
)
173+
{ default: () => slots.default && slots.default() },
174+
)
175+
: h(
176+
CButton,
177+
{
178+
...togglerProps.value,
179+
color: props.color,
180+
component: props.as,
181+
disabled: props.disabled,
182+
shape: props.shape,
183+
size: props.size,
184+
variant: props.variant,
185+
ref: (el) => {
186+
togglerRef.value = el
187+
},
188+
},
189+
() =>
190+
props.split
191+
? h('span', { class: 'visually-hidden' }, 'Toggle Dropdown')
192+
: slots.default && slots.default(),
193+
)
194194
},
195195
})
196196

packages/coreui-vue/src/components/dropdown/__tests__/CDropdownHeader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultWrapper = mount(Component, {
1212

1313
const customWrapper = mount(Component, {
1414
propsData: {
15-
component: 'h2',
15+
as: 'h2',
1616
},
1717
slots: {
1818
default: 'Default slot',

0 commit comments

Comments
 (0)