2023-06-07 16:22:44 +02:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-06-07 16:22:44 +02:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-06-07 16:22:44 +02:00
|
|
|
*/
|
2023-06-30 18:21:18 -04:00
|
|
|
import { useState } from "react";
|
2023-06-07 16:22:44 +02:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
|
|
import styles from "./StarRatingInput.module.css";
|
2023-09-27 19:06:10 -04:00
|
|
|
import StarSelected from "../icons/StarSelected.svg?react";
|
|
|
|
|
import StarUnselected from "../icons/StarUnselected.svg?react";
|
2023-06-07 16:22:44 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
starCount: number;
|
|
|
|
|
onChange: (stars: number) => void;
|
|
|
|
|
required?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function StarRatingInput({
|
|
|
|
|
starCount,
|
|
|
|
|
onChange,
|
|
|
|
|
required,
|
|
|
|
|
}: Props): JSX.Element {
|
|
|
|
|
const [rating, setRating] = useState(0);
|
|
|
|
|
const [hover, setHover] = useState(0);
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.starRating}>
|
|
|
|
|
{[...Array(starCount)].map((_star, index) => {
|
|
|
|
|
index += 1;
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={styles.inputContainer}
|
2023-09-22 18:05:13 -04:00
|
|
|
onMouseEnter={(): void => setHover(index)}
|
|
|
|
|
onMouseLeave={(): void => setHover(rating)}
|
2023-06-07 16:22:44 +02:00
|
|
|
key={index}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
className={styles.hideElement}
|
|
|
|
|
type="radio"
|
|
|
|
|
id={"starInput" + String(index)}
|
|
|
|
|
value={String(index) + "Star"}
|
|
|
|
|
name="star rating"
|
2023-09-22 18:05:13 -04:00
|
|
|
onChange={(_ev): void => {
|
2023-06-07 16:22:44 +02:00
|
|
|
setRating(index);
|
|
|
|
|
onChange(index);
|
|
|
|
|
}}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<label
|
|
|
|
|
className={styles.hideElement}
|
|
|
|
|
id={"starInvisibleLabel" + String(index)}
|
2023-06-07 17:12:24 +02:00
|
|
|
htmlFor={"starInput" + String(index)}
|
2023-06-07 16:22:44 +02:00
|
|
|
>
|
2023-11-20 13:00:43 +00:00
|
|
|
{t("star_rating_input_label", {
|
2023-06-07 16:22:44 +02:00
|
|
|
count: index,
|
|
|
|
|
})}
|
|
|
|
|
</label>
|
|
|
|
|
<label
|
|
|
|
|
className={styles.starIcon}
|
|
|
|
|
id={"starIcon" + String(index)}
|
2023-06-07 17:12:24 +02:00
|
|
|
htmlFor={"starInput" + String(index)}
|
2023-06-07 16:22:44 +02:00
|
|
|
>
|
|
|
|
|
{index <= (hover || rating) ? (
|
|
|
|
|
<StarSelected />
|
|
|
|
|
) : (
|
|
|
|
|
<StarUnselected />
|
|
|
|
|
)}
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|