From 6b571ffe00eac6520ceb26bc76c7d96b0bb11c55 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 19 Oct 2023 14:57:43 +0530 Subject: [PATCH] Fix: Passing property to mutable component argument --- fastn-js/src/component_invocation.rs | 6 +- fastn-js/src/to_js.rs | 12 ++- ftd/src/js/utils.rs | 3 +- ftd/t/js/65-counter.ftd | 26 +++++ ftd/t/js/65-counter.html | 153 +++++++++++++++++++++++++++ 5 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 ftd/t/js/65-counter.ftd create mode 100644 ftd/t/js/65-counter.html diff --git a/fastn-js/src/component_invocation.rs b/fastn-js/src/component_invocation.rs index bb151c1efc..dbc4d291a5 100644 --- a/fastn-js/src/component_invocation.rs +++ b/fastn-js/src/component_invocation.rs @@ -44,7 +44,7 @@ pub enum ElementKind { #[derive(Debug)] pub struct InstantiateComponent { pub component: InstantiateComponentData, - pub arguments: Vec<(String, fastn_js::SetPropertyValue)>, + pub arguments: Vec<(String, fastn_js::SetPropertyValue, bool)>, pub parent: String, pub inherited: String, pub should_return: bool, @@ -66,7 +66,7 @@ pub enum InstantiateComponentData { impl InstantiateComponent { pub fn new( component_name: &str, - arguments: Vec<(String, fastn_js::SetPropertyValue)>, + arguments: Vec<(String, fastn_js::SetPropertyValue, bool)>, parent: &str, inherited: &str, should_return: bool, @@ -86,7 +86,7 @@ impl InstantiateComponent { pub fn new_with_definition( component_definition: fastn_js::SetPropertyValue, - arguments: Vec<(String, fastn_js::SetPropertyValue)>, + arguments: Vec<(String, fastn_js::SetPropertyValue, bool)>, parent: &str, inherited: &str, should_return: bool, diff --git a/fastn-js/src/to_js.rs b/fastn-js/src/to_js.rs index 806e641511..5ecf155587 100644 --- a/fastn-js/src/to_js.rs +++ b/fastn-js/src/to_js.rs @@ -259,8 +259,16 @@ impl fastn_js::InstantiateComponent { text("{") .append( pretty::RcDoc::intersperse( - self.arguments.iter().map(|(k, v)| { - format!("{}: {}", fastn_js::utils::name_to_js_(k), v.to_js()) + self.arguments.iter().map(|(k, value, is_mutable)| { + format!( + "{}: {}", + fastn_js::utils::name_to_js_(k), + if *is_mutable { + format!("fastn.mutable({})", value.to_js()) + } else { + value.to_js() + } + ) }), comma().append(space()), ) diff --git a/ftd/src/js/utils.rs b/ftd/src/js/utils.rs index 8ce8d7eb6f..05a606a535 100644 --- a/ftd/src/js/utils.rs +++ b/ftd/src/js/utils.rs @@ -257,7 +257,7 @@ pub(crate) fn get_set_property_values_for_provided_component_properties( component_properties: &[ftd::interpreter::Property], line_number: usize, has_rive_components: &mut bool, -) -> Option> { +) -> Option> { use itertools::Itertools; // Attempt to retrieve component or web component arguments @@ -280,6 +280,7 @@ pub(crate) fn get_set_property_values_for_provided_component_properties( has_rive_components, false, ), + v.mutable, ) }) }) diff --git a/ftd/t/js/65-counter.ftd b/ftd/t/js/65-counter.ftd new file mode 100644 index 0000000000..ee61fac90e --- /dev/null +++ b/ftd/t/js/65-counter.ftd @@ -0,0 +1,26 @@ +-- counter: +$count: 10 + +/-- counter: 10 + +-- component counter: +caption integer $count: 20 + +-- ftd.row: +border-width.px: 2 +padding.px: 20 +spacing.fixed.px: 20 +background.solid if { counter.count % 2 == 0 }: yellow +border-radius.px: 5 + +-- ftd.text: ➕ +$on-click$: $ftd.increment-by($a=$counter.count, v=1) + +-- ftd.integer: $counter.count + +-- ftd.text: ➖ +$on-click$: $ftd.increment-by($a=$counter.count, v=-1) + +-- end: ftd.row + +-- end: counter diff --git a/ftd/t/js/65-counter.html b/ftd/t/js/65-counter.html new file mode 100644 index 0000000000..bca154d7c2 --- /dev/null +++ b/ftd/t/js/65-counter.html @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + +
10
10
+ +