Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce immutable string, array and map #2563

Merged
merged 40 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1ef3cc8
Add imut to Yew's dependencies
cecton Mar 3, 2022
c7b958d
Update example to use trunk
cecton Mar 31, 2022
a1ec56d
Merge commit 221b4dfa51e0383dec3262e773d70ab131e9d7c4 (no conflict)
cecton Mar 31, 2022
9f4937c
Merge commit 78d4204a9a52a826bd2be4e7383d29e04233fb5b (conflicts)
cecton Mar 31, 2022
272192d
Merge commit ea8a530454f2bd82637cff1f4494d37e4d93e95d (no conflict)
cecton Mar 31, 2022
46660ed
Update with most recent version of Yew
cecton Mar 31, 2022
8bea4e6
Revert changes to workflow
cecton Mar 31, 2022
741e0c5
Update README
cecton Mar 31, 2022
7ff2aa7
Update css
cecton Mar 31, 2022
ce9ef97
Merge commit e2405298c6bbb25c7d5695abb67a87fbfaa4f464 (no conflict)
cecton Apr 15, 2022
c4bac0f
Merge commit ae268855897d4bc75a57bf4d706f7fe3586586a2 (conflicts)
cecton Apr 15, 2022
2a1801f
Merge commit 73771562c22bcbbbda21e700aed0ae8f9dc5956b (no conflict)
cecton Apr 15, 2022
a616ca6
Merge commit e9b64e0a4511c7213fddd06af388d2e36b39f314 (conflicts)
cecton Apr 15, 2022
0d57621
Merge commit d2c3685521bdfd948e119c5b99e8da3f4a7597da (no conflict)
cecton Apr 15, 2022
126fb18
Renamed crate imut to implicit-clone
cecton Apr 15, 2022
6fbdf8c
Add website doc
cecton Apr 15, 2022
8f3ce15
Fix incorrect import
cecton Apr 15, 2022
b10f743
Doc update: rewording, typos, ...
cecton Apr 15, 2022
5d27f5b
Update yew-macro test's stderr
cecton Apr 15, 2022
edceb15
Update array example to use function component with use_state
cecton Apr 16, 2022
efb55e6
clippy
cecton Apr 16, 2022
ca1fab3
Update doc example to use functional component
cecton Apr 16, 2022
f07eeb6
Merge commit 12c9ebb0eaa2bcacb4c29b3bc9d3c4ac153fae73 (no conflict)
cecton Apr 24, 2022
dde9984
Merge commit c28a71e78e20e330ebc3fc40d3e74ec64bf6de20 (conflicts)
cecton Apr 24, 2022
2e093a0
Merge commit 2db4c81ad664d10ffd656f78481930b9395ce776 (no conflict)
cecton Apr 24, 2022
76595b7
cargo fmt
cecton Apr 24, 2022
f63be01
cargo fmt --all
cecton Apr 24, 2022
0bf27e3
Merge branch 'master' into immutable
cecton May 27, 2022
59b2538
Update website/docs/advanced-topics/immutable.mdx
cecton May 30, 2022
61fa8de
Update lib.rs
cecton May 30, 2022
3781b23
Update immutable.mdx
cecton May 30, 2022
af8df52
Fix imports
cecton May 30, 2022
8cd8539
Merge remote-tracking branch 'origin/master' into immutable
cecton Jun 16, 2022
8e58911
Update output error
cecton Jun 16, 2022
a9602c2
Update website/docs/advanced-topics/immutable.mdx
cecton Jun 20, 2022
7c22326
Remove map from implicit-clone features
cecton Jun 21, 2022
fb24ba2
Merge remote-tracking branch 'origin/master' into immutable
cecton Jun 21, 2022
e56b54b
Revert "Remove map from implicit-clone features"
cecton Jun 21, 2022
c3da26d
Merge branch 'master' into immutable
ranile Jun 24, 2022
1e4b52a
Fix tests
ranile Jun 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"examples/web_worker_fib",
"examples/ssr_router",
"examples/suspense",
"examples/immutable",

