material_dioxus/text_inputs/
validity_state.rs

1use js_sys::Object;
2use wasm_bindgen::prelude::*;
3use wasm_bindgen::JsCast;
4
5#[wasm_bindgen]
6extern "C" {
7    #[derive(Debug)]
8    #[wasm_bindgen(extends = Object)]
9    pub(crate) type ValidityStateJS;
10
11    #[wasm_bindgen(method, setter = badInput)]
12    pub fn set_bad_input(this: &ValidityStateJS, val: bool);
13
14    #[wasm_bindgen(method, setter = customError)]
15    pub fn set_custom_error(this: &ValidityStateJS, val: bool);
16
17    #[wasm_bindgen(method, setter = patternMismatch)]
18    pub fn set_pattern_mismatch(this: &ValidityStateJS, val: bool);
19
20    #[wasm_bindgen(method, setter = rangeOverflow)]
21    pub fn set_range_overflow(this: &ValidityStateJS, val: bool);
22
23    #[wasm_bindgen(method, setter = rangeUnderflow)]
24    pub fn set_range_underflow(this: &ValidityStateJS, val: bool);
25
26    #[wasm_bindgen(method, setter = too_long)]
27    pub fn set_too_long(this: &ValidityStateJS, val: bool);
28
29    #[wasm_bindgen(method, setter = tooShort)]
30    pub fn set_too_short(this: &ValidityStateJS, val: bool);
31
32    #[wasm_bindgen(method, setter = type_mismatch)]
33    pub fn set_type_mismatch(this: &ValidityStateJS, val: bool);
34
35    #[wasm_bindgen(method, setter = valid)]
36    pub fn set_valid(this: &ValidityStateJS, val: bool);
37
38    #[wasm_bindgen(method, setter = valueMissing)]
39    pub fn set_value_missing(this: &ValidityStateJS, val: bool);
40}
41
42impl Default for ValidityStateJS {
43    fn default() -> Self {
44        Object::new().unchecked_into()
45    }
46}
47
48/// Rust type for validity props
49pub struct ValidityState {
50    bad_input: bool,
51    custom_error: bool,
52    pattern_mismatch: bool,
53    range_overflow: bool,
54    range_underflow: bool,
55    too_long: bool,
56    too_short: bool,
57    type_mismatch: bool,
58    valid: bool,
59    value_missing: bool,
60}
61
62impl ValidityState {
63    /// Creates a new `ValidityState`.
64    ///
65    /// All the fields except `valid` is set to false except `valid`, which is
66    /// set to `true`
67    pub fn new() -> Self {
68        Self {
69            bad_input: false,
70            custom_error: false,
71            pattern_mismatch: false,
72            range_overflow: false,
73            range_underflow: false,
74            too_long: false,
75            too_short: false,
76            type_mismatch: false,
77            valid: true,
78            value_missing: false,
79        }
80    }
81
82    pub fn bad_input(&self) -> bool {
83        self.bad_input
84    }
85    pub fn custom_error(&self) -> bool {
86        self.custom_error
87    }
88    pub fn pattern_mismatch(&self) -> bool {
89        self.pattern_mismatch
90    }
91    pub fn range_overflow(&self) -> bool {
92        self.range_overflow
93    }
94    pub fn range_underflow(&self) -> bool {
95        self.range_underflow
96    }
97    pub fn too_long(&self) -> bool {
98        self.too_long
99    }
100    pub fn too_short(&self) -> bool {
101        self.too_short
102    }
103    pub fn type_mismatch(&self) -> bool {
104        self.type_mismatch
105    }
106    pub fn valid(&self) -> bool {
107        self.valid
108    }
109    pub fn value_missing(&self) -> bool {
110        self.value_missing
111    }
112
113    pub fn set_bad_input(&mut self, value: bool) -> &mut Self {
114        self.bad_input = value;
115        self
116    }
117    pub fn set_custom_error(&mut self, value: bool) -> &mut Self {
118        self.custom_error = value;
119        self
120    }
121    pub fn set_pattern_mismatch(&mut self, value: bool) -> &mut Self {
122        self.pattern_mismatch = value;
123        self
124    }
125    pub fn set_range_overflow(&mut self, value: bool) -> &mut Self {
126        self.range_overflow = value;
127        self
128    }
129    pub fn set_range_underflow(&mut self, value: bool) -> &mut Self {
130        self.range_underflow = value;
131        self
132    }
133    pub fn set_too_long(&mut self, value: bool) -> &mut Self {
134        self.too_long = value;
135        self
136    }
137    pub fn set_too_short(&mut self, value: bool) -> &mut Self {
138        self.too_short = value;
139        self
140    }
141    pub fn set_type_mismatch(&mut self, value: bool) -> &mut Self {
142        self.type_mismatch = value;
143        self
144    }
145    pub fn set_valid(&mut self, value: bool) -> &mut Self {
146        self.valid = value;
147        self
148    }
149    pub fn set_value_missing(&mut self, value: bool) -> &mut Self {
150        self.value_missing = value;
151        self
152    }
153}
154
155impl From<ValidityState> for ValidityStateJS {
156    fn from(validity_state: ValidityState) -> Self {
157        let validity_state_js = ValidityStateJS::default();
158        validity_state_js.set_bad_input(validity_state.bad_input());
159        validity_state_js.set_custom_error(validity_state.custom_error());
160        validity_state_js.set_pattern_mismatch(validity_state.pattern_mismatch());
161        validity_state_js.set_range_overflow(validity_state.range_overflow());
162        validity_state_js.set_range_underflow(validity_state.range_underflow());
163        validity_state_js.set_too_long(validity_state.too_long());
164        validity_state_js.set_too_short(validity_state.too_short());
165        validity_state_js.set_type_mismatch(validity_state.type_mismatch());
166        validity_state_js.set_valid(validity_state.valid());
167        validity_state_js.set_value_missing(validity_state.value_missing());
168        validity_state_js
169    }
170}
171
172impl Default for ValidityState {
173    fn default() -> Self {
174        Self::new()
175    }
176}