Skip to content

Commit

Permalink
Fix hide middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Apr 19, 2024
1 parent 4dac2d1 commit 4a23e33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
24 changes: 23 additions & 1 deletion packages/core/src/compute_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,29 @@ pub fn compute_position<Element: Clone, Window: Clone>(
y = next_y.unwrap_or(y);

if let Some(data) = data {
middleware_data.set(middleware.name(), data);
let existing_data = middleware_data.get(middleware.name());

let new_data = match existing_data {
Some(existing_data) => {
let mut a = existing_data
.as_object()
.expect("Existing data should be an object.")
.to_owned();

let mut b = data
.as_object()
.expect("New data should be an object.")
.to_owned();

b.retain(|_, v| !v.is_null());
a.extend(b);

serde_json::Value::Object(a)
}
None => data,
};

middleware_data.set(middleware.name(), new_data);
}

if let Some(reset) = reset {
Expand Down
42 changes: 17 additions & 25 deletions packages/core/src/middleware/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,13 @@ impl<Element: Clone> Default for HideOptions<Element> {
}
}

/// Data stored in [`HideData`] when using [`HideStrategy::ReferenceHidden`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct HideDataReferenceHidden {
pub reference_hidden: bool,
pub reference_hidden_offsets: SideObject,
}

/// Data stored in [`HideData`] when using [`HideStrategy::Escaped`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct HideDataEscaped {
pub escaped: bool,
pub escaped_offsets: SideObject,
}

/// Data stored by [`Hide`] middleware.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum HideData {
ReferenceHidden(HideDataReferenceHidden),
Escaped(HideDataEscaped),
pub struct HideData {
pub reference_hidden: Option<bool>,
pub reference_hidden_offsets: Option<SideObject>,
pub escaped: Option<bool>,
pub escaped_offsets: Option<SideObject>,
}

/// Provides data to hide the floating element in applicable situations,
Expand Down Expand Up @@ -163,10 +151,12 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Hide<'a,
x: None,
y: None,
data: Some(
serde_json::to_value(HideData::ReferenceHidden(HideDataReferenceHidden {
reference_hidden: is_any_side_fully_clipped(&offsets),
reference_hidden_offsets: offsets,
}))
serde_json::to_value(HideData {
reference_hidden: Some(is_any_side_fully_clipped(&offsets)),
reference_hidden_offsets: Some(offsets),
escaped: None,
escaped_offsets: None,
})
.expect("Data should be valid JSON."),
),
reset: None,
Expand All @@ -190,10 +180,12 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Hide<'a,
x: None,
y: None,
data: Some(
serde_json::to_value(HideData::Escaped(HideDataEscaped {
escaped: is_any_side_fully_clipped(&offsets),
escaped_offsets: offsets,
}))
serde_json::to_value(HideData {
reference_hidden: None,
reference_hidden_offsets: None,
escaped: Some(is_any_side_fully_clipped(&offsets)),
escaped_offsets: Some(offsets),
})
.expect("Data should be valid JSON."),
),
reset: None,
Expand Down
15 changes: 3 additions & 12 deletions packages/leptos/tests/visual/src/spec/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,9 @@ pub fn Hide() -> impl IntoView {
);

let hide_data = move || middleware_data().get_as::<HideData>(HIDE_NAME);
let reference_hidden = move || {
hide_data().map_or(false, |data| match data {
HideData::ReferenceHidden(data) => data.reference_hidden,
HideData::Escaped(_) => false,
})
};
let escaped = move || {
hide_data().map_or(false, |data| match data {
HideData::ReferenceHidden(_) => false,
HideData::Escaped(data) => data.escaped,
})
};
let reference_hidden =
move || hide_data().map_or(false, |data| data.reference_hidden.unwrap_or(false));
let escaped = move || hide_data().map_or(false, |data| data.escaped.unwrap_or(false));

let hierarchy_update = update.clone();

Expand Down

0 comments on commit 4a23e33

Please sign in to comment.