Struct Input

Source
pub struct Input { /* private fields */ }
Expand description

custom_input_component A custom input component that handles user input and validation.

§Arguments

  • props - The properties of the component.
    • input_valid_handle - A handle to track the validity of the input.
    • aria_invalid - A string representing the ‘aria-invalid’ attribute value for accessibility. Defaults to “true”.
    • aria_required - A string representing the ‘aria-required’ attribute value for accessibility. Defaults to “true”.
    • input_type - The type of the input element. Defaults to “text”.
    • input_ref - A reference to the input element.
    • input_handle - A handle to set the value of the input.
    • validate_function - A callback function to validate the input value.

§Returns

(Html): An HTML representation of the input component.

§Examples

// Example of using the custom_input_component
use regex::Regex;
use serde::{Deserialize, Serialize};
use input_yew::CustomInput;
use yew::prelude::*;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct LoginUserSchema {
    email: String,
    password: String,
}

fn validate_email(email: String) -> bool {
    let pattern = Regex::new(r"^[^ ]+@[^ ]+\.[a-z]{2,3}$").unwrap();
    pattern.is_match(&email)
}

fn validate_password(password: String) -> bool {
    !&password.is_empty()
}

#[function_component(LoginFormOne)]
pub fn login_form_one() -> Html {
    let error_handle = use_state(String::default);
    let error = (*error_handle).clone();;

    let email_valid_handle = use_state(|| true);
    let email_valid = (*email_valid_handle).clone();;

    let password_valid_handle = use_state(|| true);
    let password_valid = (*password_valid_handle).clone();;

    let input_email_ref = use_node_ref();
    let input_email_handle = use_state(String::default);
    let input_email = (*input_email_handle).clone();;

    let input_password_ref = use_node_ref();
    let input_password_handle = use_state(String::default);
    let input_password = (*input_password_handle).clone();;

    let onsubmit = Callback::from(move |event: SubmitEvent| {
        event.prevent_default();

        let email_ref = input_password.clone();
        let password_ref = input_password.clone();
        let error_handle = error_handle.clone();

        // Custom logic for your endpoint goes here: `spawn_local`
    });

    html! {
        <div class="form-one-content" role="main" aria-label="Sign In Form">
          <div class="text">
            <h2>{"Sign In"}</h2>
            if !error.is_empty() {
              <div class="error">{error}</div>
            }
          </div>
          <form action="#" aria-label="Sign In Form" onsubmit={onsubmit}>
              <CustomInput
                input_type={"text"}
                input_handle={input_email_handle}
                name={"email"}
                input_ref={input_email_ref}
                input_placeholder={"Email"}
                icon_class={"fas fa-user"}
                error_message={"Enter a valid email address"}
                form_input_field_class={"form-one-field"}
                form_input_error_class={"error-txt"}
                required={true}
                input_valid_handle={email_valid_handle}
                validate_function={validate_email}
              />
              <CustomInput
                input_type={"password"}
                input_handle={input_password_handle}
                name={"password"}
                input_ref={input_password_ref}
                input_placeholder={"Password"}
                icon_class={"fas fa-lock"}
                error_message={"Password can't be blank!"}
                form_input_field_class={"form-one-field"}
                form_input_error_class={"error-txt"}
                required={true}
                input_valid_handle={password_valid_handle}
                validate_function={validate_password}
                eye_active={"fa fa-eye"}
                eye_disabled={"fa fa-eye-slash"}
              />
            <div class="form-one-forgot-pass">
              <a href="#" aria-label="Forgot Password?">{"Forgot Password?"}</a>
            </div>
            <button type="submit">{"Sign in"}</button>
            <div class="sign-up">
              {"Not a member?"}
              <a href="#" aria-label="Sign up now">{"Sign up now"}</a>
            </div>
          </form>
        </div>
    }
}

Trait Implementations§

Source§

impl BaseComponent for CustomInput
where CustomInput: 'static,

Source§

type Message = ()

The Component’s Message.
Source§

type Properties = Props

The Component’s Properties.
Source§

fn create(ctx: &Context<CustomInput>) -> CustomInput

Creates a component.
Source§

fn update( &mut self, _ctx: &Context<CustomInput>, _msg: <CustomInput as BaseComponent>::Message, ) -> bool

Updates component’s internal state.
Source§

fn changed( &mut self, _ctx: &Context<CustomInput>, _old_props: &<CustomInput as BaseComponent>::Properties, ) -> bool

React to changes of component properties.
Source§

fn view(&self, ctx: &Context<CustomInput>) -> Result<VNode, RenderError>

Returns a component layout to be rendered.
Source§

fn rendered(&mut self, _ctx: &Context<CustomInput>, _first_render: bool)

Notified after a layout is rendered.
Source§

fn destroy(&mut self, _ctx: &Context<CustomInput>)

Notified before a component is destroyed.
Source§

fn prepare_state(&self) -> Option<String>

Prepares the server-side state.
Source§

impl Debug for CustomInput

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FunctionProvider for CustomInput

Source§

type Properties = Props

Properties for the Function Component.
Source§

fn run( ctx: &mut HookContext, props: &<CustomInput as FunctionProvider>::Properties, ) -> Result<VNode, RenderError>

Render the component. This function returns the Html to be rendered for the component. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

Source§

impl<T> HasAllProps<(), T> for T