1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use crate::styles::{get_pallete, get_size, get_style, Palette, Size, Style};
use wasm_bindgen_test::*;
use yew::prelude::*;
use yew::{utils, App};

/// # Form Submit
///
/// ## Features required
///
/// forms
///
/// see example in Form
pub struct FormSubmit {
    props: Props,
}

#[derive(Clone, Properties)]
pub struct Props {
    /// Text of submit. Required
    pub value: String,
    /// Type submit style
    #[prop_or(Palette::Standard)]
    pub submit_palette: Palette,
    /// the submit style according with the purpose
    #[prop_or(Style::Regular)]
    pub submit_style: Style,
    /// the size of the submit
    #[prop_or(Size::Medium)]
    pub size: Size,
    /// Whether the form control is disabled
    #[prop_or(false)]
    pub disabled: bool,
    /// General property to get the ref of the component
    #[prop_or_default]
    pub code_ref: NodeRef,
    /// General property to add keys
    #[prop_or_default]
    pub key: String,
    /// general property to add custom class styles
    #[prop_or_default]
    pub class_name: String,
    /// general property to add custom id
    #[prop_or_default]
    pub id: String,
}

impl Component for FormSubmit {
    type Message = ();
    type Properties = Props;

    fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
        Self { props }
    }

    fn update(&mut self, _msg: Self::Message) -> ShouldRender {
        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.props = props;

        true
    }

    fn view(&self) -> Html {
        html! {
            <input
                type="submit"
                key=self.props.key.clone()
                ref=self.props.code_ref.clone()
                class=format!(
                    "form-submit {} {} {} {}",
                    get_style(self.props.submit_style.clone()),
                    get_pallete(self.props.submit_palette.clone()),
                    get_size(self.props.size.clone()),
                    self.props.class_name,
                ),
                disabled=self.props.disabled
                id=self.props.id
                value=self.props.value
            />
        }
    }
}

#[wasm_bindgen_test]
fn should_create_form_submit() {
    let props = Props {
        value: "submit".to_string(),
        disabled: false,
        key: "".to_string(),
        code_ref: NodeRef::default(),
        id: "result".to_string(),
        class_name: "form-submit-test".to_string(),
        submit_style: Style::Regular,
        submit_palette: Palette::Standard,
        size: Size::Medium,
    };

    let form_submit: App<FormSubmit> = App::new();

    form_submit.mount_with_props(
        utils::document().get_element_by_id("output").unwrap(),
        props,
    );

    let form_submit_element = utils::document().get_element_by_id("result").unwrap();

    assert_eq!(form_submit_element.tag_name(), "INPUT");
}