open_lark/card/components/containers/
form.rs1use serde::{Deserialize, Serialize};
2
3use crate::card::components::{interactive_components::input::InputConfirm, CardElement};
4
5#[derive(Debug, Serialize, Deserialize)]
7pub struct FormContainer {
8 tag: String,
10 name: String,
13 elements: Vec<CardElement>,
16 #[serde(skip_serializing_if = "Option::is_none")]
28 r#type: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
34 confirm: Option<InputConfirm>,
35}
36
37impl Default for FormContainer {
38 fn default() -> Self {
39 FormContainer {
40 tag: "form".to_string(),
41 name: "".to_string(),
42 elements: vec![],
43 r#type: None,
44 confirm: None,
45 }
46 }
47}
48
49impl FormContainer {
50 pub fn new() -> Self {
51 FormContainer::default()
52 }
53
54 pub fn name(mut self, name: &str) -> Self {
55 self.name = name.to_string();
56 self
57 }
58
59 pub fn elements(mut self, elements: Vec<CardElement>) -> Self {
60 self.elements = elements;
61 self
62 }
63
64 pub fn r#type(mut self, r#type: &str) -> Self {
65 self.r#type = Some(r#type.to_string());
66 self
67 }
68
69 pub fn confirm(mut self, confirm: InputConfirm) -> Self {
70 self.confirm = Some(confirm);
71 self
72 }
73}
74
75#[cfg(test)]
76mod test {
77 use serde_json::json;
78
79 use crate::card::components::{
80 content_components::plain_text::PlainText,
81 interactive_components::{button::FeishuCardButton, input::FeishuCardInput},
82 CardElement,
83 };
84
85 use super::*;
86
87 #[test]
88 fn test_form_container() {
89 let form = FormContainer::new().name("form_1").elements(vec![
90 CardElement::InputForm(FeishuCardInput::new().name("reason").required(true)),
91 CardElement::Button(
92 FeishuCardButton::new()
93 .action_type("form_submit")
94 .name("submit")
95 .r#type("primary")
96 .text(PlainText::text("提交").tag("lark_md")),
97 ),
98 ]);
99
100 let expect = json!( {
101 "tag": "form", "name": "form_1", "elements": [
104 {
105 "tag": "input", "name": "reason", "required": true },
109 {
110 "tag": "button", "action_type": "form_submit", "name": "submit", "text": { "content": "提交",
115 "tag": "lark_md"
116 },
117 "type": "primary", }
119 ]
120 });
121
122 assert_eq!(json!(form), expect);
123 }
124}