Skip to content

Commit

Permalink
Make <Form/> action dynamic in exchange for it being required. This i…
Browse files Browse the repository at this point in the history
…s a pretty good trade, and more importantly it's needed to make forms work properly on routes using params without rerendering the entire form
  • Loading branch information
gbj committed Oct 21, 2022
1 parent 289a156 commit 9bf3e80
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions router/src/components/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ use leptos::*;
use typed_builder::TypedBuilder;
use wasm_bindgen::JsCast;

use crate::{use_navigate, use_resolved_path};
use crate::{use_navigate, use_resolved_path, ToHref};

#[derive(TypedBuilder)]
pub struct FormProps {
pub struct FormProps<A>
where
A: ToHref + 'static,
{
#[builder(default, setter(strip_option))]
method: Option<String>,
#[builder(default, setter(strip_option))]
action: Option<String>,
action: A,
#[builder(default, setter(strip_option))]
enctype: Option<String>,
children: Box<dyn Fn() -> Vec<Element>>,
}

#[allow(non_snake_case)]
pub fn Form(cx: Scope, props: FormProps) -> Element {
pub fn Form<A>(cx: Scope, props: FormProps<A>) -> Element
where
A: ToHref + 'static,
{
let FormProps {
method,
action,
enctype,
children,
} = props;

let action = use_resolved_path(cx, move || action.to_href()());

let on_submit = move |ev: web_sys::Event| {
if ev.default_prevented() {
return;
Expand Down

0 comments on commit 9bf3e80

Please sign in to comment.