1
0

checkbox.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. use std::collections::HashMap;
  2. use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData};
  3. use dioxus_native_core::{
  4. custom_element::CustomElement,
  5. node::OwnedAttributeDiscription,
  6. node_ref::AttributeMask,
  7. prelude::NodeType,
  8. real_dom::{ElementNodeMut, NodeImmutable, NodeMut, NodeTypeMut},
  9. NodeId,
  10. };
  11. use shipyard::UniqueView;
  12. use crate::hooks::FormData;
  13. use super::{RinkWidget, WidgetContext};
  14. #[derive(Debug, Default)]
  15. pub(crate) struct CheckBox {
  16. div_id: NodeId,
  17. text_id: NodeId,
  18. value: String,
  19. checked: bool,
  20. }
  21. impl CheckBox {
  22. fn width(el: &ElementNodeMut) -> String {
  23. if let Some(value) = el
  24. .get_attribute(&OwnedAttributeDiscription {
  25. name: "width".to_string(),
  26. namespace: None,
  27. })
  28. .and_then(|value| value.as_text())
  29. .map(|value| value.to_string())
  30. {
  31. value
  32. } else {
  33. "1px".to_string()
  34. }
  35. }
  36. fn height(el: &ElementNodeMut) -> String {
  37. if let Some(value) = el
  38. .get_attribute(&OwnedAttributeDiscription {
  39. name: "height".to_string(),
  40. namespace: None,
  41. })
  42. .and_then(|value| value.as_text())
  43. .map(|value| value.to_string())
  44. {
  45. value
  46. } else {
  47. "1px".to_string()
  48. }
  49. }
  50. fn update_size_attr(&mut self, el: &mut ElementNodeMut) {
  51. let width = Self::width(el);
  52. let height = Self::height(el);
  53. let single_char = width == "1px" || height == "1px";
  54. let border_style = if single_char { "none" } else { "solid" };
  55. el.set_attribute(
  56. OwnedAttributeDiscription {
  57. name: "border-style".to_string(),
  58. namespace: Some("style".to_string()),
  59. },
  60. border_style.to_string(),
  61. );
  62. }
  63. fn update_value_attr(&mut self, el: &ElementNodeMut) {
  64. self.value = el
  65. .get_attribute(&OwnedAttributeDiscription {
  66. name: "value".to_string(),
  67. namespace: None,
  68. })
  69. .and_then(|value| value.as_text())
  70. .map(|value| value.to_string())
  71. .unwrap_or_else(|| "on".to_string());
  72. }
  73. fn update_checked_attr(&mut self, el: &ElementNodeMut) {
  74. self.checked = el
  75. .get_attribute(&OwnedAttributeDiscription {
  76. name: "checked".to_string(),
  77. namespace: None,
  78. })
  79. .and_then(|value| value.as_text())
  80. .map(|value| value.to_string())
  81. .unwrap_or_else(|| "false".to_string())
  82. == "true";
  83. }
  84. fn write_value(&self, mut root: NodeMut) {
  85. let single_char = {
  86. let node_type = root.node_type_mut();
  87. let NodeTypeMut::Element(el) = node_type else {
  88. panic!("input must be an element")
  89. };
  90. Self::width(&el) == "1px" || Self::height(&el) == "1px"
  91. };
  92. let rdom = root.real_dom_mut();
  93. if let Some(mut text) = rdom.get_mut(self.text_id) {
  94. let node_type = text.node_type_mut();
  95. let NodeTypeMut::Text(mut text) = node_type else {
  96. panic!("input must be an element")
  97. };
  98. let value = if single_char {
  99. if self.checked {
  100. "☑"
  101. } else {
  102. "☐"
  103. }
  104. } else if self.checked {
  105. "✓"
  106. } else {
  107. " "
  108. };
  109. *text.text_mut() = value.to_string();
  110. }
  111. }
  112. fn switch(&mut self, mut node: NodeMut) {
  113. let new_state = !self.checked;
  114. let data = FormData {
  115. value: new_state
  116. .then(|| self.value.to_string())
  117. .unwrap_or_default(),
  118. values: HashMap::new(),
  119. files: None,
  120. };
  121. {
  122. let ctx: UniqueView<WidgetContext> = node
  123. .real_dom_mut()
  124. .raw_world_mut()
  125. .borrow()
  126. .expect("expected widget context");
  127. ctx.send(crate::Event {
  128. id: self.div_id,
  129. name: "input",
  130. data: crate::EventData::Form(data),
  131. bubbles: true,
  132. });
  133. }
  134. self.checked = new_state;
  135. self.write_value(node);
  136. }
  137. }
  138. impl CustomElement for CheckBox {
  139. const NAME: &'static str = "input";
  140. fn roots(&self) -> Vec<NodeId> {
  141. vec![self.text_id]
  142. }
  143. fn create(mut root: dioxus_native_core::real_dom::NodeMut) -> Self {
  144. let node_type = root.node_type();
  145. let NodeType::Element(el) = &*node_type else {
  146. panic!("input must be an element")
  147. };
  148. let value = el
  149. .attributes
  150. .get(&OwnedAttributeDiscription {
  151. name: "value".to_string(),
  152. namespace: None,
  153. })
  154. .and_then(|value| value.as_text())
  155. .map(|value| value.to_string());
  156. drop(node_type);
  157. let rdom = root.real_dom_mut();
  158. let text = rdom.create_node(String::new());
  159. let text_id = text.id();
  160. root.add_event_listener("click");
  161. root.add_event_listener("keydown");
  162. let div_id = root.id();
  163. let myself = Self {
  164. div_id,
  165. text_id,
  166. value: value.unwrap_or_default(),
  167. checked: false,
  168. };
  169. myself.write_value(root);
  170. myself
  171. }
  172. fn attributes_changed(
  173. &mut self,
  174. mut root: dioxus_native_core::real_dom::NodeMut,
  175. attributes: &dioxus_native_core::node_ref::AttributeMask,
  176. ) {
  177. match attributes {
  178. AttributeMask::All => {
  179. {
  180. let node_type = root.node_type_mut();
  181. let NodeTypeMut::Element(mut el) = node_type else {
  182. panic!("input must be an element")
  183. };
  184. self.update_value_attr(&el);
  185. self.update_size_attr(&mut el);
  186. self.update_checked_attr(&el);
  187. }
  188. self.write_value(root);
  189. }
  190. AttributeMask::Some(attrs) => {
  191. {
  192. let node_type = root.node_type_mut();
  193. let NodeTypeMut::Element(mut el) = node_type else {
  194. panic!("input must be an element")
  195. };
  196. if attrs.contains("width") || attrs.contains("height") {
  197. self.update_size_attr(&mut el);
  198. }
  199. if attrs.contains("value") {
  200. self.update_value_attr(&el);
  201. }
  202. if attrs.contains("checked") {
  203. self.update_checked_attr(&el);
  204. }
  205. }
  206. if attrs.contains("checked") {
  207. self.write_value(root);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. impl RinkWidget for CheckBox {
  214. fn handle_event(&mut self, event: &crate::Event, node: dioxus_native_core::real_dom::NodeMut) {
  215. match event.name {
  216. "click" => self.switch(node),
  217. "keydown" => {
  218. if let crate::EventData::Keyboard(data) = &event.data {
  219. if !data.is_auto_repeating()
  220. && match data.key() {
  221. Key::Character(c) if c == " " => true,
  222. Key::Enter => true,
  223. _ => false,
  224. }
  225. {
  226. self.switch(node);
  227. }
  228. }
  229. }
  230. _ => {}
  231. }
  232. }
  233. }