forked from jd-solanki/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rating): Add Rating component (jd-solanki#60)
Co-authored-by: JD Solanki <jdsolanki0001@gmail.com>
- Loading branch information
1 parent
2eb7394
commit 023f169
Showing
16 changed files
with
999 additions
and
835 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { computed, defineComponent, ref, toRef } from 'vue' | ||
import { useLayer, useProps as useLayerProps } from '@/composables/useLayer' | ||
import { disabled, readonly } from '@/composables/useProps' | ||
|
||
export const ARating = defineComponent({ | ||
name: 'ARating', | ||
props: { | ||
// color: { default: 'primary' }, | ||
...useLayerProps({ | ||
color: { | ||
default: 'warning', | ||
}, | ||
}), | ||
|
||
/** | ||
* Bind v-model value to rating | ||
*/ | ||
modelValue: { | ||
type: Number, | ||
default: undefined, | ||
}, | ||
|
||
/** | ||
* Sets amount of rating items | ||
*/ | ||
length: { | ||
type: [Number, String], | ||
default: 5, | ||
}, | ||
|
||
/** | ||
* Allows the award of half a point | ||
*/ | ||
halve: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
|
||
/** | ||
* Sets empty icon | ||
*/ | ||
emptyIcon: { | ||
type: String, | ||
default: 'i-bx:star', | ||
}, | ||
|
||
/** | ||
* Sets half-filled icon | ||
*/ | ||
halfIcon: { | ||
type: String, | ||
default: 'i-bx:bxs-star-half', | ||
}, | ||
|
||
/** | ||
* Sets filled icon | ||
*/ | ||
fullIcon: { | ||
type: String, | ||
default: 'i-bx:bxs-star', | ||
}, | ||
|
||
/** | ||
* Allows to see visual changes of value on hover | ||
*/ | ||
noHoverHint: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
|
||
/** | ||
* Animate icon on hover | ||
*/ | ||
animate: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
|
||
/** | ||
* Make rating component readonly | ||
*/ | ||
readonly, | ||
|
||
/** | ||
* Disable rating selection | ||
*/ | ||
disabled, | ||
}, | ||
emits: ['update:modelValue'], | ||
setup(props, { emit }) { | ||
const { getLayerClasses } = useLayer() | ||
|
||
const { styles, classes } = getLayerClasses( | ||
toRef(props, 'color'), | ||
ref(''), | ||
ref(false), | ||
) | ||
|
||
const rating = ref(0) | ||
const isHovered = ref(false) | ||
|
||
const visibleRating = computed(() => | ||
!props.noHoverHint | ||
&& !props.readonly | ||
&& !props.disabled | ||
&& isHovered.value | ||
? rating.value | ||
: props.modelValue ?? 0, | ||
) | ||
|
||
const items = computed(() => | ||
Array.from({ length: Number(props.length) }, (_, i) => i + 1).map(item => | ||
item <= visibleRating.value | ||
? props.fullIcon | ||
: item - visibleRating.value === 0.5 | ||
? props.halfIcon | ||
: props.emptyIcon, | ||
), | ||
) | ||
|
||
const handleClick = () => { | ||
emit('update:modelValue', rating.value) | ||
} | ||
|
||
const onMouseEnter = (e: MouseEvent, index: number) => { | ||
isHovered.value = true | ||
|
||
const { offsetX, target } = e | ||
if (target instanceof HTMLElement) { | ||
const widthPercentage = (offsetX * 100) / target.clientWidth | ||
props.halve | ||
? (rating.value = widthPercentage < 50 ? index + 0.5 : index + 1) | ||
: (rating.value = index + 1) | ||
} | ||
} | ||
|
||
const onMouseLeave = () => { | ||
isHovered.value = false | ||
} | ||
|
||
return () => ( | ||
<div | ||
class={[ | ||
'a-rating flex', | ||
(props.animate && !props.readonly && !props.disabled) && 'a-rating-animated', | ||
props.readonly && 'a-rating-readonly pointer-events-none', | ||
props.disabled && 'a-rating-disabled pointer-events-none', | ||
...classes.value, | ||
]} | ||
style={[...styles.value]} | ||
> | ||
|
||
{items.value.map((icon, i) => { | ||
return <i class={['cursor-pointer', icon]} | ||
onClick={handleClick} | ||
onMouseenter={(event => onMouseEnter(event, i))} | ||
onMouseleave={onMouseLeave}/> | ||
})} | ||
</div> | ||
) | ||
}, | ||
}) | ||
|
||
export type ARating = InstanceType<typeof ARating> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ARating } from './ARating' | ||
|
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
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
15 changes: 15 additions & 0 deletions
15
packages/documentation/docs/demos/rating/DemoRatingAnimate.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
animate | ||
empty-icon="i-bx:heart" | ||
full-icon="i-bx:bxs-heart" | ||
color="danger" | ||
/> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating v-model="rating" /> | ||
</template> |
12 changes: 12 additions & 0 deletions
12
packages/documentation/docs/demos/rating/DemoRatingColor.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
color="primary" | ||
/> | ||
</template> |
14 changes: 14 additions & 0 deletions
14
packages/documentation/docs/demos/rating/DemoRatingCustomIcon.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
empty-icon="i-bx:heart" | ||
full-icon="i-bx:bxs-heart" | ||
color="danger" | ||
/> | ||
</template> |
13 changes: 13 additions & 0 deletions
13
packages/documentation/docs/demos/rating/DemoRatingHalve.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
halve | ||
class="text-2xl" | ||
/> | ||
</template> |
14 changes: 14 additions & 0 deletions
14
packages/documentation/docs/demos/rating/DemoRatingLength.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col gap-2"> | ||
<ARating | ||
v-model="rating" | ||
length="3" | ||
/> | ||
</div> | ||
</template> |
13 changes: 13 additions & 0 deletions
13
packages/documentation/docs/demos/rating/DemoRatingNoHoverHint.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
halve | ||
no-hover-hint | ||
/> | ||
</template> |
27 changes: 27 additions & 0 deletions
27
packages/documentation/docs/demos/rating/DemoRatingSizing.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(0) | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col gap-2"> | ||
<ARating | ||
v-model="rating" | ||
class="text-sm" | ||
/> | ||
<ARating | ||
v-model="rating" | ||
class="text-base" | ||
/> | ||
<ARating | ||
v-model="rating" | ||
class="text-lg" | ||
/> | ||
<ARating v-model="rating" /> | ||
<ARating | ||
v-model="rating" | ||
class="text-2xl" | ||
/> | ||
</div> | ||
</template> |
16 changes: 16 additions & 0 deletions
16
packages/documentation/docs/demos/rating/DemoRatingStates.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const rating = ref(3) | ||
</script> | ||
|
||
<template> | ||
<ARating | ||
v-model="rating" | ||
readonly | ||
/> | ||
<ARating | ||
v-model="rating" | ||
disabled | ||
/> | ||
</template> |
Oops, something went wrong.