open_lark/card/components/interactive_components/
select_static.rs1use serde::{Deserialize, Serialize};
2
3use crate::card::{
4 components::{
5 content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
6 },
7 icon::FeishuCardTextIcon,
8};
9
10#[derive(Debug, Serialize, Deserialize)]
12pub struct SelectStatic {
13 tag: String,
15 #[serde(skip_serializing_if = "Option::is_none")]
20 r#type: Option<String>,
21 #[serde(skip_serializing_if = "Option::is_none")]
26 name: Option<String>,
27 #[serde(skip_serializing_if = "Option::is_none")]
35 required: Option<bool>,
36 #[serde(skip_serializing_if = "Option::is_none")]
41 disabled: Option<bool>,
42 #[serde(skip_serializing_if = "Option::is_none")]
44 initial_index: Option<u32>,
45 #[serde(skip_serializing_if = "Option::is_none")]
47 placeholder: Option<PlainText>,
48 #[serde(skip_serializing_if = "Option::is_none")]
54 width: Option<String>,
55 #[serde(skip_serializing_if = "Option::is_none")]
57 options: Option<Vec<SelectStaticOption>>,
58 #[serde(skip_serializing_if = "Option::is_none")]
63 confirm: Option<InputConfirm>,
64}
65
66impl Default for SelectStatic {
67 fn default() -> Self {
68 Self {
69 tag: "select_static".to_string(),
70 r#type: None,
71 name: None,
72 required: None,
73 disabled: None,
74 initial_index: None,
75 placeholder: None,
76 width: None,
77 options: None,
78 confirm: None,
79 }
80 }
81}
82
83impl SelectStatic {
84 pub fn new() -> Self {
85 Self::default()
86 }
87
88 pub fn r#type(mut self, r#type: &str) -> Self {
89 self.r#type = Some(r#type.to_string());
90 self
91 }
92
93 pub fn name(mut self, name: &str) -> Self {
94 self.name = Some(name.to_string());
95 self
96 }
97
98 pub fn required(mut self, required: bool) -> Self {
99 self.required = Some(required);
100 self
101 }
102
103 pub fn disabled(mut self, disabled: bool) -> Self {
104 self.disabled = Some(disabled);
105 self
106 }
107
108 pub fn initial_index(mut self, initial_index: u32) -> Self {
109 self.initial_index = Some(initial_index);
110 self
111 }
112
113 pub fn placeholder(mut self, placeholder: PlainText) -> Self {
114 self.placeholder = Some(placeholder);
115 self
116 }
117
118 pub fn width(mut self, width: &str) -> Self {
119 self.width = Some(width.to_string());
120 self
121 }
122
123 pub fn options(mut self, options: Vec<SelectStaticOption>) -> Self {
124 self.options = Some(options);
125 self
126 }
127
128 pub fn confirm(mut self, confirm: InputConfirm) -> Self {
129 self.confirm = Some(confirm);
130 self
131 }
132}
133
134#[derive(Debug, Serialize, Deserialize, Default)]
136pub struct SelectStaticOption {
137 text: PlainText,
139 #[serde(skip_serializing_if = "Option::is_none")]
141 icon: Option<FeishuCardTextIcon>,
142 value: String,
147}
148
149impl SelectStaticOption {
150 pub fn new(text: &str, value: &str) -> Self {
151 Self {
152 text: PlainText::text(text),
153 icon: None,
154 value: value.to_string(),
155 }
156 }
157
158 pub fn text(mut self, text: PlainText) -> Self {
159 self.text = text;
160 self
161 }
162
163 pub fn icon(mut self, icon: FeishuCardTextIcon) -> Self {
164 self.icon = Some(icon);
165 self
166 }
167
168 pub fn value(mut self, value: &str) -> Self {
169 self.value = value.to_string();
170 self
171 }
172}
173
174#[cfg(test)]
175mod test {
176 use serde_json::json;
177
178 use crate::card::{
179 components::{
180 content_components::plain_text::PlainText,
181 interactive_components::{
182 input::InputConfirm,
183 select_static::{SelectStatic, SelectStaticOption},
184 },
185 },
186 icon::FeishuCardTextIcon,
187 };
188
189 #[test]
190 fn test_select_static() {
191 let select_static = SelectStatic::new()
192 .r#type("text")
193 .name("select_static1")
194 .required(false)
195 .disabled(false)
196 .initial_index(1)
197 .placeholder(PlainText::text("默认提示文本"))
198 .width("default")
199 .options(vec![SelectStaticOption::new("我是交互组件", "selectDemo1")
200 .icon(
201 FeishuCardTextIcon::new()
202 .token("chat-forbidden_outlined")
203 .color("orange")
204 .img_key("img_v2_38811724"),
205 )])
206 .confirm(InputConfirm::new("弹窗标题", "弹窗正文文案"));
207
208 let json = json!({
209 "tag": "select_static", "type": "text", "name": "select_static1", "required": false, "disabled": false, "initial_index": 1, "placeholder": {
216 "tag": "plain_text",
218 "content": "默认提示文本"
219 },
220 "width": "default", "options": [
222 {
224 "text": {
225 "tag": "plain_text",
227 "content": "我是交互组件"
228 },
229 "icon": {
230 "tag": "standard_icon", "token": "chat-forbidden_outlined", "color": "orange", "img_key": "img_v2_38811724" },
236 "value": "selectDemo1" }
238 ],
239 "confirm": {
240 "title": {
242 "tag": "plain_text",
243 "content": "弹窗标题"
244 },
245 "text": {
246 "tag": "plain_text",
247 "content": "弹窗正文文案"
248 }
249 }
250 });
251
252 assert_eq!(serde_json::to_value(&select_static).unwrap(), json);
253 }
254}