virtual_node/event/
event_name.rs

1use std::borrow::Cow;
2
3/// The name of the event with the `on` prefix.
4///
5/// onclick, oninput, onmousemove ... etc
6#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
7pub struct EventName(Cow<'static, str>);
8
9impl EventName {
10    /// Create a new `EventName`.
11    ///
12    /// # Panics
13    ///
14    /// Panics if the event does not start with "on". Such as "onclick".
15    pub fn new(name: Cow<'static, str>) -> Self {
16        EventName(name)
17    }
18
19    /// `onclick`, `onmousemove` etc
20    pub fn with_on_prefix(&self) -> &str {
21        &self.0.as_ref()
22    }
23
24    /// `click`, `mousemove` etc
25    pub fn without_on_prefix(&self) -> &str {
26        &self.0.as_ref()[2..]
27    }
28}
29
30impl EventName {
31    /// Whether or not this event gets handled by our event delegation system.
32    /// If not, the event will be attached to the DOM element.
33    pub fn is_delegated(&self) -> bool {
34        if self == &Self::ONCLICK {
35            true
36        } else {
37            false
38        }
39    }
40}
41
42impl EventName {
43    /// The "onclick" event
44    pub const ONCLICK: EventName = EventName(Cow::Borrowed("onclick"));
45
46    /// The "oninput" event
47    pub const ONINPUT: EventName = EventName(Cow::Borrowed("oninput"));
48}
49
50impl From<&'static str> for EventName {
51    fn from(event_name: &'static str) -> Self {
52        EventName(Cow::Borrowed(event_name))
53    }
54}
55
56impl From<String> for EventName {
57    fn from(event_name: String) -> Self {
58        EventName(Cow::Owned(event_name))
59    }
60}