use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::card::components::{
content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct DatePicker {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
required: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
disabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
placeholder: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
initial_date: Option<String>,
value: Value,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
}
impl Default for DatePicker {
fn default() -> Self {
Self {
tag: "date_picker".to_string(),
name: None,
required: None,
disabled: None,
placeholder: None,
width: None,
initial_date: None,
value: Value::Null,
confirm: None,
}
}
}
impl DatePicker {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
pub fn required(mut self, required: bool) -> Self {
self.required = Some(required);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = Some(disabled);
self
}
pub fn placeholder(mut self, placeholder: PlainText) -> Self {
self.placeholder = Some(placeholder);
self
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn initial_date(mut self, initial_date: &str) -> Self {
self.initial_date = Some(initial_date.to_string());
self
}
pub fn value(mut self, value: Value) -> Self {
self.value = value;
self
}
pub fn confirm(mut self, confirm: InputConfirm) -> Self {
self.confirm = Some(confirm);
self
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn test_date_picker() {
let date_picker = DatePicker::new()
.name("date_picker1")
.required(false)
.disabled(false)
.placeholder(PlainText::text("请选择"))
.width("default")
.initial_date("2024-01-01")
.value(json!({"key_1": "value_1"}))
.confirm(InputConfirm::new("title", "content"));
let json = json!({
"tag": "date_picker",
"name": "date_picker1", "required": false, "disabled": false, "width": "default", "initial_date": "2024-01-01", "placeholder": {
"tag": "plain_text",
"content": "请选择"
},
"value": {
"key_1": "value_1"
},
"confirm": {
"title": {
"tag": "plain_text",
"content": "title"
},
"text": {
"tag": "plain_text",
"content": "content"
}
}
});
assert_eq!(serde_json::to_value(&date_picker).unwrap(), json);
}
}