edge_frame/
field.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::{cell::RefCell, rc::Rc};

use web_sys::Event;
use yew::{Callback, UseStateHandle};

use super::util::*;

#[derive(Clone)]
pub struct Field<R, S> {
    initial_value: Option<R>,
    raw_value: Rc<RefCell<Option<R>>>,
    value_state: UseStateHandle<Option<R>>,
    converter: Callback<Event, R>,
    validator: Callback<R, Result<S, String>>,
}

pub type TextField<S> = Field<String, S>;
pub type CheckedField<S> = Field<bool, S>;

impl<S> Field<String, S>
where
    S: Clone,
{
    pub fn text(
        initial_value: String,
        value_state: UseStateHandle<Option<String>>,
        validator: impl Fn(String) -> Result<S, String> + 'static,
    ) -> Rc<Self> {
        Rc::new(Self::new(
            Some(initial_value),
            value_state,
            get_input_text,
            validator,
        ))
    }
}

impl<S> Field<bool, S>
where
    S: Clone,
{
    pub fn checked(
        initial_value: bool,
        value_state: UseStateHandle<Option<bool>>,
        validator: impl Fn(bool) -> Result<S, String> + 'static,
    ) -> Rc<Self> {
        Rc::new(Self::new(
            Some(initial_value),
            value_state,
            get_input_checked,
            validator,
        ))
    }
}

impl<R, S> Field<R, S>
where
    R: Default + Clone + PartialEq + 'static,
    S: Clone,
{
    pub fn new(
        initial_value: Option<R>,
        value_state: UseStateHandle<Option<R>>,
        converter: impl Fn(Event) -> R + 'static,
        validator: impl Fn(R) -> Result<S, String> + 'static,
    ) -> Self {
        Self {
            initial_value,
            value_state,
            raw_value: Rc::new(RefCell::new(None)),
            converter: Callback::from(converter),
            validator: Callback::from(validator),
        }
    }

    pub fn is_dirty(&self) -> bool {
        self.has_errors()
            || self.raw_value.borrow_mut().is_some()
            || self.value_state.is_some() && *self.value_state != self.initial_value
    }

    pub fn value(&self) -> Option<S> {
        self.validator.emit(self.raw_value()).ok()
    }

    pub fn raw_value(&self) -> R {
        self.raw_value.borrow().clone().unwrap_or_else(|| {
            self.value_state.as_ref().cloned().unwrap_or_else(|| {
                self.initial_value
                    .clone()
                    .unwrap_or_else(|| Default::default())
            })
        })
    }

    pub fn has_errors(&self) -> bool {
        self.error().is_some()
    }

    pub fn error(&self) -> Option<String> {
        match self.validator.emit(self.raw_value()) {
            Ok(_) => None,
            Err(error) => Some(error),
        }
    }

    pub fn error_str(&self) -> String {
        self.error().unwrap_or_else(|| "\u{00a0}".into())
    }

    pub fn change<V>(&self, callback: Callback<()>) -> impl Fn(V)
    where
        V: Into<Event>,
    {
        let this = (*self).clone();

        move |event| {
            this.on_change(event.into());
            callback.emit(());
        }
    }

    pub fn on_change(&self, event: Event) {
        let value = self.converter.emit(event);

        *self.raw_value.borrow_mut() = Some(value.clone());
        self.value_state.set(Some(value));
    }
}