use crate::element::{Element, HtmlElementConfig};
use super::field::{create_input, create_labeled_input, AsHtmlConfig, InputFieldConfig};
pub struct RangeInputConfigs {
pub max: f64,
pub min: f64,
pub step: Option<f64>,
}
pub fn create_labeled_range_input(
html_configs: InputFieldConfig,
input_configs: RangeInputConfigs,
value: Option<f64>,
) -> Element {
let value = match value {
Some(val) => Some(val.to_string()),
None => None,
};
create_labeled_input(html_configs, "range".to_string(), input_configs, value)
}
pub fn create_range_input(
html_configs: InputFieldConfig,
input_configs: RangeInputConfigs,
value: Option<f64>,
) -> Element {
let value = match value {
Some(val) => Some(val.to_string()),
None => None,
};
create_input(html_configs, "range".to_string(), input_configs, value)
}
impl RangeInputConfigs {
pub fn new(min: f64, max: f64) -> Self {
Self {
max,
min,
..Self::default()
}
}
pub fn with_max(mut self, max: f64) -> Self {
self.max = max;
self
}
pub fn with_min(mut self, min: f64) -> Self {
self.min = min;
self
}
pub fn with_step(mut self, step: f64) -> Self {
self.step = Some(step);
self
}
pub fn without_step(mut self) -> Self {
self.step = None;
self
}
}
impl AsHtmlConfig for RangeInputConfigs {
fn set_html_configs(&self, mut configs: HtmlElementConfig) -> HtmlElementConfig {
configs = configs.set_attribute("min".to_string(), Some(self.min.to_string()));
configs = configs.set_attribute("max".to_string(), Some(self.max.to_string()));
if let Some(step) = self.step {
configs = configs.set_attribute("max".to_string(), Some(step.to_string()));
}
configs
}
}
impl Default for RangeInputConfigs {
fn default() -> Self {
Self {
max: 1.0,
min: 0.0,
step: None,
}
}
}