Skip to main content

react_rs_elements/
attributes.rs

1use crate::reactive::ReactiveValue;
2
3pub struct Attribute {
4    pub name: String,
5    pub value: AttributeValue,
6}
7
8pub enum AttributeValue {
9    String(String),
10    Bool(bool),
11    ReactiveString(ReactiveValue<String>),
12    ReactiveBool(ReactiveValue<bool>),
13}
14
15impl Attribute {
16    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
17        Self {
18            name: name.into(),
19            value: AttributeValue::String(value.into()),
20        }
21    }
22
23    pub fn boolean(name: impl Into<String>, value: bool) -> Self {
24        Self {
25            name: name.into(),
26            value: AttributeValue::Bool(value),
27        }
28    }
29
30    pub fn reactive_string(name: impl Into<String>, value: ReactiveValue<String>) -> Self {
31        Self {
32            name: name.into(),
33            value: AttributeValue::ReactiveString(value),
34        }
35    }
36
37    pub fn reactive_bool(name: impl Into<String>, value: ReactiveValue<bool>) -> Self {
38        Self {
39            name: name.into(),
40            value: AttributeValue::ReactiveBool(value),
41        }
42    }
43
44    pub fn to_static_value(&self) -> String {
45        match &self.value {
46            AttributeValue::String(s) => s.clone(),
47            AttributeValue::Bool(b) => b.to_string(),
48            AttributeValue::ReactiveString(r) => r.get(),
49            AttributeValue::ReactiveBool(r) => r.get().to_string(),
50        }
51    }
52}