input.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. use dioxus_native_core::{
  2. custom_element::CustomElement, node::OwnedAttributeDiscription, prelude::NodeType,
  3. real_dom::NodeImmutable,
  4. };
  5. use super::{
  6. checkbox::CheckBox, number::Number, password::Password, slider::Slider, textbox::TextBox,
  7. RinkWidget,
  8. };
  9. use crate::widgets::button::Button;
  10. pub(crate) enum Input {
  11. Button(Button),
  12. CheckBox(CheckBox),
  13. TextBox(TextBox),
  14. Password(Password),
  15. Number(Number),
  16. Slider(Slider),
  17. }
  18. impl CustomElement for Input {
  19. const NAME: &'static str = "input";
  20. fn roots(&self) -> Vec<dioxus_native_core::NodeId> {
  21. match self {
  22. Input::Button(button) => button.roots(),
  23. Input::CheckBox(checkbox) => checkbox.roots(),
  24. Input::TextBox(textbox) => textbox.roots(),
  25. Input::Password(password) => password.roots(),
  26. Input::Number(number) => number.roots(),
  27. Input::Slider(slider) => slider.roots(),
  28. }
  29. }
  30. fn slot(&self) -> Option<dioxus_native_core::NodeId> {
  31. match self {
  32. Input::Button(button) => button.slot(),
  33. Input::CheckBox(checkbox) => checkbox.slot(),
  34. Input::TextBox(textbox) => textbox.slot(),
  35. Input::Password(password) => password.slot(),
  36. Input::Number(number) => number.slot(),
  37. Input::Slider(slider) => slider.slot(),
  38. }
  39. }
  40. fn create(mut root: dioxus_native_core::real_dom::NodeMut) -> Self {
  41. {
  42. // currently widgets are not allowed to have children
  43. let children = root.child_ids();
  44. let rdom = root.real_dom_mut();
  45. for child in children {
  46. if let Some(mut child) = rdom.get_mut(child) {
  47. child.remove();
  48. }
  49. }
  50. }
  51. let node_type = root.node_type();
  52. let NodeType::Element(el) = &*node_type else {
  53. panic!("input must be an element")
  54. };
  55. let input_type = el
  56. .attributes
  57. .get(&OwnedAttributeDiscription {
  58. name: "type".to_string(),
  59. namespace: None,
  60. })
  61. .and_then(|value| value.as_text());
  62. match input_type
  63. .map(|type_| type_.trim().to_lowercase())
  64. .as_deref()
  65. {
  66. Some("button") => {
  67. drop(node_type);
  68. Input::Button(Button::create(root))
  69. }
  70. Some("checkbox") => {
  71. drop(node_type);
  72. Input::CheckBox(CheckBox::create(root))
  73. }
  74. Some("textbox") => {
  75. drop(node_type);
  76. Input::TextBox(TextBox::create(root))
  77. }
  78. Some("password") => {
  79. drop(node_type);
  80. Input::Password(Password::create(root))
  81. }
  82. Some("number") => {
  83. drop(node_type);
  84. Input::Number(Number::create(root))
  85. }
  86. Some("range") => {
  87. drop(node_type);
  88. Input::Slider(Slider::create(root))
  89. }
  90. _ => {
  91. drop(node_type);
  92. Input::TextBox(TextBox::create(root))
  93. }
  94. }
  95. }
  96. fn attributes_changed(
  97. &mut self,
  98. root: dioxus_native_core::real_dom::NodeMut,
  99. attributes: &dioxus_native_core::node_ref::AttributeMask,
  100. ) {
  101. match self {
  102. Input::Button(button) => {
  103. button.attributes_changed(root, attributes);
  104. }
  105. Input::CheckBox(checkbox) => {
  106. checkbox.attributes_changed(root, attributes);
  107. }
  108. Input::TextBox(textbox) => {
  109. textbox.attributes_changed(root, attributes);
  110. }
  111. Input::Password(password) => {
  112. password.attributes_changed(root, attributes);
  113. }
  114. Input::Number(number) => {
  115. number.attributes_changed(root, attributes);
  116. }
  117. Input::Slider(slider) => {
  118. slider.attributes_changed(root, attributes);
  119. }
  120. }
  121. }
  122. }
  123. impl RinkWidget for Input {
  124. fn handle_event(&mut self, event: &crate::Event, node: dioxus_native_core::real_dom::NodeMut) {
  125. match self {
  126. Input::Button(button) => {
  127. button.handle_event(event, node);
  128. }
  129. Input::CheckBox(checkbox) => {
  130. checkbox.handle_event(event, node);
  131. }
  132. Input::TextBox(textbox) => {
  133. textbox.handle_event(event, node);
  134. }
  135. Input::Password(password) => {
  136. password.handle_event(event, node);
  137. }
  138. Input::Number(number) => {
  139. number.handle_event(event, node);
  140. }
  141. Input::Slider(slider) => {
  142. slider.handle_event(event, node);
  143. }
  144. }
  145. }
  146. }