Skip to content

Commit

Permalink
feat: allow using Formatter::Function from WASM, add value_formatter …
Browse files Browse the repository at this point in the history
…in Tooltip
  • Loading branch information
v3xro committed Jul 26, 2024
1 parent 7bb6774 commit 2926354
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions charming/src/element/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use serde::Serialize;

use super::RawString;
use serde::Serialize;

#[derive(Serialize)]
#[serde(untagged)]
pub enum Formatter {
String(String),

#[cfg(target_arch = "wasm32")]
Function(#[serde(with = "serde_wasm_bindgen::preserve")] web_sys::js_sys::Function),

#[cfg(not(target_arch = "wasm32"))]
Function(RawString),
}

Expand All @@ -15,6 +19,7 @@ impl From<&str> for Formatter {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<RawString> for Formatter {
fn from(s: RawString) -> Self {
Formatter::Function(s)
Expand Down
9 changes: 9 additions & 0 deletions charming/src/element/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct Tooltip {
#[serde(skip_serializing_if = "Option::is_none")]
formatter: Option<Formatter>,

#[serde(skip_serializing_if = "Option::is_none")]
value_formatter: Option<Formatter>,

#[serde(skip_serializing_if = "Option::is_none")]
position: Option<String>,

Expand All @@ -59,6 +62,7 @@ impl Tooltip {
trigger_on: None,
axis_pointer: None,
formatter: None,
value_formatter: None,
position: None,
padding: None,
background_color: None,
Expand Down Expand Up @@ -87,6 +91,11 @@ impl Tooltip {
self
}

pub fn value_formatter<F: Into<Formatter>>(mut self, value_formatter: F) -> Self {
self.value_formatter = Some(value_formatter.into());
self
}

pub fn position<S: Into<String>>(mut self, position: S) -> Self {
self.position = Some(position.into());
self
Expand Down

0 comments on commit 2926354

Please sign in to comment.