Skip to content

Instantly share code, notes, and snippets.

@onurozkan
Created May 30, 2018 12:51
Show Gist options
  • Select an option

  • Save onurozkan/5fdf6f6b6b3310b533512c0eb217b87d to your computer and use it in GitHub Desktop.

Select an option

Save onurozkan/5fdf6f6b6b3310b533512c0eb217b87d to your computer and use it in GitHub Desktop.
Micro rating component for vue.
<template>
<div class="component__rating">
<span :key="n" v-for="(n) in size">
<a href="#" @mouseover="highlight(n)" @mouseleave="highlight(0)" @click="select(n)" :class="{'rating-hover': (n <= hover), 'rating-selected': (n <= selected)}">
<i :class="setIcon(n)"></i>
</a>
</span>
</div>
</template>
<script>
export default {
name: 'Rating',
data () {
return {
hover: 0,
selected: 0
}
},
props: {
name: {type: String},
size: {type: Number, default: 5},
icon: {type: String, default: 'far fa-star'},
iconSelected: {type: String, default: 'fa fa-star'}
},
methods: {
setIcon (n) {
return (n <= this.selected) ? this.iconSelected : this.icon
},
highlight (value) {
this.$set(this.$data, 'hover', value)
},
select (value) {
this.$set(this.$data, 'selected', value)
this.$emit('select', {name: this.name, value: value})
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment