Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
feat(card): add primary action component (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
tychenjiajun authored and matsp committed Jun 17, 2019
1 parent 9cda7b6 commit 980e706
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 54 deletions.
27 changes: 1 addition & 26 deletions components/card/Card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
:class="classes"
class="mdc-card"
>
<div
v-if="$slots['actionableContent']"
ref="content"
:class="contentClasses"
tabindex="0"
>
<slot name="actionableContent" />
</div>
<slot />
<div
v-if="$slots['actionButtons'] || $slots['actionIcons'] || $slots['fullBleedButton']"
Expand Down Expand Up @@ -38,7 +30,6 @@
</template>

<script>
import { MDCRipple } from '@material/ripple'
import { baseComponentMixin, themeClassMixin } from '../base'
export default {
Expand All @@ -55,8 +46,7 @@ export default {
},
data () {
return {
slotObserver: undefined,
mdcRipple: undefined
slotObserver: undefined
}
},
computed: {
Expand All @@ -65,11 +55,6 @@ export default {
'mdc-card--outlined': this.outlined
}
},
contentClasses () {
return {
'mdc-card__primary-action': this.primaryAction
}
},
actionClasses () {
return {
'mdc-card__actions--full-bleed': this.fullBleedAction
Expand All @@ -79,26 +64,16 @@ export default {
return this.$slots.actionableContent != null
}
},
watch: {
primaryAction (value) {
value ? this.mdcRipple = MDCRipple.attachTo(this.$refs.content) : this.mdcRipple.destroy()
}
},
mounted () {
this.updateSlots()
this.slotObserver = new MutationObserver(() => this.updateSlots())
this.slotObserver.observe(this.$el, {
childList: true,
subtree: true
})
if (this.primaryAction) { this.mdcRipple = MDCRipple.attachTo(this.$refs.content) }
},
beforeDestroy () {
this.slotObserver.disconnect()
if (typeof this.mdcRipple !== 'undefined') {
this.mdcRipple.destroy()
}
},
methods: {
updateSlots () {
Expand Down
30 changes: 30 additions & 0 deletions components/card/CardPrimaryAction.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<div
class="mdc-card__primary-action"
tabindex="0"
>
<slot />
</div>
</template>

<script>
import { MDCRipple } from '@material/ripple'
export default {
name: 'CardPrimaryAction',
data () {
return {
mdcRipple: undefined
}
},
mounted () {
this.mdcRipple = MDCRipple.attachTo(this.$el)
},
beforeDestroy () {
if (this.mdcRipple) {
this.mdcRipple.destroyed()
}
}
}
</script>
27 changes: 15 additions & 12 deletions components/card/__test__/Card.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Card from '../Card.vue'
import CardMedia from '../CardMedia.vue'
import Button from '../../button/Button.vue'
import IconButton from '../../icon-button/IconButton.vue'
import CardPrimaryAction from '../CardPrimaryAction'

describe('Card Media', () => {
it('should mount', () => {
Expand Down Expand Up @@ -61,18 +62,6 @@ describe('Card', () => {
expect(wrapper.classes()).toContain('mdc-card--outlined')
})

it('should render as primary action', () => {
let wrapper = mount(Card, {
slots: {
actionableContent: 'content'
}
})
expect(wrapper).toMatchSnapshot()
let content = wrapper.find('.mdc-card__primary-action')
expect(content).toBeDefined()
expect(content.attributes().tabindex).toBe('0')
})

it('should render with full bleed action', () => {
let wrapper = mount(Card, {
propsData: {
Expand Down Expand Up @@ -117,3 +106,17 @@ describe('Card', () => {
})
})
})

describe('CardPrimaryAction', () => {
it('should mount', () => {
let wrapper = mount(CardPrimaryAction)
expect(wrapper.isVueInstance()).toBeTruthy()
expect(wrapper.vm.$data.mdcRipple).toBeDefined()
})

it('should render with no prop', () => {
let wrapper = mount(CardPrimaryAction)
expect(wrapper).toMatchSnapshot()
expect(wrapper.classes()).toContain('mdc-card__primary-action')
})
})
14 changes: 2 additions & 12 deletions components/card/__test__/__snapshots__/Card.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,11 @@ exports[`Card Media should render with no prop 1`] = `
exports[`Card should render as outlined 1`] = `
<div class="mdc-card mdc-card--outlined">
<!---->
<!---->
</div>
`;

exports[`Card should render as primary action 1`] = `
<div class="mdc-card">
<div tabindex="0" class="mdc-card__primary-action">content</div>
<!---->
</div>
`;

exports[`Card should render with action buttons 1`] = `
<div class="mdc-card">
<!---->
<div class="mdc-card__actions">
<!---->
<div class="mdc-card__action-buttons"><button class="mdc-button mdc-card__action mdc-card__action--button"> </button><button class="mdc-button mdc-card__action mdc-card__action--button"> </button></div>
Expand All @@ -45,7 +36,6 @@ exports[`Card should render with action buttons 1`] = `

exports[`Card should render with action icons 1`] = `
<div class="mdc-card">
<!---->
<div class="mdc-card__actions">
<!---->
<!---->
Expand All @@ -64,7 +54,6 @@ exports[`Card should render with action icons 1`] = `

exports[`Card should render with full bleed action 1`] = `
<div class="mdc-card">
<!---->
<div class="mdc-card__actions mdc-card__actions--full-bleed"><button class="mdc-button mdc-card__action mdc-card__action--button"> </button>
<!---->
<!---->
Expand All @@ -75,6 +64,7 @@ exports[`Card should render with full bleed action 1`] = `
exports[`Card should render with no prop 1`] = `
<div class="mdc-card">
<!---->
<!---->
</div>
`;

exports[`CardPrimaryAction should render with no prop 1`] = `<div tabindex="0" class="mdc-card__primary-action"></div>`;
2 changes: 2 additions & 0 deletions components/card/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Card from './Card.vue'
import CardMedia from './CardMedia.vue'
import CardPrimaryAction from './CardPrimaryAction'
import './styles.scss'

import { initPlugin } from '../'
Expand All @@ -8,6 +9,7 @@ const plugin = {
install (vm) {
vm.component('m-card', Card)
vm.component('m-card-media', CardMedia)
vm.component('m-card-primary-action', CardPrimaryAction)
}
}
export default plugin
Expand Down
4 changes: 2 additions & 2 deletions docs/.vuepress/components/CardDemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:outlined="radioProps[1].value"
:fullBleedAction="checkboxProps[0].value"
:primaryAction="checkboxProps[1].value">
<section class="card-section" slot="actionableContent">
<m-card-primary-action class="card-section">
<m-typo-headline :level="6">
Title
</m-typo-headline>
Expand All @@ -15,7 +15,7 @@
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</m-typo-body>
</section>
</m-card-primary-action>
<m-button slot="fullBleedButton">
full bleed
</m-button>
Expand Down
3 changes: 1 addition & 2 deletions docs/.vuepress/components/CardMediaDemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
:fullBleedAction="checkboxProps[2].value"
:primaryAction="checkboxProps[3].value">
<m-card-media
slot="media"
class="card-media card-media-content"
:square="checkboxProps[0].value"
:sixteenNine="checkboxProps[1].value">
Expand Down Expand Up @@ -55,7 +54,7 @@ export default {
.card-section {
display: flex;
flex-direction: column;
align-items: left;
align-items: start;
justify-content: flex-start;
padding: 8px 16px;
width: 300px;
Expand Down

0 comments on commit 980e706

Please sign in to comment.