1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::card::{
    components::{
        content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
    },
    href::FeishuCardHrefVal,
};

/// 折叠按钮组
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardOverflow {
    /// 折叠按钮组的标签。固定值为 overflow。
    tag: String,
    /// 折叠按钮组的宽度。支持以下枚举值:
    ///
    /// - default:默认宽度
    /// - fill:卡片最大支持宽度
    /// - [100,∞)px:自定义宽度。超出卡片宽度时将按最大支持宽度展示
    #[serde(skip_serializing_if = "Option::is_none")]
    width: Option<String>,
    /// 折叠按钮组当中的选项按钮。详见下文 options 字段说明。
    options: Vec<OverflowOption>,
    /// 组件整体的回调数据。当用户点击折叠按钮组的折叠按钮后,会将 value
    /// 的值返回给接收回调数据的服务器。后续你可以通过服务器接收的 value 值进行业务处理。
    #[serde(skip_serializing_if = "Option::is_none")]
    value: Option<Value>,
    /// 二次确认弹窗配置。指在用户提交时弹出二次确认弹窗提示;只有用户点击确认后,
    /// 才提交输入的内容。该字段默认提供了确认和取消按钮,你只需要配置弹窗的标题与内容即可。
    ///
    /// 注意:confirm 字段仅在用户点击包含提交属性的按钮时才会触发二次确认弹窗。
    #[serde(skip_serializing_if = "Option::is_none")]
    confirm: Option<InputConfirm>,
}

impl Default for FeishuCardOverflow {
    fn default() -> Self {
        Self {
            tag: "overflow".to_string(),
            width: None,
            options: vec![],
            value: None,
            confirm: None,
        }
    }
}

impl FeishuCardOverflow {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn width(mut self, width: &str) -> Self {
        self.width = Some(width.to_string());
        self
    }

    pub fn options(mut self, options: Vec<OverflowOption>) -> Self {
        self.options = options;
        self
    }

    pub fn value(mut self, value: Value) -> Self {
        self.value = Some(value);
        self
    }

    pub fn confirm(mut self, confirm: InputConfirm) -> Self {
        self.confirm = Some(confirm);
        self
    }

    pub fn add_option(mut self, option: OverflowOption) -> Self {
        self.options.push(option);
        self
    }
}

/// options 字段说明
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct OverflowOption {
    /// 按钮上的文本。
    text: Option<PlainText>,
    /// 为按钮添加多端的跳转链接。
    multi_url: Option<FeishuCardHrefVal>,
    /// 该按钮的回传参数值。当用户点击选项后,应用会将该值返回至卡片请求地址。
    value: Option<String>,
}

impl OverflowOption {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn text(mut self, text: PlainText) -> Self {
        self.text = Some(text);
        self
    }

    pub fn multi_url(mut self, multi_url: FeishuCardHrefVal) -> Self {
        self.multi_url = Some(multi_url);
        self
    }

    pub fn value(mut self, value: impl ToString) -> Self {
        self.value = Some(value.to_string());
        self
    }
}

#[cfg(test)]
mod test {
    use serde_json::json;

    use crate::card::{
        components::{
            content_components::plain_text::PlainText,
            interactive_components::{
                input::InputConfirm,
                overflow::{FeishuCardOverflow, OverflowOption},
            },
        },
        href::FeishuCardHrefVal,
    };

    #[test]
    fn test_overflow() {
        let overflow = FeishuCardOverflow::new()
            .width("fill")
            .options(vec![OverflowOption::new()
                .text(PlainText::text("这是一个链接跳转"))
                .multi_url(
                    FeishuCardHrefVal::new().url("https://open.feishu.cn/document/home/index"),
                )
                .value("document")])
            .value(json!({"key_1": "value_1"}))
            .confirm(InputConfirm::new("title", "content"));

        let json = json!({
          "tag": "overflow",
          "width": "fill", // 折叠按钮组的宽度。默认值为 default。
          "options": [
            // 在此添加折叠按钮组当中的选项按钮。
            { // 为按钮添加文本。
              "text": {
                "tag": "plain_text", // 文本的标签。固定值为 plain_text。
                "content": "这是一个链接跳转" // 文本的内容,最多支持 100 个字符。
              },
              "multi_url": { // 为按钮添加跳转链接。
                "url": "https://open.feishu.cn/document/home/index", // 兜底的跳转地址。
              },
              "value": "document" // 该按钮的回传参数值。当用户点击选项后,应用会将该值返回至卡片请求地址。
            }
          ],
          "value": {
            // 组件整体的回调数据。
            "key_1": "value_1"
          },
          "confirm": {
            // 二次确认弹窗配置
            "title": {
              "tag": "plain_text",
              "content": "title"
            },
            "text": {
              "tag": "plain_text",
              "content": "content"
            }
          }
        });

        assert_eq!(serde_json::to_value(&overflow).unwrap(), json);
    }
}