use serde::{Deserialize, Serialize};
use crate::card::components::{
content_components::plain_text::PlainText,
interactive_components::select_person::SelectPersonOption,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct MultiSelectPerson {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
r#type: Option<String>,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
required: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
disabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
placeholder: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
selected_values: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<Vec<SelectPersonOption>>,
}
impl Default for MultiSelectPerson {
fn default() -> Self {
Self {
tag: "multi_select_person".to_string(),
r#type: None,
name: "".to_string(),
required: None,
disabled: None,
placeholder: None,
width: None,
selected_values: None,
options: None,
}
}
}
impl MultiSelectPerson {
pub fn new() -> Self {
Self::default()
}
pub fn r#type(mut self, r#type: &str) -> Self {
self.r#type = Some(r#type.to_string());
self
}
pub fn required(mut self, required: bool) -> Self {
self.required = Some(required);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = Some(disabled);
self
}
pub fn placeholder(mut self, placeholder: PlainText) -> Self {
self.placeholder = Some(placeholder);
self
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn options(mut self, options: Vec<SelectPersonOption>) -> Self {
self.options = Some(options);
self
}
pub fn name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
pub fn selected_values(mut self, selected_values: Vec<&str>) -> Self {
self.selected_values = Some(selected_values.iter().map(|s| s.to_string()).collect());
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::components::interactive_components::select_person::SelectPersonOption;
use super::*;
#[test]
fn test_select_person() {
let select_person = MultiSelectPerson::new()
.r#type("text")
.name("multi_select_users")
.required(true)
.disabled(false)
.placeholder(PlainText::text("默认提示文本"))
.width("default")
.selected_values(vec!["ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx"])
.options(vec![
SelectPersonOption::new("ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx"),
SelectPersonOption::new("ou_f9d24af786a14340721288cda6axxxxx"),
]);
let json = json!({
"tag": "multi_select_person", "type": "text", "name":"multi_select_users", "placeholder": {
"tag": "plain_text",
"content": "默认提示文本"
},
"width": "default", "required":true, "disabled":false, "selected_values": ["ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx"], "options": [
{
"value": "ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx"
},
{
"value": "ou_f9d24af786a14340721288cda6axxxxx"
}
]
});
assert_eq!(serde_json::to_value(&select_person).unwrap(), json);
}
}