Skip to content

Commit

Permalink
refactor: update shared props types handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrholek committed Sep 15, 2024
1 parent e579fd7 commit 2f96ef2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
11 changes: 10 additions & 1 deletion packages/coreui-vue/src/components/form/CFormControlWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { CFormFloating } from './CFormFloating'
import { CFormLabel } from './CFormLabel'
import { CFormText } from './CFormText'

type CFormControlValidationProps = InstanceType<typeof CFormControlValidation>['$props']

interface CFormControlWrapperProps {
floatingLabel?: string
id?: string
label?: string
text?: string
}

const CFormControlWrapper = defineComponent({
name: 'CFormControlWrapper',
inheritAttrs: false,
Expand Down Expand Up @@ -32,7 +41,7 @@ const CFormControlWrapper = defineComponent({
*/
text: String,
},
setup(props, { slots }) {
setup(props: CFormControlWrapperProps & CFormControlValidationProps, { slots }) {
const formControlValidation = () =>
h(
CFormControlValidation,
Expand Down
4 changes: 3 additions & 1 deletion packages/coreui-vue/src/components/form/CFormInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ const CFormInput = defineComponent({
h(
CFormControlWrapper,
{
describedby: attrs['aria-describedby'],
...(typeof attrs['aria-describedby'] === 'string' && {
describedby: attrs['aria-describedby'],
}),
feedback: props.feedback,
feedbackInvalid: props.feedbackInvalid,
feedbackValid: props.feedbackValid,
Expand Down
4 changes: 3 additions & 1 deletion packages/coreui-vue/src/components/form/CFormSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ const CFormSelect = defineComponent({
h(
CFormControlWrapper,
{
describedby: attrs['aria-describedby'],
...(typeof attrs['aria-describedby'] === 'string' && {
describedby: attrs['aria-describedby'],
}),
feedback: props.feedback,
feedbackInvalid: props.feedbackInvalid,
feedbackValid: props.feedbackValid,
Expand Down
4 changes: 3 additions & 1 deletion packages/coreui-vue/src/components/form/CFormTextarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ const CFormTextarea = defineComponent({
h(
CFormControlWrapper,
{
describedby: attrs['aria-describedby'],
...(typeof attrs['aria-describedby'] === 'string' && {
describedby: attrs['aria-describedby'],
}),
feedback: props.feedback,
feedbackInvalid: props.feedbackInvalid,
feedbackValid: props.feedbackValid,
Expand Down
9 changes: 7 additions & 2 deletions packages/coreui-vue/src/components/nav/CNavItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { defineComponent, h } from 'vue'

import { CNavLink } from './CNavLink'

type CNavLinkProps = Omit<InstanceType<typeof CNavLink>['$props'], 'as'>

interface CNavItemProps {
as: string
}

const CNavItem = defineComponent({
name: 'CNavItem',
props: {
Expand All @@ -14,12 +20,11 @@ const CNavItem = defineComponent({
default: 'li',
},
},
setup(props, { slots }) {
setup(props: CNavLinkProps & CNavItemProps, { slots }) {
return () =>
h(
props.as,
{
as: props.component,
class: 'nav-item',
},
props.href
Expand Down
8 changes: 7 additions & 1 deletion packages/coreui-vue/src/components/toast/CToastClose.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { defineComponent, h, inject } from 'vue'
import { CCloseButton } from '../close-button/CCloseButton'

type CCloseButtonProps = InstanceType<typeof CCloseButton>['$props']

interface CToastCloseProps {
as: string
}

const CToastClose = defineComponent({
name: 'CToastClose',
props: {
Expand All @@ -16,7 +22,7 @@ const CToastClose = defineComponent({
*/
'close',
],
setup(props, { slots, emit }) {
setup(props: CToastCloseProps & CCloseButtonProps, { slots, emit }) {
// eslint-disable-next-line no-unused-vars
const updateVisible = inject('updateVisible') as (visible: boolean) => void
const handleClose = () => {
Expand Down
13 changes: 2 additions & 11 deletions packages/coreui-vue/src/components/toast/CToastHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ const CToastHeader = defineComponent({
*/
closeButton: Boolean,
},
emits: [
/**
* Event called after clicking the close button.
*/
'close',
],
setup(props, { slots, emit }) {
setup(props, { slots }) {
return () =>
h('div', { class: 'toast-header' }, [
slots.default && slots.default(),
props.closeButton &&
h(CToastClose, {
onClose: () => emit('close'),
}),
props.closeButton && h(CToastClose),
])
},
})
Expand Down

0 comments on commit 2f96ef2

Please sign in to comment.