Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: button component #219

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crates/web/src/components/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use leptos::{

use townhall_client::Client;

use crate::components::button::{Button, ButtonType, ButtonVariant};
use crate::components::text_field::{TextField, TextFieldType};

#[component]
Expand Down Expand Up @@ -56,12 +57,10 @@ pub fn LoginCard(
placeholder="* * * * * * *"
value=password_value
/>
<button
type="submit"
class="bg-blue-700 text-white font-bold w-full mt-3 rounded-md py-3 px-4"
>

<Button r#type=ButtonType::Submit full_width=true>
Log in
</button>
</Button>
<Show when=move || error_getter.get().is_some()>
<div class="bg-rose-600 text-white p-2 rounded-md">
{error_getter.get().unwrap()}
Expand Down
11 changes: 6 additions & 5 deletions crates/web/src/components/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use leptos::{
use townhall_client::Client;
use townhall_types::user::Email;

use crate::components::button::{Button, ButtonType, ButtonVariant};
use crate::components::text_field::{TextField, TextFieldType};

#[component]
Expand Down Expand Up @@ -83,13 +84,13 @@ pub fn SignupCard(
placeholder="Password"
value=password_value
/>
<button
type="button"
on:click=move |_| submit.dispatch(())
class="bg-blue-700 text-white font-bold w-full mt-3 rounded-md py-3 px-4"
<Button
r#type=ButtonType::Button
on:click=move |_| submit.dispatch(())
full_width=true
>
Sign up
</button>
</Button>
<Show when=move || error_getter.get().is_some()>
<div class="bg-rose-600 text-white p-2 rounded-md">
{error_getter.get().unwrap()}
Expand Down
44 changes: 42 additions & 2 deletions crates/web/src/components/button.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::fmt;
use std::collections::HashSet;
use std::rc::Rc;

use leptos::web_sys::MouseEvent;
use leptos::{component, create_memo, view, Children, IntoView, MaybeProp, SignalGet, TextProp};

use super::helper::maybe_children::MaybeChildren;
Expand All @@ -12,13 +15,32 @@ pub enum ButtonVariant {
Outlined,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum ButtonType {
#[default]
Button,
Submit,
}

impl fmt::Display for ButtonType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ButtonType::Submit => write!(f, "submit"),
ButtonType::Button => write!(f, "button"),
}
}
}

#[component]
pub fn Button(
#[prop(optional, into)] id: TextProp,
#[prop(optional, into)] class: TextProp,
#[prop(optional, into)] disabled: MaybeProp<bool>,
#[prop(optional, into)] full_width: MaybeProp<bool>,
#[prop(optional, into)] variant: MaybeProp<ButtonVariant>,
#[prop(optional)] children: Option<Children>,
#[prop(optional, into)] on_click: MaybeProp<Rc<dyn Fn(MouseEvent) -> () + 'static>>,
#[prop(optional, into)] r#type: ButtonType,
) -> impl IntoView {
let custom_classes = class.get();
let class_names = create_memo(move |_| {
Expand All @@ -34,6 +56,12 @@ pub fn Button(
classes.insert("font-semibold");
classes.insert("text-sm");

if let Some(is_full_width) = full_width.get() {
if is_full_width {
classes.insert("w-full");
}
}

if let Some(is_disabled) = disabled.get() {
if is_disabled {
classes.insert("opacity-70");
Expand All @@ -48,7 +76,7 @@ pub fn Button(
}
ButtonVariant::Contained => {
classes.insert("btn-contained");
classes.insert("bg-purple-500");
classes.insert("bg-blue-700");
classes.insert("text-white");
}
ButtonVariant::Outlined => {
Expand All @@ -62,8 +90,20 @@ pub fn Button(
classes.into_iter().collect::<Vec<&str>>().join(" ")
});

let handle_click = move |event| {
if let Some(on_click) = on_click.get() {
on_click.clone()(event);
}
};

view! {
<button id=id class=class_names disabled=move || disabled.get()>
<button
type=format!("{}", r#type)
id=id
class=class_names
disabled=move || disabled.get()
on:click=handle_click
>
<MaybeChildren value=children let:children>
{children()}
</MaybeChildren>
Expand Down
Loading