use super::*;
pub fn rating_fill_percent(value: f64, max: f64) -> f64 {
if max <= 0.0 {
return 0.0;
}
(value / max * 100.0).clamp(0.0, 100.0)
}
#[component]
pub fn euv_rating(node: VirtualNode<EuvRatingProps>) -> VirtualNode {
let EuvRatingProps {
value,
max,
readonly,
size,
}: EuvRatingProps = node.try_get_props().unwrap_or_default();
let current: f64 = value.get();
let scale: f64 = max.get();
let percent: f64 = rating_fill_percent(current, scale);
let units: i32 = if scale <= 0.0 { 0 } else { scale as i32 };
let filled: i32 = ((current + 0.5).floor() as i32).clamp(0, units);
let px: i32 = size.px();
let star_style: String = format!("font-size: {px}px;");
let fill_style: String = format!("width: {percent}%;");
let aria_label: String = format!("{} of {} stars", current, scale);
html! {
div {
class: c_euv_rating()
role: "img"
aria-label: aria_label
for index in 0..units {
span {
key: index.to_string()
class: c_euv_rating_star()
style: star_style.clone()
if index < filled {
"★"
} else {
"☆"
}
}
}
if { !readonly.get() } {
div {
class: c_euv_rating_fill()
style: fill_style.clone()
}
}
}
}
}