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

Commit aab5bd5

Browse files
authored
Editable header custom input actions (#1458)
1 parent b0538f3 commit aab5bd5

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
use crate::components::stimulus::stimulus_target::StimulusTarget;
1+
use crate::components::stimulus::{
2+
stimulus_action::{StimulusAction, StimulusActions},
3+
stimulus_target::StimulusTarget,
4+
};
25
use pgml_components::component;
36
use sailfish::TemplateOnce;
47
use std::fmt;
58

9+
use crate::utils::random_string;
10+
611
pub enum Headers {
712
H1,
813
H2,
@@ -32,17 +37,31 @@ pub struct EditableHeader {
3237
header_type: Headers,
3338
input_target: StimulusTarget,
3439
input_name: Option<String>,
40+
input_actions: StimulusActions,
3541
id: String,
3642
}
3743

3844
impl Default for EditableHeader {
3945
fn default() -> Self {
46+
let mut input_actions = StimulusActions::default();
47+
input_actions.push(
48+
StimulusAction::new_keydown_with_key("enter")
49+
.controller("inputs-text-editable-header")
50+
.method("blur"),
51+
);
52+
input_actions.push(
53+
StimulusAction::new_focusout()
54+
.controller("inputs-text-editable-header")
55+
.method("focusout"),
56+
);
57+
4058
Self {
41-
value: String::from("Title Goes Here"),
59+
value: String::from("Title goes here"),
4260
header_type: Headers::H3,
4361
input_target: StimulusTarget::new(),
4462
input_name: None,
45-
id: String::from(""),
63+
input_actions,
64+
id: random_string(12),
4665
}
4766
}
4867
}
@@ -72,6 +91,11 @@ impl EditableHeader {
7291
self
7392
}
7493

94+
pub fn input_action(mut self, input_action: StimulusAction) -> Self {
95+
self.input_actions.push(input_action);
96+
self
97+
}
98+
7599
pub fn id(mut self, id: &str) -> Self {
76100
self.id = id.to_string();
77101
self

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
<%= value %>
1010
</span>
1111

12-
<input type="text" class="form-control" value="<%= value %>" style="display: none" maxlength="50" autocomplete="off"
13-
name='<%= input_name.unwrap_or_else(|| "".to_string()) %>'
14-
data-inputs-text-editable-header-target="input"
15-
data-action="keydown.enter->inputs-text-editable-header#blur focusout->inputs-text-editable-header#focusout"
16-
<%- input_target %> >
12+
<input
13+
type="text"
14+
class="form-control"
15+
value="<%= value %>"
16+
style="display: none"
17+
maxlength="50"
18+
autocomplete="off"
19+
name="<%= input_name.unwrap_or_default() %>"
20+
data-inputs-text-editable-header-target="input"
21+
data-action="<%- input_actions %>"
22+
<%- input_target %>
23+
>
1724

1825
<div>
1926
<span class="material-symbols-outlined">

pgml-dashboard/src/components/stimulus/stimulus_action/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum StimulusEvents {
1313
FocusIn,
1414
KeyDown,
1515
KeyUp,
16+
KeyDownWithKey(String),
1617
}
1718

1819
impl fmt::Display for StimulusEvents {
@@ -27,6 +28,7 @@ impl fmt::Display for StimulusEvents {
2728
StimulusEvents::FocusIn => write!(f, "focusin"),
2829
StimulusEvents::KeyDown => write!(f, "keydown"),
2930
StimulusEvents::KeyUp => write!(f, "keyup"),
31+
StimulusEvents::KeyDownWithKey(ref key) => write!(f, "keydown.{}", key),
3032
}
3133
}
3234
}
@@ -45,6 +47,7 @@ impl FromStr for StimulusEvents {
4547
"focusin" => Ok(StimulusEvents::FocusIn),
4648
"keydown" => Ok(StimulusEvents::KeyDown),
4749
"keyup" => Ok(StimulusEvents::KeyUp),
50+
"keydown.enter" => Ok(StimulusEvents::KeyDownWithKey("enter".into())),
4851
_ => Err(()),
4952
}
5053
}
@@ -88,6 +91,14 @@ impl StimulusAction {
8891
pub fn new_input() -> Self {
8992
Self::new().action(StimulusEvents::Input)
9093
}
94+
95+
pub fn new_focusout() -> Self {
96+
Self::new().action(StimulusEvents::FocusOut)
97+
}
98+
99+
pub fn new_keydown_with_key(key: &str) -> Self {
100+
Self::new().action(StimulusEvents::KeyDownWithKey(key.into()))
101+
}
91102
}
92103

93104
impl fmt::Display for StimulusAction {

0 commit comments

Comments
 (0)