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

simple multi-field form example #3684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
124 changes: 26 additions & 98 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/form/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "form"
version = "0.1.0"
authors = ["Kenzi Connor <git@knz.ai>"]
edition = "2021"
license = "Unlicense"

[dependencies]
js-sys = "0.3"
yew = { path = "../../packages/yew", features = ["csr"] }

[dependencies.web-sys]
version = "0.3.69"
features = ["FormData", "HtmlFormElement"]
13 changes: 13 additions & 0 deletions examples/form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Form Example

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

This example demonstrates using a form and FormData instead of accessing the fields individually other ways

## Running

Run this application with the trunk development server:

```bash
trunk serve --open
```
11 changes: 11 additions & 0 deletions examples/form/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yew • Form</title>

<link data-trunk rel="rust" />
</head>

<body></body>
</html>
59 changes: 59 additions & 0 deletions examples/form/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use web_sys::{FormData, HtmlFormElement};
use yew::prelude::*;

pub enum Msg {
Submit(SubmitEvent),
}

pub struct App {
names: Vec<String>,
}

impl Component for App {
type Message = Msg;
type Properties = ();

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

fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Submit(event) => {
event.prevent_default();
let form: HtmlFormElement = event.target_unchecked_into();
let form_data = FormData::new_with_form(&form).expect("form data");
self.names.push(format!(
"{} {}",
form_data.get("first").as_string().unwrap(),
form_data.get("last").as_string().unwrap()
));
}
}
true
}

fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<div>
<form onsubmit={ctx.link().callback(Msg::Submit)}>
<label>{"Sign up"}</label>
<input name="first" placeholder="First name"/>
<input name="last" placeholder="Last name"/>
<input type="submit"/>
</form>
<ul>
{ for self.names.iter().map( |name| html! {
<li>{ name }</li>
})}
</ul>
</div>
}
}
}

fn main() {
yew::Renderer::<App>::new().render();
}
Loading