Last active
April 15, 2020 09:00
-
-
Save webdesignberlin/f5bd8debdb40c9cc0d5bcf0d952f8525 to your computer and use it in GitHub Desktop.
generate BEModifier for vue computed property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const modifierClasses = (vm) => ((root) => { | |
if (vm.modifier) { | |
const classArray = (typeof vm.modifier === 'string') | |
? new Array(vm.modifier) | |
: [...vm.modifier]; | |
return classArray.map((modifier) => `${root}--${modifier}`); | |
} | |
return null; | |
}); | |
export const modifier = (acceptedValues = []) => ({ | |
type: [String, Array], | |
default: null, | |
validator: (value) => { | |
if (typeof value === 'string') { | |
return acceptedValues.includes(value); | |
} | |
const checkArray = []; | |
value.forEach((item) => { | |
checkArray.push(acceptedValues.includes(item)); | |
}); | |
return checkArray.every((item) => item); | |
}, | |
}); | |
export default modifierClasses; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<section | |
:class="modifierClasses('teaser')" | |
class="teaser"> | |
</section> | |
</template> | |
<script> | |
import { modifierClasses, modifier } from '@/helpers/modifierClasses'; | |
export default { | |
name: 'ImageTeaser', | |
props: { | |
modifier: modifier(['small-left', 'small-right']), | |
}, | |
computed: { | |
modifierClasses, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment