use serde::{Deserialize, Serialize};
use crate::node::{ActionId, Common, IntoNode, Key, Node};
use crate::text::Line;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Input {
#[serde(flatten)]
pub common: Common,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub placeholder: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub label: Option<Line>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub multiline: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rows: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub secret: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_length: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub disabled: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub on_submit: Option<ActionId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub on_change: Option<ActionId>,
}
impl Input {
#[must_use]
pub fn new(key: impl Into<Key>) -> Self {
let mut common = Common::new();
common.key = Some(key.into());
Self {
common,
value: None,
placeholder: None,
label: None,
multiline: None,
rows: None,
secret: None,
max_length: None,
disabled: None,
on_submit: None,
on_change: None,
}
}
#[must_use]
pub fn value(mut self, value: impl Into<String>) -> Self {
self.value = Some(value.into());
self
}
#[must_use]
pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = Some(placeholder.into());
self
}
#[must_use]
pub fn label(mut self, label: impl Into<Line>) -> Self {
self.label = Some(label.into());
self
}
#[must_use]
pub fn multiline(mut self, rows: u16) -> Self {
self.multiline = Some(true);
self.rows = Some(rows);
self
}
#[must_use]
pub fn secret(mut self, secret: bool) -> Self {
self.secret = Some(secret);
self
}
#[must_use]
pub fn max_length(mut self, max_length: u32) -> Self {
self.max_length = Some(max_length);
self
}
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = Some(disabled);
self
}
#[must_use]
pub fn on_submit(mut self, action: impl Into<ActionId>) -> Self {
self.on_submit = Some(action.into());
self
}
#[must_use]
pub fn on_change(mut self, action: impl Into<ActionId>) -> Self {
self.on_change = Some(action.into());
self
}
}
impl IntoNode for Input {}
impl From<Input> for Node {
fn from(i: Input) -> Self {
Node::Input(i)
}
}