open_lark/card/components/interactive_components/
overflow.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::card::{
5    components::{
6        content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
7    },
8    href::FeishuCardHrefVal,
9};
10
11/// 折叠按钮组
12#[derive(Debug, Serialize, Deserialize)]
13pub struct FeishuCardOverflow {
14    /// 折叠按钮组的标签。固定值为 overflow。
15    tag: String,
16    /// 折叠按钮组的宽度。支持以下枚举值:
17    ///
18    /// - default:默认宽度
19    /// - fill:卡片最大支持宽度
20    /// - [100,∞)px:自定义宽度。超出卡片宽度时将按最大支持宽度展示
21    #[serde(skip_serializing_if = "Option::is_none")]
22    width: Option<String>,
23    /// 折叠按钮组当中的选项按钮。详见下文 options 字段说明。
24    options: Vec<OverflowOption>,
25    /// 组件整体的回调数据。当用户点击折叠按钮组的折叠按钮后,会将 value
26    /// 的值返回给接收回调数据的服务器。后续你可以通过服务器接收的 value 值进行业务处理。
27    #[serde(skip_serializing_if = "Option::is_none")]
28    value: Option<Value>,
29    /// 二次确认弹窗配置。指在用户提交时弹出二次确认弹窗提示;只有用户点击确认后,
30    /// 才提交输入的内容。该字段默认提供了确认和取消按钮,你只需要配置弹窗的标题与内容即可。
31    ///
32    /// 注意:confirm 字段仅在用户点击包含提交属性的按钮时才会触发二次确认弹窗。
33    #[serde(skip_serializing_if = "Option::is_none")]
34    confirm: Option<InputConfirm>,
35}
36
37impl Default for FeishuCardOverflow {
38    fn default() -> Self {
39        Self {
40            tag: "overflow".to_string(),
41            width: None,
42            options: vec![],
43            value: None,
44            confirm: None,
45        }
46    }
47}
48
49impl FeishuCardOverflow {
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    pub fn width(mut self, width: &str) -> Self {
55        self.width = Some(width.to_string());
56        self
57    }
58
59    pub fn options(mut self, options: Vec<OverflowOption>) -> Self {
60        self.options = options;
61        self
62    }
63
64    pub fn value(mut self, value: Value) -> Self {
65        self.value = Some(value);
66        self
67    }
68
69    pub fn confirm(mut self, confirm: InputConfirm) -> Self {
70        self.confirm = Some(confirm);
71        self
72    }
73
74    pub fn add_option(mut self, option: OverflowOption) -> Self {
75        self.options.push(option);
76        self
77    }
78}
79
80/// options 字段说明
81#[derive(Debug, Serialize, Deserialize, Default)]
82pub struct OverflowOption {
83    /// 按钮上的文本。
84    text: Option<PlainText>,
85    /// 为按钮添加多端的跳转链接。
86    multi_url: Option<FeishuCardHrefVal>,
87    /// 该按钮的回传参数值。当用户点击选项后,应用会将该值返回至卡片请求地址。
88    value: Option<String>,
89}
90
91impl OverflowOption {
92    pub fn new() -> Self {
93        Self::default()
94    }
95
96    pub fn text(mut self, text: PlainText) -> Self {
97        self.text = Some(text);
98        self
99    }
100
101    pub fn multi_url(mut self, multi_url: FeishuCardHrefVal) -> Self {
102        self.multi_url = Some(multi_url);
103        self
104    }
105
106    pub fn value(mut self, value: impl ToString) -> Self {
107        self.value = Some(value.to_string());
108        self
109    }
110}
111
112#[cfg(test)]
113mod test {
114    use serde_json::json;
115
116    use crate::card::{
117        components::{
118            content_components::plain_text::PlainText,
119            interactive_components::{
120                input::InputConfirm,
121                overflow::{FeishuCardOverflow, OverflowOption},
122            },
123        },
124        href::FeishuCardHrefVal,
125    };
126
127    #[test]
128    fn test_overflow() {
129        let overflow = FeishuCardOverflow::new()
130            .width("fill")
131            .options(vec![OverflowOption::new()
132                .text(PlainText::text("这是一个链接跳转"))
133                .multi_url(
134                    FeishuCardHrefVal::new().url("https://open.feishu.cn/document/home/index"),
135                )
136                .value("document")])
137            .value(json!({"key_1": "value_1"}))
138            .confirm(InputConfirm::new("title", "content"));
139
140        let json = json!({
141          "tag": "overflow",
142          "width": "fill", // 折叠按钮组的宽度。默认值为 default。
143          "options": [
144            // 在此添加折叠按钮组当中的选项按钮。
145            { // 为按钮添加文本。
146              "text": {
147                "tag": "plain_text", // 文本的标签。固定值为 plain_text。
148                "content": "这是一个链接跳转" // 文本的内容,最多支持 100 个字符。
149              },
150              "multi_url": { // 为按钮添加跳转链接。
151                "url": "https://open.feishu.cn/document/home/index", // 兜底的跳转地址。
152              },
153              "value": "document" // 该按钮的回传参数值。当用户点击选项后,应用会将该值返回至卡片请求地址。
154            }
155          ],
156          "value": {
157            // 组件整体的回调数据。
158            "key_1": "value_1"
159          },
160          "confirm": {
161            // 二次确认弹窗配置
162            "title": {
163              "tag": "plain_text",
164              "content": "title"
165            },
166            "text": {
167              "tag": "plain_text",
168              "content": "content"
169            }
170          }
171        });
172
173        assert_eq!(serde_json::to_value(&overflow).unwrap(), json);
174    }
175}