prevent_default.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use dioxus_native_core::prelude::*;
  2. use dioxus_native_core_macro::partial_derive_state;
  3. use shipyard::Component;
  4. #[derive(PartialEq, Debug, Clone, Copy, Component, Default)]
  5. pub(crate) enum PreventDefault {
  6. Focus,
  7. KeyPress,
  8. KeyRelease,
  9. KeyDown,
  10. KeyUp,
  11. MouseDown,
  12. Click,
  13. MouseEnter,
  14. MouseLeave,
  15. MouseOut,
  16. #[default]
  17. Unknown,
  18. MouseOver,
  19. ContextMenu,
  20. Wheel,
  21. MouseUp,
  22. }
  23. #[partial_derive_state]
  24. impl State for PreventDefault {
  25. type ParentDependencies = ();
  26. type ChildDependencies = ();
  27. type NodeDependencies = ();
  28. const NODE_MASK: dioxus_native_core::node_ref::NodeMaskBuilder<'static> =
  29. dioxus_native_core::node_ref::NodeMaskBuilder::new()
  30. .with_attrs(dioxus_native_core::node_ref::AttributeMaskBuilder::Some(&[
  31. "dioxus-prevent-default",
  32. ]))
  33. .with_listeners();
  34. fn update<'a>(
  35. &mut self,
  36. node_view: NodeView,
  37. _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  38. _: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  39. _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
  40. _: &SendAnyMap,
  41. ) -> bool {
  42. let new = match node_view.attributes().and_then(|mut attrs| {
  43. attrs
  44. .find(|a| a.attribute.name == "dioxus-prevent-default")
  45. .and_then(|a| a.value.as_text())
  46. }) {
  47. Some("onfocus") => PreventDefault::Focus,
  48. Some("onkeypress") => PreventDefault::KeyPress,
  49. Some("onkeyrelease") => PreventDefault::KeyRelease,
  50. Some("onkeydown") => PreventDefault::KeyDown,
  51. Some("onkeyup") => PreventDefault::KeyUp,
  52. Some("onclick") => PreventDefault::Click,
  53. Some("onmousedown") => PreventDefault::MouseDown,
  54. Some("onmouseup") => PreventDefault::MouseUp,
  55. Some("onmouseenter") => PreventDefault::MouseEnter,
  56. Some("onmouseover") => PreventDefault::MouseOver,
  57. Some("onmouseleave") => PreventDefault::MouseLeave,
  58. Some("onmouseout") => PreventDefault::MouseOut,
  59. Some("onwheel") => PreventDefault::Wheel,
  60. Some("oncontextmenu") => PreventDefault::ContextMenu,
  61. _ => return false,
  62. };
  63. if new == *self {
  64. false
  65. } else {
  66. *self = new;
  67. true
  68. }
  69. }
  70. fn create<'a>(
  71. node_view: NodeView<()>,
  72. node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  73. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  74. children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
  75. context: &SendAnyMap,
  76. ) -> Self {
  77. let mut myself = Self::default();
  78. myself.update(node_view, node, parent, children, context);
  79. myself
  80. }
  81. }