|
3 | 3 | import { twMerge } from 'tailwind-merge'; |
4 | 4 | import generateId from '../utils/generateId.js'; |
5 | 5 | import type { ComponentType } from 'svelte'; |
| 6 | + import type { HTMLAttributes } from 'svelte/elements'; |
6 | 7 |
|
7 | | - export let divClass: string = 'flex items-center'; |
8 | | - export let size: number = 24; |
9 | | - export let total: number = 5; |
10 | | - export let rating: number = 4; |
11 | | - export let partialId: string = 'partialStar' + generateId(); |
12 | | - export let icon: ComponentType = Star; |
13 | | - export let count: boolean = false; |
| 8 | + interface $$Props extends HTMLAttributes<HTMLDivElement> { |
| 9 | + divClass?: string; |
| 10 | + size?: number; |
| 11 | + total?: number; |
| 12 | + rating: number; |
| 13 | + partialId?: string; |
| 14 | + icon?: ComponentType; |
| 15 | + count?: boolean; |
| 16 | + iconFillColor?: string; |
| 17 | + iconStrokeColor?: string; |
| 18 | + } |
| 19 | +
|
| 20 | + export let divClass: $$Props['divClass'] = 'flex items-center'; |
| 21 | + export let size: $$Props['size'] = 24; |
| 22 | + export let total: NonNullable<$$Props['total']> = 5; |
| 23 | + export let rating: $$Props['rating'] = 4; |
| 24 | + export let partialId: $$Props['partialId'] = 'partialStar' + generateId(); |
| 25 | + export let icon: $$Props['icon'] = Star; |
| 26 | + export let count: $$Props['count'] = false; |
| 27 | + export let iconFillColor: $$Props['iconFillColor']; |
| 28 | + export let iconStrokeColor: $$Props['iconStrokeColor']; |
14 | 29 |
|
15 | 30 | // generate unique id for full star and gray star |
16 | 31 | const fullStarId: string = generateId(); |
17 | 32 | const grayStarId: string = generateId(); |
18 | | - let fullStars: number = Math.floor(rating); |
19 | | - let rateDiffence = rating - fullStars; |
20 | | - let percentRating = Math.round(rateDiffence * 100); |
21 | | - let grayStars: number = total - (fullStars + Math.ceil(rateDiffence)); |
| 33 | + $: fullStars = Math.floor(rating); |
| 34 | + $: rateDiffence = rating - fullStars; |
| 35 | + $: percentRating = Math.round(rateDiffence * 100); |
| 36 | + $: grayStars = total - (fullStars + Math.ceil(rateDiffence)); |
22 | 37 | // console.log(fullStars, grayStars, rateDiffence, percentRating) |
23 | 38 | </script> |
24 | 39 |
|
25 | 40 | <div class={twMerge(divClass, $$props.class)}> |
26 | 41 | {#if count} |
27 | | - <svelte:component this={icon} fillPercent={100} {size} /> |
| 42 | + <svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} fillPercent={100} {size} /> |
28 | 43 | <p class="ms-2 text-sm font-bold text-gray-900 dark:text-white">{rating}</p> |
29 | 44 | <slot /> |
30 | 45 | {:else} |
31 | 46 | {#each Array(fullStars) as star} |
32 | | - <svelte:component this={icon} {size} fillPercent={100} id={fullStarId} /> |
| 47 | + <svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={100} id={fullStarId} /> |
33 | 48 | {/each} |
34 | 49 | {#if percentRating} |
35 | | - <svelte:component this={icon} {size} fillPercent={percentRating} id={partialId} /> |
| 50 | + <svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={percentRating} id={partialId} /> |
36 | 51 | {/if} |
37 | 52 | {#each Array(grayStars) as star} |
38 | | - <svelte:component this={icon} {size} fillPercent={0} id={grayStarId} /> |
| 53 | + <svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={0} id={grayStarId} /> |
39 | 54 | {/each} |
40 | 55 | {#if $$slots.text} |
41 | 56 | <slot name="text" /> |
|
0 commit comments