Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Form RS

Edit Component

Y Form RS Yew Usage

Adding Form RS to your project is simple:

  1. Make sure your project is set up with Yew. Follow their Getting Started Guide for setup instructions.

  2. Add the Form RS component to your dependencies by including it in your Cargo.toml file:

    cargo add form-rs --features=yew
    
  3. Import the form components into your Yew component and start using them in your app.

πŸ› οΈ Usage

Basic Login Form

use form_rs::yew::{Form, Control, FormLabel, Helper, Field};
use form_rs::{Method, ValidationState};
use yew::prelude::*;

#[function_component(LoginForm)]
pub fn login_form() -> Html {
    let email = use_state(String::new);
    let email_valid = use_state(|| true);

    html! {
        <Form method={Method::Post}>
            <Field
                id="email"
                label="Email"
                r#type="email"
                placeholder="ferris@opensass.org"
                helper_text="Enter a valid email."
                required={true}
                full_width={true}
                handle={email.clone()}
                valid_handle={email_valid.clone()}
                validate_function={Callback::from(|v: String| v.contains('@') && v.contains('.'))}
            />
        </Form>
    }
}

ARIA Validation with Helper Text

use form_rs::yew::{Form, Field};
use form_rs::{ValidationState};
use yew::prelude::*;

#[function_component(AriaForm)]
pub fn aria_form() -> Html {
    let val = use_state(String::new);
    let valid = use_state(|| true);

    html! {
        <Form>
            <Field
                id="username"
                label="Username"
                r#type="text"
                helper_text="3-20 characters."
                validation_state={ValidationState::Invalid("Too short".into())}
                handle={val.clone()}
                valid_handle={valid.clone()}
                validate_function={Callback::from(|v: String| v.len() >= 3)}
            />
        </Form>
    }
}

Group with Checkboxes

use form_rs::yew::{Form, Group, ControlLabel};
use form_rs::LabelPlacement;
use yew::prelude::*;

#[function_component(CheckboxGroup)]
pub fn checkbox_group() -> Html {
    html! {
        <Form>
            <Group>
                <ControlLabel
                    label="Email notifications"
                    label_placement={LabelPlacement::End}
                    control={html! { <input type="checkbox" /> }}
                />
                <ControlLabel
                    label="SMS notifications"
                    label_placement={LabelPlacement::End}
                    control={html! { <input type="checkbox" /> }}
                />
            </Group>
        </Form>
    }
}

πŸ”§ Props

Form

PropertyTypeDescriptionDefault
methodMethodHTTP submission method.Method::Get
action&'static strForm submission URL.""
enc_typeEncTypeEncoding type for submission.EncType::UrlEncoded
novalidateboolBypass browser native validation.false
autocomplete&'static strautocomplete attribute."off"
on_submitOption<Callback<SubmitEvent>>Submit handler.None
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""
id&'static strid attribute.""
aria_label&'static strAccessible label.""

Control

PropertyTypeDescriptionDefault
disabledboolDisables the control.false
errorboolShows error state.false
focusedboolShows focused state.false
full_widthboolExpands to full width.false
requiredboolMarks as required.false
variantVariantVisual style (Outlined, Filled, Standard).Outlined
colorColorAccent color.Primary
sizeSizeInput size (Small, Medium).Medium
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""

Field

PropertyTypeDescriptionDefault
id&'static strInput id and label for. Required.-
label&'static strLabel text.""
r#type&'static strInput type (text, email, password…)."text"
placeholder&'static strPlaceholder text.""
helper_text&'static strHelper message below the input.""
validation_stateValidationStateExternal validation result.None
requiredboolHTML required attribute.false
disabledboolDisables the input.false
full_widthboolExpands to full width.true
variantVariantVisual style.Outlined
colorColorAccent color.Primary
sizeSizeInput size.Medium
pattern&'static strRegex pattern for native validation.".*"
maxlengthOption<usize>Maximum character count.None
minlengthOption<usize>Minimum character count.None
handleUseStateHandle<String>Controlled value handle. Required.-
valid_handleUseStateHandle<bool>Validity state handle. Required.-
validate_functionCallback<String, bool>Custom validator. Required.-

FormLabel

PropertyTypeDescriptionDefault
html_for&'static strfor attribute linking to input id.""
errorboolShows error color.false
focusedboolShows focused color.false
requiredboolAppends required asterisk.false
disabledboolRenders in disabled style.false
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""

Helper

PropertyTypeDescriptionDefault
errorboolRenders in error color.false
validboolRenders in success color.false
disabledboolRenders in disabled style.false
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""

Group

PropertyTypeDescriptionDefault
rowboolLays children horizontally.false
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""

ControlLabel

PropertyTypeDescriptionDefault
labelHtmlLabel text or element. Required.-
label_placementLabelPlacementEnd, Start, Top, or Bottom.End
disabledboolRenders in disabled style.false
requiredboolAppends required asterisk.false
class&'static strExtra CSS classes.""
style&'static strExtra inline CSS.""

πŸ’‘ Notes

  • Field is a convenience composition of Control + FormLabel + Input + Helper. Use it for the common case.
  • Use Control + FormLabel + Helper manually when you need full layout control.
  • Set validation_state={ValidationState::Invalid("message".into())} to show server-side errors.
  • All components are WCAG 2.2 AA compliant, aria-describedby, aria-required, and aria-invalid are wired automatically.

πŸ”— See Also

Usage

CLI Usage

You can import Form RS directly into your project to customize and modify it using the opensass cli:

os add form-rs yew