|
1 | 1 | <script lang="ts"> |
2 | 2 | import Star from "./Star.svelte"; |
3 | | - import { idGenerator } from "$lib/uiHelpers.svelte"; |
4 | 3 | import { type RatingProps as Props, rating as ratingVariants } from "."; |
5 | 4 |
|
6 | | - let { children, text, divClass, size = 24, total = 5, rating = 4, partialId = "partialStar" + idGenerator(), Icon = Star, count = false, pClass }: Props = $props(); |
| 5 | + let { children, text, divClass, size = 24, total = 5, rating = 4, Icon = Star, count = false, pClass }: Props = $props(); |
7 | 6 |
|
8 | 7 | const { base, p } = $derived(ratingVariants()); |
9 | | -
|
10 | | - // generate unique id for full star and gray star |
11 | | - const fullStarId: string = idGenerator(); |
12 | | - const grayStarId: string = idGenerator(); |
| 8 | + const ratingGroupId = crypto.randomUUID(); |
13 | 9 | let fullStars: number = Math.floor(rating); |
14 | 10 | let rateDiffence = rating - fullStars; |
15 | 11 | let percentRating = Math.round(rateDiffence * 100); |
|
18 | 14 |
|
19 | 15 | <div class={base({ class: divClass })}> |
20 | 16 | {#if count && children} |
21 | | - <Icon fillPercent={100} {size} /> |
| 17 | + <Icon fillPercent={100} {size} starIndex={0} groupId={ratingGroupId}/> |
22 | 18 | <p class={p({ class: pClass })}>{rating}</p> |
23 | 19 | {@render children()} |
24 | 20 | {:else} |
25 | 21 | <!-- eslint-disable @typescript-eslint/no-unused-vars --> |
26 | | - {#each Array(fullStars) as _} |
27 | | - <Icon {size} fillPercent={100} id={fullStarId} /> |
| 22 | + {#each Array(fullStars) as _, index} |
| 23 | + <Icon {size} fillPercent={100} starIndex={index} |
| 24 | + groupId={`${ratingGroupId}-full`} /> |
28 | 25 | {/each} |
29 | 26 | {#if percentRating} |
30 | | - <Icon {size} fillPercent={percentRating} id={partialId} /> |
| 27 | + <Icon {size} fillPercent={percentRating} starIndex={fullStars} |
| 28 | + groupId={`${ratingGroupId}-partial`} /> |
31 | 29 | {/if} |
32 | | - {#each Array(grayStars) as _} |
33 | | - <Icon {size} fillPercent={0} id={grayStarId} /> |
| 30 | + {#each Array(grayStars) as _, index} |
| 31 | + <Icon {size} fillPercent={0} starIndex={index} |
| 32 | + groupId={`${ratingGroupId}-empty`} /> |
34 | 33 | {/each} |
35 | 34 | {#if text} |
36 | 35 | {@render text()} |
|
0 commit comments