feat: implement IntoProperty for Oco (#4174)

This commit is contained in:
Gabriel Lopes Veiga
2025-08-09 16:34:51 -03:00
committed by GitHub
parent 8f623a2d5b
commit 30141293f6

View File

@@ -2,6 +2,7 @@ use crate::{
html::{
attribute::{any_attribute::AnyAttribute, AttributeValue},
class::IntoClass,
property::IntoProperty,
style::IntoStyle,
},
hydration::Cursor,
@@ -11,6 +12,7 @@ use crate::{
view::{strings::StrState, Position, PositionState, ToTemplate},
};
use oco_ref::Oco;
use wasm_bindgen::JsValue;
/// Retained view state for [`Oco`].
pub struct OcoStrState {
@@ -248,6 +250,47 @@ impl IntoClass for Oco<'static, str> {
}
}
impl IntoProperty for Oco<'static, str> {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Self;
type CloneableOwned = Self;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from_str(self.as_ref());
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from_str(self.as_ref());
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = JsValue::from_str(self.as_ref());
Rndr::set_property(el, key, &value);
*prev = value;
}
fn into_cloneable(self) -> Self::Cloneable {
self
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
self
}
}
impl IntoStyle for Oco<'static, str> {
type AsyncOutput = Self;
type State = (crate::renderer::types::Element, Self);