open_lark/card/components/interactive_components/
picker_time.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::card::components::{
5 content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
6};
7
8#[derive(Debug, Serialize, Deserialize)]
10pub struct PickerTime {
11 tag: String,
13 #[serde(skip_serializing_if = "Option::is_none")]
17 name: Option<String>,
18 #[serde(skip_serializing_if = "Option::is_none")]
24 required: Option<bool>,
25 #[serde(skip_serializing_if = "Option::is_none")]
30 disabled: Option<bool>,
31 #[serde(skip_serializing_if = "Option::is_none")]
33 initial_time: Option<String>,
34 #[serde(skip_serializing_if = "Option::is_none")]
41 placeholder: Option<PlainText>,
42 #[serde(skip_serializing_if = "Option::is_none")]
48 width: Option<String>,
49 value: Value,
54 #[serde(skip_serializing_if = "Option::is_none")]
59 confirm: Option<InputConfirm>,
60}
61
62impl Default for PickerTime {
63 fn default() -> Self {
64 Self {
65 tag: "picker_time".to_string(),
66 name: None,
67 required: None,
68 disabled: None,
69 initial_time: None,
70 placeholder: None,
71 width: None,
72 value: Value::Null,
73 confirm: None,
74 }
75 }
76}
77
78impl PickerTime {
79 pub fn new() -> Self {
80 Self::default()
81 }
82
83 pub fn name(mut self, name: &str) -> Self {
84 self.name = Some(name.to_string());
85 self
86 }
87
88 pub fn required(mut self, required: bool) -> Self {
89 self.required = Some(required);
90 self
91 }
92
93 pub fn disabled(mut self, disabled: bool) -> Self {
94 self.disabled = Some(disabled);
95 self
96 }
97
98 pub fn initial_time(mut self, initial_time: &str) -> Self {
99 self.initial_time = Some(initial_time.to_string());
100 self
101 }
102
103 pub fn placeholder(mut self, placeholder: PlainText) -> Self {
104 self.placeholder = Some(placeholder);
105 self
106 }
107
108 pub fn width(mut self, width: &str) -> Self {
109 self.width = Some(width.to_string());
110 self
111 }
112
113 pub fn value(mut self, value: Value) -> Self {
114 self.value = value;
115 self
116 }
117
118 pub fn confirm(mut self, confirm: InputConfirm) -> Self {
119 self.confirm = Some(confirm);
120 self
121 }
122}
123
124#[cfg(test)]
125mod test {
126 use serde_json::json;
127
128 #[test]
129 fn test_time_selector() {
130 let picker_time = super::PickerTime::new()
131 .name("picker_time1")
132 .required(false)
133 .disabled(false)
134 .initial_time("11:30")
135 .placeholder(super::PlainText::text("请选择"))
136 .width("default")
137 .value(json!({ "key_1": "value_1"}))
138 .confirm(super::InputConfirm::new("title", "content"));
139
140 let json = json!({
141 "tag": "picker_time", "name": "picker_time1", "required": false, "disabled": false, "width": "default", "initial_time": "11:30", "placeholder": {
148 "tag": "plain_text",
150 "content": "请选择"
151 },
152 "value": {
153 "key_1": "value_1"
155 },
156 "confirm": {
157 "title": {
159 "tag": "plain_text",
160 "content": "title"
161 },
162 "text": {
163 "tag": "plain_text",
164 "content": "content"
165 }
166 }
167 });
168
169 assert_eq!(json!(picker_time), json);
170 }
171}