Skip to content

Instantly share code, notes, and snippets.

@cvega93
Created March 26, 2019 17:40
Show Gist options
  • Save cvega93/91102f8937d8a52d747a5b01d05bf5dd to your computer and use it in GitHub Desktop.
Save cvega93/91102f8937d8a52d747a5b01d05bf5dd to your computer and use it in GitHub Desktop.
Stars component for Vue.js
<template>
<span>
<slot name="ratio"></slot>
<i class="fa fa-star" aria-hidden="true" v-for="(i, index) in completeStarts()" :key="index"></i>
<i class="fa fa-star-half-o" aria-hidden="true" v-if="haveHalfStart()"></i>
<i class="fa fa-star-o" aria-hidden="true" v-for="(x, index) in incompleteStarts()" :key="index + 5"></i>
<slot></slot>
</span>
</template>
<script>
export default {
name: 'stars',
props: ['rating','color','spacing','size'],
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
methods: {
completeStarts: function () {
if (this.rating > 0) {
return Math.floor(this.rating)
}
return 0
},
haveHalfStart: function () {
return (this.rating % 1) > 0
},
incompleteStarts: function () {
if (this.rating > 0) {
return 5 - Math.ceil(this.rating)
}
return 5
},
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment