viewy 2.0.1

A web UI toolkit that combine the advantages of a design system and an ui library.
use crate::DefaultModifiers;
use crate::Renderable;
use crate::components::View;
use crate::node::Node;

/// Use this component to display the progress of a task
#[derive(Debug, Clone)]
pub struct ProgressBar {
    node: Node,

    /// The current numeric value. This must be between the minimum and maximum values (min attribute and max attribute) if they are specified. If unspecified or malformed, the value is 0. If specified, but not within the range given by the min attribute and max attribute, the value is equal to the nearest end of the range.
    /// > Note: Unless the value attribute is between 0 and 1 (inclusive), the min and max attributes should define the range so that the value attribute's value is within it.
    pub value: Option<f32>,

    /// The upper numeric bound of the measured range. This must be greater than the minimum value (min attribute), if specified. If unspecified, the maximum value is 1.0
    pub max: Option<f32>,
}

impl ProgressBar {
    pub fn new() -> Self {
        ProgressBar {
            node: Node::default(),
            value: None,
            max: None,
        }
    }

    pub fn value(&mut self, value: f32) -> &mut Self {
        self.value = Some(value);
        self
    }

    pub fn max(&mut self, max: f32) -> &mut Self {
        self.max = Some(max);
        self
    }
}
impl std::ops::Deref for ProgressBar {
    type Target = Node;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}

impl std::ops::DerefMut for ProgressBar {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.node
    }
}
impl DefaultModifiers for ProgressBar {}

impl Renderable for ProgressBar {
    fn render(mut self) -> Node {
        self.add_class("progress-bar");

        let mut progress_element = View::new();
        progress_element
            .tag("progress")
            .add_class("progress-bar__progress-element");

        if let Some(value) = &self.value {
            progress_element.set_attr("value", &value.to_string());
        }

        if let Some(max) = self.max {
            progress_element.set_attr("max", &max.to_string());
        }

        progress_element.node.text = Some(format!(
            "{}/{}",
            self.value.unwrap_or(0.0),
            self.max.unwrap_or(1.0)
        ));

        self.node.children.push(progress_element.render());

        self.node
    }
}