Skip to content

Commit 56a04bf

Browse files
Samet195thedodd
authored andcommitted
Upgraded and adapted to yew 0.20
1 parent 755aa48 commit 56a04bf

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

Cargo.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ categories = ["wasm", "web-programming"]
1111
keywords = ["wasm", "web", "bulma", "sass", "yew"]
1212

1313
[dependencies]
14-
derive_more = { version="0.99.9", default-features=false, features=["display"] }
15-
web-sys = { version="0.3", features=["Element", "File", "HtmlCollection", "HtmlSelectElement"] }
16-
yew = "0.19"
17-
yew-agent = "0.1"
18-
yew-router = { version="0.16", optional=true }
19-
wasm-bindgen = "0.2"
20-
serde = "1"
14+
derive_more = { version = "0.99.17", default-features = false, features = [
15+
"display",
16+
] }
17+
web-sys = { version = "0.3.61", features = [
18+
"Element",
19+
"File",
20+
"HtmlCollection",
21+
"HtmlSelectElement",
22+
] }
23+
yew = { version = "0.20.0", features = ["csr"] }
24+
yew-agent = "0.2.0"
25+
yew-router = { version = "0.17.0", optional = true }
26+
wasm-bindgen = "0.2.84"
27+
serde = { version = "1.0.152", features = ["derive"] }
2128

2229
[features]
2330
default = ["router"]

src/components/modal.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::collections::HashSet;
2+
use std::rc::Rc;
23

4+
use serde::{Deserialize, Serialize};
35
use yew::prelude::*;
4-
use yew_agent::{Agent, AgentLink, Bridge, Bridged, HandlerId};
6+
use yew_agent::{Bridge, Bridged, HandlerId, Public, Worker, WorkerLink};
57

68
/// Modal actions.
79
pub enum ModalMsg {
810
Open,
911
Close,
10-
CloseFromAgent(ModalCloseMsg),
12+
CloseFromWorker(ModalCloseMsg),
1113
}
1214

1315
#[derive(Clone, Debug, Properties, PartialEq)]
@@ -41,8 +43,12 @@ impl Component for Modal {
4143
type Properties = ModalProps;
4244

4345
fn create(ctx: &Context<Self>) -> Self {
44-
let callback = ctx.link().callback(ModalMsg::CloseFromAgent);
45-
let subscription = ModalCloser::bridge(callback);
46+
let link = ctx.link().clone();
47+
48+
let subscription = ModalCloser::bridge(Rc::new(move |msg| {
49+
link.send_message(ModalMsg::CloseFromWorker(msg));
50+
}));
51+
4652
Self { subscription, is_active: false }
4753
}
4854

@@ -54,7 +60,7 @@ impl Component for Modal {
5460
ModalMsg::Open => {
5561
self.is_active = true;
5662
}
57-
ModalMsg::CloseFromAgent(id) => {
63+
ModalMsg::CloseFromWorker(id) => {
5864
if id.0 == ctx.props().id {
5965
self.is_active = false;
6066
} else {
@@ -130,8 +136,11 @@ impl Component for ModalCard {
130136
type Properties = ModalCardProps;
131137

132138
fn create(ctx: &Context<Self>) -> Self {
133-
let callback = ctx.link().callback(ModalMsg::CloseFromAgent);
134-
let subscription = ModalCloser::bridge(callback);
139+
let link = ctx.link().clone();
140+
141+
let subscription = ModalCloser::bridge(Rc::new(move |msg| {
142+
link.send_message(ModalMsg::CloseFromWorker(msg));
143+
}));
135144
Self { subscription, is_active: false }
136145
}
137146

@@ -143,7 +152,7 @@ impl Component for ModalCard {
143152
ModalMsg::Open => {
144153
self.is_active = true;
145154
}
146-
ModalMsg::CloseFromAgent(id) => {
155+
ModalMsg::CloseFromWorker(id) => {
147156
if id.0 == ctx.props().id {
148157
self.is_active = false;
149158
} else {
@@ -195,7 +204,7 @@ impl Component for ModalCard {
195204
///
196205
/// The ID provided in this message must match the ID of the modal which is to be closed, else
197206
/// the message will be ignored.
198-
#[derive(Clone, Debug)]
207+
#[derive(Clone, Debug, Deserialize, Serialize)]
199208
pub struct ModalCloseMsg(pub String);
200209

201210
/// An agent used for being able to close `Modal` & `ModalCard` instances by ID.
@@ -238,18 +247,19 @@ pub struct ModalCloseMsg(pub String);
238247
///
239248
/// This pattern allows you to communicate with a modal by its given ID, allowing
240249
/// you to close the modal from anywhere in your application.
250+
// #[derive(Serialize)]
241251
pub struct ModalCloser {
242-
link: AgentLink<Self>,
252+
link: WorkerLink<Self>,
243253
subscribers: HashSet<HandlerId>,
244254
}
245255

246-
impl Agent for ModalCloser {
247-
type Reach = yew_agent::Context<Self>;
256+
impl Worker for ModalCloser {
257+
type Reach = Public<Self>;
248258
type Message = ();
249259
type Input = ModalCloseMsg; // The agent receives requests to close modals by ID.
250260
type Output = ModalCloseMsg; // The agent forwards the input to all registered modals.
251261

252-
fn create(link: AgentLink<Self>) -> Self {
262+
fn create(link: WorkerLink<Self>) -> Self {
253263
Self { link, subscribers: HashSet::new() }
254264
}
255265

src/elements/button.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use derive_more::Display;
2-
use yew::events::{Event, FocusEvent, MouseEvent};
2+
use yew::events::{Event, MouseEvent};
33
use yew::prelude::*;
44

55
#[derive(Clone, Debug, Properties, PartialEq)]
@@ -244,7 +244,7 @@ pub struct ButtonInputSubmitProps {
244244
pub classes: Classes,
245245
/// The submit handler to use for this component.
246246
#[prop_or_default]
247-
pub onsubmit: Callback<FocusEvent>,
247+
pub onsubmit: Callback<SubmitEvent>,
248248
/// Render a loading spinner within this component.
249249
#[prop_or_default]
250250
pub loading: bool,

0 commit comments

Comments
 (0)