web_event/input_event/input/
mod.rs

1use js_sys::Array;
2use std::fmt::{Debug, Formatter};
3use wasm_bindgen::JsCast;
4use web_sys::{DataTransfer, Event, EventTarget, InputEvent, Node, Window};
5
6/// Execute when a user writes something in an `<input>` field.
7/// - Bubbles: Yes
8/// - Cancelable: No
9/// - Event type: [`Event`] + [`InputEvent`]
10/// - Supported HTML tags: 	`<input type="color">`, `<input type="date">`, `<input type="datetime">`,
11///   `<input type="email">`, `<input type="month">`, `<input type="number">`, `<input
12///   type="password">`, `<input type="range">`, `<input type="search">`, `<input type="tel">`,
13///   `<input type="text">`, `<input type="time">`, `<input type="url">`, `<input type="week">` and
14///   `<textarea>`.
15#[derive(Clone, PartialEq, Eq)]
16pub struct OnInput {
17    inner: InputEvent,
18}
19
20impl Debug for OnInput {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        f.write_str("OnInput")
23    }
24}
25
26impl From<Event> for OnInput {
27    fn from(e: Event) -> Self {
28        Self { inner: e.unchecked_into() }
29    }
30}
31
32/// methods inherit form [`InputEvent`]
33impl OnInput {
34    /// Getter for the `isComposing` field of this object.
35    ///
36    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/isComposing)
37    #[inline]
38    pub fn is_composing(&self) -> bool {
39        self.inner.is_composing()
40    }
41    /// Getter for the `inputType` field of this object.
42    ///
43    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/inputType)
44    #[inline]
45    pub fn input_type(&self) -> String {
46        self.inner.input_type()
47    }
48    /// Getter for the `data` field of this object.
49    ///
50    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/data)
51    #[inline]
52    pub fn data(&self) -> Option<String> {
53        self.inner.data()
54    }
55    /// Getter for the `dataTransfer` field of this object.
56    ///
57    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/dataTransfer)
58    #[inline]
59    pub fn data_transfer(&self) -> Option<DataTransfer> {
60        self.inner.data_transfer()
61    }
62    /// The `getTargetRanges()` method.
63    ///
64    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/getTargetRanges)
65    #[inline]
66    pub fn get_target_ranges(&self) -> ::js_sys::Array {
67        self.inner.get_target_ranges()
68    }
69}
70
71/// methods inherit form [`UiEvent`]
72impl OnInput {
73    /// Getter for the `view` field of this object.
74    ///
75    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/view)
76    #[inline]
77    pub fn view(&self) -> Option<Window> {
78        self.inner.view()
79    }
80    /// Getter for the `detail` field of this object.
81    ///
82    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail)
83    #[inline]
84    pub fn detail(&self) -> i32 {
85        self.inner.detail()
86    }
87    /// Getter for the `layerX` field of this object.
88    ///
89    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/layerX)
90    #[inline]
91    pub fn layer_x(&self) -> i32 {
92        self.inner.layer_x()
93    }
94    /// Getter for the `layerY` field of this object.
95    ///
96    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/layerY)
97    #[inline]
98    pub fn layer_y(&self) -> i32 {
99        self.inner.layer_y()
100    }
101    /// Getter for the `pageX` field of this object.
102    ///
103    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/pageX)
104    #[inline]
105    pub fn page_x(&self) -> i32 {
106        self.inner.page_x()
107    }
108    /// Getter for the `pageY` field of this object.
109    ///
110    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/pageY)
111    #[inline]
112    pub fn page_y(&self) -> i32 {
113        self.inner.page_y()
114    }
115    /// Getter for the `which` field of this object.
116    ///
117    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/which)
118    #[inline]
119    pub fn which(&self) -> u32 {
120        self.inner.which()
121    }
122    /// Getter for the `rangeParent` field of this object.
123    ///
124    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/rangeParent)
125    #[inline]
126    pub fn range_parent(&self) -> Option<Node> {
127        self.inner.range_parent()
128    }
129    /// Getter for the `rangeOffset` field of this object.
130    ///
131    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/rangeOffset)
132    #[inline]
133    pub fn range_offset(&self) -> i32 {
134        self.inner.range_offset()
135    }
136}
137
138/// methods inherit form [`Event`]
139impl OnInput {
140    /// Getter for the `type` field of this object.
141    ///
142    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/type)
143    #[inline]
144    pub fn r#type(&self) -> String {
145        self.inner.type_()
146    }
147    /// Getter for the `target` field of this object.
148    ///
149    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/target)
150    pub fn target(&self) -> Option<EventTarget> {
151        self.inner.target()
152    }
153    /// Getter for the `currentTarget` field of this object.
154    ///
155    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget)
156    pub fn current_target(&self) -> Option<EventTarget> {
157        self.inner.current_target()
158    }
159    /// Getter for the `eventPhase` field of this object.
160    ///
161    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/eventPhase)
162    #[inline]
163    pub fn event_phase(&self) -> u16 {
164        self.inner.event_phase()
165    }
166    /// Getter for the `bubbles` field of this object.
167    ///
168    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/bubbles)
169    #[inline]
170    pub fn bubbles(&self) -> bool {
171        self.inner.bubbles()
172    }
173    /// Getter for the `cancelable` field of this object.
174    ///
175    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable)
176    #[inline]
177    pub fn cancelable(&self) -> bool {
178        self.inner.cancelable()
179    }
180    /// Getter for the `defaultPrevented` field of this object.
181    ///
182    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/defaultPrevented)
183    #[inline]
184    pub fn default_prevented(&self) -> bool {
185        self.inner.default_prevented()
186    }
187    /// Getter for the `composed` field of this object.
188    ///
189    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/composed)
190    #[inline]
191    pub fn composed(&self) -> bool {
192        self.inner.composed()
193    }
194    /// Getter for the `isTrusted` field of this object.
195    ///
196    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted)
197    #[inline]
198    pub fn is_trusted(&self) -> bool {
199        self.inner.is_trusted()
200    }
201    /// Getter for the `timeStamp` field of this object.
202    ///
203    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp)
204    #[inline]
205    pub fn time_stamp(&self) -> f64 {
206        self.inner.time_stamp()
207    }
208    /// Getter for the `cancelBubble` field of this object.
209    ///
210    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelBubble)
211    #[inline]
212    pub fn cancel_bubble(&self) -> bool {
213        self.inner.cancel_bubble()
214    }
215    /// Setter for the `cancelBubble` field of this object.
216    ///
217    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelBubble)
218    #[inline]
219    pub fn set_cancel_bubble(&self, value: bool) {
220        self.inner.set_cancel_bubble(value)
221    }
222    /// The `composedPath()` method.
223    ///
224    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath)
225    #[inline]
226    pub fn composed_path(&self) -> Array {
227        self.inner.composed_path()
228    }
229    /// The `preventDefault()` method.
230    ///
231    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
232    #[inline]
233    pub fn prevent_default(&self) {
234        self.inner.prevent_default()
235    }
236    /// The `stopImmediatePropagation()` method.
237    ///
238    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation)
239    #[inline]
240    pub fn stop_immediate_propagation(&self) {
241        self.inner.stop_immediate_propagation()
242    }
243    /// The `stopPropagation()` method.
244    ///
245    /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation)
246    #[inline]
247    pub fn stop_propagation(&self) {
248        self.inner.stop_propagation()
249    }
250}