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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
use crate::util::Color;
use yew::prelude::*;
#[derive(Clone, PartialEq, Eq)]
pub enum ButtonSize {
Large,
Normal,
Small,
}
impl Default for ButtonSize {
fn default() -> Self {
ButtonSize::Normal
}
}
/// # Button component
/// Button with various properties, including support for opening or closing a modal
/// dialog [crate::component::Modal].
///
/// Buttons can be grouped in a [crate::component::ButtonGroup].
///
/// See [ButtonProps] for a listing of properties.
///
/// ## Example
/// Example of a simple button:
///
/// ```rust
/// use yew::prelude::*;
/// use yew_bootstrap::component::Button;
/// use yew_bootstrap::util::Color;
/// fn test() -> Html {
/// html!{
/// <Button style={Color::Primary} text={ "Button text" }/>
/// }
/// }
/// ```
///
/// A button can be linked to a [crate::component::Modal] dialog or
/// close this modal.
///
/// ```rust
/// use yew::prelude::*;
/// use yew_bootstrap::component::Button;
/// use yew_bootstrap::component::Modal;
/// use yew_bootstrap::util::Color;
/// fn test() -> Html {
/// html ! {
/// <>
/// <Modal id="ExampleModal">
/// <Button modal_dismiss={true}>{ "Close the modal" }</Button>
/// </Modal>
/// <Button style={Color::Primary} modal_target={ "ExampleModal" }>
/// { "Open Modal" }
/// </Button>
/// </>
/// }
/// }
/// ```
pub struct Button {}
/// # Properties for [Button]
#[derive(Properties, Clone, PartialEq)]
pub struct ButtonProps {
/// CSS class
#[prop_or_default]
pub class: String,
/// Optional children
#[prop_or_default]
pub children: Children,
/// Treat button as block that spans the full width of the parent
#[prop_or_default]
pub block: bool,
/// Status of the button. Disabled buttons cannot be clicked.
#[prop_or_default]
pub disabled: bool,
/// Name of the component
#[prop_or_default]
pub name: String,
/// Event called when the button is clicked
#[prop_or_default]
pub onclick: Callback<MouseEvent>,
/// Show button as outlined instead of filled
#[prop_or_default]
pub outline: bool,
/// Size of the button
#[prop_or_default]
pub size: ButtonSize,
/// Color of the button, default [Color::Primary]
#[prop_or(Color::Primary)]
pub style: Color,
/// Text displayed in the button
#[prop_or_default]
pub text: String,
/// if provided, we will set data-bs-toggle and data-bs-target for modal opening
#[prop_or_default]
pub modal_target: Option<String>,
/// true if this button dismisses the modal that contains it
#[prop_or_default]
pub modal_dismiss: bool,
}
impl Component for Button {
type Message = ();
type Properties = ButtonProps;
fn create(_ctx: &Context<Self>) -> Self {
Self {}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
let mut classes = Classes::new();
classes.push("btn");
if props.outline {
classes.push(format!("btn-outline-{}", props.style));
} else {
classes.push(format!("btn-{}", props.style));
}
match props.size {
ButtonSize::Large => classes.push("btn-lg"),
ButtonSize::Small => classes.push("btn-sm"),
_ => (),
}
if props.block {
classes.push("btn-block");
}
classes.push(props.class.clone());
let modal_dismiss = match props.modal_dismiss {
true => "modal",
false => "",
};
if let Some(target) = &props.modal_target {
html! {
<button
class={classes}
disabled={props.disabled}
name={props.name.clone()}
onclick={props.onclick.clone()}
data-bs-toggle="modal"
data-bs-target={format!("#{}",target.clone())}
>
{ &props.text }
{ for props.children.iter() }
</button>
}
} else {
html! {
<button
class={classes}
disabled={props.disabled}
name={props.name.clone()}
onclick={props.onclick.clone()}
data-bs-dismiss={modal_dismiss}
>
{ &props.text }
{ for props.children.iter() }
</button>
}
}
}
}