Skip to content

Commit

Permalink
fix(kcard): add title tag prop (#2069)
Browse files Browse the repository at this point in the history
* fix(kcard): add title tag prop

* fix(kcard): title tag type

* docs(card): prop types
  • Loading branch information
portikM authored and adamdehaven committed Jun 15, 2024
1 parent f1012e2 commit 5ee7015
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
21 changes: 21 additions & 0 deletions docs/components/card.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ String to be used as a title. Can also be [slotted](#slots).
<KCard title="Card Title" />
```

### titleTag

HTML element you want title to be rendered as. Defaults to `div`.

Accepted values are:
* `div`
* `span`
* `a`
* `h1`
* `h2`
* `h3`
* `h4`
* `h5`
* `h6`

<KCard title-tag="h4" title="Card Title H4 Heading" />

```html
<KCard title-tag="h4" title="Card Title H4 Heading" />
```

## Slots

### default
Expand Down
8 changes: 8 additions & 0 deletions sandbox/pages/SandboxCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
Card content slot
</KCard>
</SandboxSectionComponent>
<SandboxSectionComponent title="titleTag">
<KCard
title="Card title"
title-tag="h2"
>
Card content slot
</KCard>
</SandboxSectionComponent>

<!-- Slots -->
<SandboxTitleComponent
Expand Down
4 changes: 3 additions & 1 deletion src/components/KCard/KCard.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ describe('KCard', () => {

it('renders title prop when passed', () => {
const titleProp = 'Title prop'
const titleTag = 'h5'

mount(KCard, {
props: {
title: titleProp,
titleTag,
},
})

cy.get('.k-card').find('.card-title').should('contain', titleProp)
cy.get('.k-card').find(`${titleTag}.card-title`).should('contain', titleProp)
})

it('renders slots when passed', () => {
Expand Down
15 changes: 12 additions & 3 deletions src/components/KCard/KCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
v-if="showCardHeader"
class="card-header"
>
<div
<component
:is="titleTag"
v-if="$slots.title || title"
class="card-title"
>
<slot name="title">
{{ title }}
</slot>
</div>
</component>
<div
v-if="$slots.actions"
class="card-actions"
Expand All @@ -35,13 +36,20 @@
</template>

<script setup lang="ts">
import { useSlots, computed } from 'vue'
import { useSlots, computed, type PropType } from 'vue'
import { HeaderTags } from '@/types'
import type { HeaderTag } from '@/types'
const props = defineProps({
title: {
type: String,
default: '',
},
titleTag: {
type: String as PropType<HeaderTag>,
default: 'div',
validator: (value: HeaderTag): boolean => HeaderTags.includes(value),
},
})
const slots = useSlots()
Expand Down Expand Up @@ -73,6 +81,7 @@ const showCardHeader = computed((): boolean => {
font-weight: var(--kui-font-weight-bold, $kui-font-weight-bold);
letter-spacing: var(--kui-letter-spacing-minus-30, $kui-letter-spacing-minus-30);
line-height: var(--kui-line-height-40, $kui-line-height-40);
margin: var(--kui-space-0, $kui-space-0);
}
.card-actions {
Expand Down
3 changes: 3 additions & 0 deletions src/types/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type HeaderTag = 'div' | 'span' | 'a' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'

export const HeaderTags: HeaderTag[] = ['div', 'span', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ export * from './alert'
export * from './badge'
export * from './breadcrumbs'
export * from './button'
export * from './card'
export * from './catalog'
export * from './code-block'
export * from './collapse'
export * from './date-time-picker'
export * from './dropdown'
export * from './empty-state'
export * from './multi-select'
export * from './table'
export * from './tabs'
Expand All @@ -27,4 +29,3 @@ export * from './toaster'
export * from './inline-edit'
export * from './text-area'
export * from './tree-list'
export * from './empty-state'

0 comments on commit 5ee7015

Please sign in to comment.