🌐 AI搜索 & 代理 主页
Skip to content

Commit 0a01cc2

Browse files
authored
input with error (#1399)
1 parent 639fc9a commit 0a01cc2

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

pgml-dashboard/src/components/inputs/text/input/input.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
div[data-controller="inputs-text-input"] {
2+
--bs-danger: #{$peach-shade-100};
3+
24
span.material-symbols-outlined {
35
margin-left: -40px;
46
color: #{$slate-shade-100};
7+
8+
&.is-invalid {
9+
color: var(--bs-danger);
10+
}
511
}
612

713
input.form-control {
@@ -12,4 +18,10 @@ div[data-controller="inputs-text-input"] {
1218
label.form-label {
1319
font-weight: #{$font-weight-normal};
1420
}
21+
22+
p {
23+
small {
24+
color: var(--bs-danger);
25+
}
26+
}
1527
}

pgml-dashboard/src/components/inputs/text/input/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct Input {
1616
autocomplete: bool,
1717
value: String,
1818
required: bool,
19+
error: Option<String>,
1920
}
2021

2122
impl Input {
@@ -38,6 +39,7 @@ impl Input {
3839
autocomplete: false,
3940
value: "".to_string(),
4041
required: false,
42+
error: None,
4143
}
4244
}
4345

@@ -90,6 +92,11 @@ impl Input {
9092
self.required = true;
9193
self
9294
}
95+
96+
pub fn error(mut self, error: Option<impl ToString>) -> Self {
97+
self.error = error.map(|e| e.to_string());
98+
self
99+
}
93100
}
94101

95102
component!(Input);

pgml-dashboard/src/components/inputs/text/input/template.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<% let (input_classes, icon_classes) = if error.is_some() {
2+
("form-control is-invalid", "material-symbols-outlined is-invalid")
3+
} else {
4+
("form-control", "material-symbols-outlined")
5+
};
6+
%>
17
<div data-controller="inputs-text-input">
28
<% if let Some(label) = label { %>
39
<label class="form-label" for="<%= id %>"><%+ label %></label>
@@ -8,7 +14,7 @@
814
id="<%= id %>"
915
type="<%= type_ %>"
1016
name="<%= name %>"
11-
class="form-control"
17+
class="<%= input_classes %>"
1218
placeholder="<%= placeholder %>"
1319
data-action="<%= input_actions %>"
1420
autocomplete="<%= autocomplete %>"
@@ -20,10 +26,15 @@
2026

2127
<% if let Some(icon) = icon { %>
2228
<span
23-
class="material-symbols-outlined"
29+
class="<%= icon_classes %>"
2430
data-action="<%= icon_actions %>">
2531
<%= icon %>
2632
</span>
2733
<% } %>
2834
</div>
35+
<% if let Some(error) = error { %>
36+
<p class="mt-1 mb-0">
37+
<small><%= error %></small>
38+
</p>
39+
<% } %>
2940
</div>

pgml-dashboard/src/components/pages/demo/template.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
.type_("text") %>
5959
</div>
6060

61+
<div class="py-5">
62+
<%+ Input::new()
63+
.label("What is your name?".into())
64+
.icon("person")
65+
.placeholder("Enter your name")
66+
.name("name")
67+
.type_("text")
68+
.error(Some("Your name is not valid.")) %>
69+
</div>
70+
6171
<div class="py-5">
6272
<%+ Search::new(SearchOptions {
6373
name: "Model search".into(),

0 commit comments

Comments
 (0)