plaster_forms/fields/
text.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::fields::ValidationFn;
use plaster::prelude::*;

/// An <input type="text" /> field
pub struct TextField {
    label: String,
    value: String,
    password: bool,
    class: String,
    validate: ValidationFn<String>,
    validation_error: Option<String>,
    on_change: Option<Callback<String>>,
    on_blur: Option<Callback<()>>,
}

pub enum Msg {
    Change(InputData),
    Blur,
}

#[derive(Default, Clone, PartialEq)]
pub struct Props {
    /// The input label
    pub label: String,
    /// The controlled value of the input
    pub value: Option<String>,
    /// Whether or not this is a password field
    pub password: bool,
    /// HTML class
    pub class: String,
    /// A function that returns a validation error
    pub validate: ValidationFn<String>,
    /// A callback that is fired when the user changes the input value
    pub on_change: Option<Callback<String>>,
    /// A callback that is fired when the field loses focus
    pub on_blur: Option<Callback<()>>,
}

impl Component for TextField {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, _context: ComponentLink<Self>) -> Self {
        TextField {
            label: props.label,
            value: props.value.unwrap_or(String::new()),
            password: props.password,
            class: props.class,
            validate: props.validate,
            validation_error: None,
            on_change: props.on_change,
            on_blur: props.on_blur,
        }
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        let mut updated = false;

        if let Some(value) = props.value {
            if value != self.value {
                self.value = value;
                updated = true;
            }
        }

        if props.label != self.label {
            self.label = props.label;
            updated = true;
        }

        if props.class != self.class {
            self.class = props.class;
            updated = true;
        }

        self.validate = props.validate;
        self.on_change = props.on_change;
        self.on_blur = props.on_blur;

        updated
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Change(data) => {
                if let Some(ref callback) = self.on_change {
                    callback.emit(data.value.clone());
                }

                self.value = data.value;

                self.validation_error = self.validate.validate(self.value.clone());
            }
            Msg::Blur => {
                if let Some(ref callback) = self.on_blur {
                    callback.emit(());
                }
            }
        };

        true
    }
}

impl Renderable<TextField> for TextField {
    fn view(&self) -> Html<Self> {
        let ty = if self.password { "password" } else { "text" };

        let (class, error) = if let Some(ref err) = self.validation_error {
            (
                format!("{} error", &self.class),
                html! {
                    <div class="input-error",>
                        {err}
                    </div>
                },
            )
        } else {
            (self.class.clone(), html!(<span />))
        };

        #[cfg(not(feature = "ionic"))]
        {
            html! {
                <div class=class,>
                    <input
                        type=ty,
                        placeholder=&self.label,
                        value=&self.value,
                        oninput=|data| Msg::Change(data),
                        onblur=|_| Msg::Blur,
                    />
                    {error}
                </div>
            }
        }

        #[cfg(feature = "ionic")]
        {
            use wasm_bindgen::JsCast;

            #[derive(serde_derive::Deserialize)]
            struct Detail {
                value: String,
            }

            html! {
                <ion_item>
                    <ion_input
                        type=ty,
                        placeholder=&self.label,
                        value=&self.value,
                        [ionChange]=|event: Event| {
                            let c: web_sys::CustomEvent = event.dyn_into().expect("is not custom event");
                            let detail: Detail = c.detail().into_serde().unwrap();
                            Msg::Change(InputData { value: detail.value })
                        },
                    />
                    {error}
                </ion_item>
            }
        }
    }
}