use serde::{Deserialize, Serialize};
use crate::card::components::{
content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct SelectPerson {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
r#type: Option<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")]
options: Option<Vec<SelectPersonOption>>,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
}
impl Default for SelectPerson {
fn default() -> Self {
Self {
tag: "select_person".to_string(),
r#type: None,
required: None,
disabled: None,
placeholder: None,
width: None,
options: None,
confirm: None,
}
}
}
impl SelectPerson {
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 confirm(mut self, confirm: InputConfirm) -> Self {
self.confirm = Some(confirm);
self
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SelectPersonOption {
value: String,
}
impl SelectPersonOption {
pub fn new(value: &str) -> Self {
Self {
value: value.to_string(),
}
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
#[test]
fn test_select_person() {
let select_person = SelectPerson::new()
.r#type("text")
.required(true)
.disabled(false)
.placeholder(PlainText::text("默认提示文本"))
.width("default")
.options(vec![
SelectPersonOption::new("ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx"),
SelectPersonOption::new("ou_f9d24af786a14340721288cda6axxxxx"),
])
.confirm(InputConfirm::new("弹窗标题", "弹窗正文文案"));
let json = json!({
"tag": "select_person", "type": "text", "required":true, "disabled":false, "placeholder": {
"tag": "plain_text",
"content": "默认提示文本"
},
"width": "default", "options": [
{
"value": "ou_48d0958ee4b2ab3eaf0b5f6c968xxxxx" },
{
"value": "ou_f9d24af786a14340721288cda6axxxxx" }
],
"confirm": {
"title": {
"tag": "plain_text",
"content": "弹窗标题"
},
"text": {
"tag": "plain_text",
"content": "弹窗正文文案"
}
}
});
assert_eq!(serde_json::to_value(&select_person).unwrap(), json);
}
}