# Tools
"tools/benchmark-struct",
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ As an example, check out the TodoMVC example here: <https://examples.yew.rs/todo
| [function_todomvc](function_todomvc) | F | Implementation of [TodoMVC](http://todomvc.com/) using function components and hooks. |
| [futures](futures) | S | Demonstrates how you can use futures and async code with Yew. Features a Markdown renderer. |
| [game_of_life](game_of_life) | S | Implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) |
| [immutable](immutable) | SF | Using immutable types in components. |
| [inner_html](inner_html) | S | Embeds an external document as raw HTML by manually managing the element |
| [js_callback](js_callback) | F | Interacts with JavaScript code |
| [keyed_list](keyed_list) | S | Demonstrates how to use keys to improve the performance of lists |
Expand Down
12 changes: 12 additions & 0 deletions examples/immutable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "immutable"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
implicit-clone = { version = "0.2", features = ["map"] }
wasm-bindgen = "0.2"
web-sys = "0.3"
yew = { path = "../../packages/yew", features = ["csr"] }
5 changes: 5 additions & 0 deletions examples/immutable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Immutable Example

[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fimmutable)](https://examples.yew.rs/immutable)

This is a technical demonstration for how to use immutables types in Yew.
13 changes: 13 additions & 0 deletions examples/immutable/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>Yew • Immutable</title>
<link data-trunk rel="scss" href="index.scss"/>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@900&display=swap" rel="stylesheet">
</head>

<body></body>

</html>
11 changes: 11 additions & 0 deletions examples/immutable/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$font-stack: Roboto, sans-serif;
$primary-color: #f5f5f5;

body {
font: 100% $font-stack;
color: black;
background-color: $primary-color;
margin: 0 auto;
min-width: 230px;
max-width: 550px;
}
54 changes: 54 additions & 0 deletions examples/immutable/src/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use implicit_clone::unsync::*;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use web_sys::{HtmlInputElement, KeyboardEvent};
use yew::prelude::*;

#[derive(Properties, PartialEq)]
struct FolksViewProps {
folks: IArray<IString>,
}

#[function_component(FolksView)]
fn folks_view(props: &FolksViewProps) -> Html {
html! {
<>
<p>{"Hello to:"}</p>
<ul>
{ for props.folks.iter().map(|s| html!(<li>{s}</li>)) }
</ul>
</>
}
}

#[function_component(ArrayExample)]
pub fn array_example() -> Html {
let folks = use_state(IArray::<IString>::default);
let onkeyup = {
let folks = folks.clone();
Callback::from(move |e: KeyboardEvent| {
if e.key() == "Enter" {
let event: Event = e.dyn_into().unwrap_throw();
let event_target = event.target().unwrap_throw();
let target: HtmlInputElement = event_target.dyn_into().unwrap_throw();
let name = target.value();
target.set_value("");

folks.set(
folks
.iter()
.chain(std::iter::once(IString::from(name)))
.collect(),
);
}
})
};

html! {
<>
<h2>{"Input"}</h2>
<input {onkeyup} />
<h2>{"Output"}</h2>
<FolksView folks={&*folks} />
</>
}
}
29 changes: 29 additions & 0 deletions examples/immutable/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mod array;
mod map;
mod string;

use yew::prelude::*;

use self::array::*;
use self::map::*;
use self::string::*;

#[function_component]
fn App() -> Html {
html! {
<>
<h1>{ "IString Example" }</h1>
<StringExample />
<hr/>
<h1>{ "IArray Example" }</h1>
<ArrayExample />
<hr/>
<h1>{ "IMap Example" }</h1>
<MapExample />
</>
}
}

fn main() {
yew::Renderer::<App>::new().render();
}
83 changes: 83 additions & 0 deletions examples/immutable/src/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use implicit_clone::unsync::*;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use web_sys::{HtmlInputElement, KeyboardEvent};
use yew::prelude::*;

#[derive(Properties, PartialEq)]
struct DisplayProps {
values: IMap<u32, IString>,
}

#[function_component]
fn Display(props: &DisplayProps) -> Html {
html! {
<>
<p>{"Hello to:"}</p>
<ul>
{ for props.values.iter().map(|(i, s)| html!(<li>{i}{" => "}{s}</li>)) }
</ul>
</>
}
}

pub struct MapExample {
values: IMap<u32, IString>,
}

pub enum MapExampleMessage {
AddName(String),
Noop,
}

impl Component for MapExample {
type Message = MapExampleMessage;
type Properties = ();

fn create(_ctx: &Context<Self>) -> Self {
Self {
values: Default::default(),
}
}

fn update(&mut self, _: &Context<Self>, msg: Self::Message) -> bool {
match msg {
MapExampleMessage::AddName(name) => {
self.values = self
.values
.iter()
.chain(std::iter::once((
self.values.len() as u32,
IString::from(name),
)))
.collect();
true
}
MapExampleMessage::Noop => false,
}
}

fn view(&self, ctx: &Context<Self>) -> Html {
let link = ctx.link();
let onkeyup = link.callback(|e: KeyboardEvent| {
if e.key() == "Enter" {
let event: Event = e.dyn_into().unwrap_throw();
let event_target = event.target().unwrap_throw();
let target: HtmlInputElement = event_target.dyn_into().unwrap_throw();
let value = target.value();
target.set_value("");
MapExampleMessage::AddName(value)
} else {
MapExampleMessage::Noop
}
});

html! {
<>
<h2>{"Input"}</h2>
<input {onkeyup} />
<h2>{"Output"}</h2>
<Display values={&self.values} />
</>
}
}
}
63 changes: 63 additions & 0 deletions examples/immutable/src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use implicit_clone::unsync::*;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use web_sys::{HtmlInputElement, InputEvent};
use yew::prelude::*;

#[derive(Properties, PartialEq)]
struct DisplayProps {
name: IString,
}

#[function_component]
fn Display(props: &DisplayProps) -> Html {
html! {
<p>{"Hello "}{&props.name}{"!"}</p>
}
}

pub struct StringExample {
name: IString,
}

pub enum StringExampleMessage {
UpdateName(String),
}

impl Component for StringExample {
type Message = StringExampleMessage;
type Properties = ();

fn create(_ctx: &Context<Self>) -> Self {
Self {
name: "World".into(),
}
}

fn update(&mut self, _: &Context<Self>, msg: Self::Message) -> bool {
match msg {
StringExampleMessage::UpdateName(name) => {
self.name = name.into();
true
}
}
}

fn view(&self, ctx: &Context<Self>) -> Html {
let link = ctx.link();
let oninput = link.callback(|e: InputEvent| {
let event: Event = e.dyn_into().unwrap_throw();
let event_target = event.target().unwrap_throw();
let target: HtmlInputElement = event_target.dyn_into().unwrap_throw();
StringExampleMessage::UpdateName(target.value())
});

html! {
<>
<h2>{"Input"}</h2>
<input value={&self.name} {oninput} />
<h2>{"Output"}</h2>
<Display name={&self.name} />
</>
}
}
}
12 changes: 6 additions & 6 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| ^ the trait `IntoPropValue<String>` is not implemented for `{integer}`
|
= help: the following implementations were found:
<&'static str as IntoPropValue<AttrValue>>
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
<&'static str as IntoPropValue<Classes>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
and 18 others
and 22 others

error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfied
--> tests/html_macro/component-fail.rs:79:34
Expand All @@ -358,11 +358,11 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| ^ the trait `IntoPropValue<String>` is not implemented for `{integer}`
|
= help: the following implementations were found:
<&'static str as IntoPropValue<AttrValue>>
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
<&'static str as IntoPropValue<Classes>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
and 18 others
and 22 others

error[E0308]: mismatched types
--> tests/html_macro/component-fail.rs:80:31
Expand Down
Loading