1use serde::{Deserialize, Serialize};
4
5use crate::node::{ActionId, Common, IntoNode, Key, Node};
6use crate::text::Line;
7
8#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Input {
13 #[serde(flatten)]
15 pub common: Common,
16 #[serde(default, skip_serializing_if = "Option::is_none")]
18 pub value: Option<String>,
19 #[serde(default, skip_serializing_if = "Option::is_none")]
21 pub placeholder: Option<String>,
22 #[serde(default, skip_serializing_if = "Option::is_none")]
24 pub label: Option<Line>,
25 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub multiline: Option<bool>,
28 #[serde(default, skip_serializing_if = "Option::is_none")]
30 pub rows: Option<u16>,
31 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub secret: Option<bool>,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub max_length: Option<u32>,
37 #[serde(default, skip_serializing_if = "Option::is_none")]
39 pub disabled: Option<bool>,
40 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub on_submit: Option<ActionId>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub on_change: Option<ActionId>,
46}
47
48impl Input {
49 #[must_use]
51 pub fn new(key: impl Into<Key>) -> Self {
52 let mut common = Common::new();
53 common.key = Some(key.into());
54 Self {
55 common,
56 value: None,
57 placeholder: None,
58 label: None,
59 multiline: None,
60 rows: None,
61 secret: None,
62 max_length: None,
63 disabled: None,
64 on_submit: None,
65 on_change: None,
66 }
67 }
68
69 #[must_use]
71 pub fn value(mut self, value: impl Into<String>) -> Self {
72 self.value = Some(value.into());
73 self
74 }
75
76 #[must_use]
78 pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
79 self.placeholder = Some(placeholder.into());
80 self
81 }
82
83 #[must_use]
85 pub fn label(mut self, label: impl Into<Line>) -> Self {
86 self.label = Some(label.into());
87 self
88 }
89
90 #[must_use]
92 pub fn multiline(mut self, rows: u16) -> Self {
93 self.multiline = Some(true);
94 self.rows = Some(rows);
95 self
96 }
97
98 #[must_use]
100 pub fn secret(mut self, secret: bool) -> Self {
101 self.secret = Some(secret);
102 self
103 }
104
105 #[must_use]
107 pub fn max_length(mut self, max_length: u32) -> Self {
108 self.max_length = Some(max_length);
109 self
110 }
111
112 #[must_use]
114 pub fn disabled(mut self, disabled: bool) -> Self {
115 self.disabled = Some(disabled);
116 self
117 }
118
119 #[must_use]
121 pub fn on_submit(mut self, action: impl Into<ActionId>) -> Self {
122 self.on_submit = Some(action.into());
123 self
124 }
125
126 #[must_use]
128 pub fn on_change(mut self, action: impl Into<ActionId>) -> Self {
129 self.on_change = Some(action.into());
130 self
131 }
132}
133
134impl IntoNode for Input {}
135impl From<Input> for Node {
136 fn from(i: Input) -> Self {
137 Node::Input(i)
138 }
139}