Skip to content

Commit a2cb05c

Browse files
feat(bootstrap): add input addon support to Dioxus (#430)
1 parent 13a4ff4 commit a2cb05c

3 files changed

Lines changed: 120 additions & 14 deletions

File tree

packages/styles/shield-bootstrap/src/dioxus.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod form;
22
mod input;
3+
mod input_addon;
34
mod method_form;
45

56
use dioxus::prelude::*;

packages/styles/shield-bootstrap/src/dioxus/input.rs

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use dioxus::prelude::*;
2-
use shield::{Input, InputValue};
2+
use shield::{Input, InputType, InputValue};
33
use shield_dioxus::Query;
44

5+
use crate::dioxus::input_addon::FormInputAddon;
6+
57
#[derive(Clone, PartialEq, Props)]
68
pub struct FormInputProps {
79
input: Input,
@@ -19,6 +21,83 @@ pub fn FormInput(props: FormInputProps) -> Element {
1921
origin.set(web_sys::window().and_then(|window| window.location().origin().ok()))
2022
});
2123

24+
let value = props.input.value.and_then(|value| match value {
25+
InputValue::Origin => origin(),
26+
InputValue::Query { key } => query.get(&key).cloned(),
27+
InputValue::String { value } => Some(value.clone()),
28+
});
29+
30+
let mut element = match props.input.r#type {
31+
InputType::Button(_) | InputType::Reset(_) | InputType::Submit(_) => {
32+
return rsx! {
33+
button {
34+
class: "btn btn-outline-primary d-flex align-items-center justify-content-center gap-1",
35+
name: props.input.name,
36+
type: match props.input.r#type {
37+
InputType::Reset(_) => "reset",
38+
InputType::Submit(_) => "submit",
39+
_ => "button"
40+
},
41+
42+
if let Some(addon) = props.input.addon_start {
43+
FormInputAddon {
44+
addon,
45+
group: false,
46+
}
47+
}
48+
49+
{value}
50+
51+
if let Some(addon) = props.input.addon_end {
52+
FormInputAddon {
53+
addon,
54+
group: false,
55+
}
56+
}
57+
}
58+
};
59+
}
60+
_ => {
61+
rsx! {
62+
input {
63+
class: "form-control",
64+
name: props.input.name,
65+
type: props.input.r#type.as_str(),
66+
value,
67+
placeholder: props.input.label.clone(),
68+
}
69+
}
70+
}
71+
};
72+
73+
if matches!(props.input.r#type, InputType::Hidden(_)) {
74+
return element;
75+
}
76+
77+
if props.input.addon_start.is_some() || props.input.addon_end.is_some() {
78+
element = rsx! {
79+
div {
80+
class: "input-group",
81+
82+
if let Some(addon) = props.input.addon_start {
83+
FormInputAddon {
84+
addon,
85+
group: true,
86+
}
87+
}
88+
89+
{ element }
90+
91+
if let Some(addon) = props.input.addon_end {
92+
FormInputAddon {
93+
addon,
94+
group: true,
95+
}
96+
}
97+
}
98+
}
99+
}
100+
22101
rsx! {
23102
div {
24103
class: "mb-3",
@@ -33,19 +112,7 @@ pub fn FormInput(props: FormInputProps) -> Element {
33112
}
34113
}
35114

36-
input {
37-
class: "form-control",
38-
name: props.input.name,
39-
type: props.input.r#type.as_str(),
40-
value: props.input.value.map(|value| match value {
41-
InputValue::Origin => origin(),
42-
InputValue::Query { key } => {
43-
query.get(&key).cloned()
44-
},
45-
InputValue::String { value } => Some(value.clone()),
46-
}),
47-
placeholder: props.input.label,
48-
}
115+
{element}
49116
}
50117
}
51118
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use dioxus::prelude::*;
2+
use shield::InputAddon;
3+
4+
#[derive(Clone, PartialEq, Props)]
5+
pub struct FormInputAddonProps {
6+
addon: InputAddon,
7+
group: bool,
8+
}
9+
10+
#[component]
11+
pub fn FormInputAddon(props: FormInputAddonProps) -> Element {
12+
match props.addon {
13+
InputAddon::Image { alt, src } => rsx! {
14+
img {
15+
src,
16+
alt,
17+
width: 16,
18+
height: 16,
19+
}
20+
},
21+
InputAddon::Text { text } => {
22+
if props.group {
23+
rsx! {
24+
span {
25+
class: "input-group-text",
26+
"{text}"
27+
}
28+
}
29+
} else {
30+
rsx! {
31+
span {
32+
"{text}"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)