Skip to main content

oino_ui/extras/
input.rs

1//! `Input` node (§4.5.2).
2
3use serde::{Deserialize, Serialize};
4
5use crate::node::{ActionId, Common, IntoNode, Key, Node};
6use crate::text::Line;
7
8/// A text field (§4.5.2). Typed text is local state; `values` collects it on
9/// submission (§4.6).
10#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Input {
13    /// Common node props (`key` is required for `Input`).
14    #[serde(flatten)]
15    pub common: Common,
16    /// Initial value (also drives the field when it changes).
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub value: Option<String>,
19    /// Placeholder.
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub placeholder: Option<String>,
22    /// Field label.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub label: Option<Line>,
25    /// Multi-line (default `false`; intrinsic height `rows`, default 3).
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub multiline: Option<bool>,
28    /// Rows when `multiline`.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub rows: Option<u16>,
31    /// Mask the text.
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub secret: Option<bool>,
34    /// Maximum length.
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub max_length: Option<u32>,
37    /// Greyed out.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub disabled: Option<bool>,
40    /// Action on Enter (the string value).
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub on_submit: Option<ActionId>,
43    /// Action on change, debounced 150 ms (the string value).
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub on_change: Option<ActionId>,
46}
47
48impl Input {
49    /// An input with the given `key` (used to collect `values`, §4.6).
50    #[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    /// Sets the initial value.
70    #[must_use]
71    pub fn value(mut self, value: impl Into<String>) -> Self {
72        self.value = Some(value.into());
73        self
74    }
75
76    /// Sets the placeholder.
77    #[must_use]
78    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
79        self.placeholder = Some(placeholder.into());
80        self
81    }
82
83    /// Sets the label.
84    #[must_use]
85    pub fn label(mut self, label: impl Into<Line>) -> Self {
86        self.label = Some(label.into());
87        self
88    }
89
90    /// Multi-line with the given row count.
91    #[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    /// Masks the text.
99    #[must_use]
100    pub fn secret(mut self, secret: bool) -> Self {
101        self.secret = Some(secret);
102        self
103    }
104
105    /// Sets the maximum length.
106    #[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    /// Greys the field out.
113    #[must_use]
114    pub fn disabled(mut self, disabled: bool) -> Self {
115        self.disabled = Some(disabled);
116        self
117    }
118
119    /// Sets the submit action.
120    #[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    /// Sets the change action.
127    #[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}