Skip to main content

zaun_client/ui/
modal.rs

1//! Modals (§8.14): labels around inputs, and text.
2#![allow(
3    clippy::new_ret_no_self,
4    reason = "unit builders named as in the zaun-bot SDK; each `new` is the component itself"
5)]
6
7use zaun_community_protocol::command::object::{
8    Checkbox as Box_, CheckboxGroup as Group, Component, FileUpload as Upload, Label,
9    RadioGroup as Radio, SelectOption, TextInput as Input, TextInputStyle,
10};
11
12use super::Select;
13
14fn options(list: &[(&str, &str)]) -> Vec<SelectOption> {
15    list.iter()
16        .map(|(label, value)| SelectOption {
17            label: (*label).to_owned(),
18            value: (*value).to_owned(),
19            description: None,
20            emoji: None,
21            default: false,
22        })
23        .collect()
24}
25
26/// A text field.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct TextInput(Input);
29
30impl TextInput {
31    fn styled(custom_id: impl Into<String>, style: TextInputStyle) -> Self {
32        Self(Input {
33            id: None,
34            custom_id: custom_id.into(),
35            style,
36            min_length: None,
37            max_length: None,
38            required: None,
39            value: None,
40            placeholder: None,
41        })
42    }
43
44    /// One line.
45    #[must_use]
46    pub fn short(custom_id: impl Into<String>) -> Self {
47        Self::styled(custom_id, TextInputStyle::Short)
48    }
49
50    /// Several lines.
51    #[must_use]
52    pub fn paragraph(custom_id: impl Into<String>) -> Self {
53        Self::styled(custom_id, TextInputStyle::Paragraph)
54    }
55
56    /// Length bounds.
57    #[must_use]
58    pub const fn length(mut self, min: u32, max: u32) -> Self {
59        (self.0.min_length, self.0.max_length) = (Some(min), Some(max));
60        self
61    }
62
63    /// Whether it must be filled (default yes).
64    #[must_use]
65    pub const fn required(mut self, required: bool) -> Self {
66        self.0.required = Some(required);
67        self
68    }
69
70    /// Grey text shown while empty.
71    #[must_use]
72    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
73        self.0.placeholder = Some(text.into());
74        self
75    }
76}
77
78/// One choice among options (type 21).
79#[derive(Debug, Clone, Copy)]
80pub struct RadioGroup;
81
82impl RadioGroup {
83    /// The input: 2–10 `(label, value)` options.
84    #[must_use]
85    pub fn new(custom_id: impl Into<String>, list: &[(&str, &str)]) -> Component {
86        Component::RadioGroup(Radio {
87            id: None,
88            custom_id: custom_id.into(),
89            options: options(list),
90            required: None,
91        })
92    }
93}
94
95/// Several choices (type 22).
96#[derive(Debug, Clone, Copy)]
97pub struct CheckboxGroup;
98
99impl CheckboxGroup {
100    /// The input: 1–10 `(label, value)` options.
101    #[must_use]
102    pub fn new(custom_id: impl Into<String>, list: &[(&str, &str)]) -> Component {
103        Component::CheckboxGroup(Group {
104            id: None,
105            custom_id: custom_id.into(),
106            options: options(list),
107            min_values: None,
108            max_values: None,
109            required: None,
110        })
111    }
112}
113
114/// One yes/no box (type 23); never required.
115#[derive(Debug, Clone, Copy)]
116pub struct Checkbox;
117
118impl Checkbox {
119    /// The input.
120    #[must_use]
121    pub fn new(custom_id: impl Into<String>) -> Component {
122        Component::Checkbox(Box_ {
123            id: None,
124            custom_id: custom_id.into(),
125            default: false,
126        })
127    }
128}
129
130/// Files the member uploads (type 19).
131#[derive(Debug, Clone, Copy)]
132pub struct FileUpload;
133
134impl FileUpload {
135    /// The input: up to `max` files.
136    #[must_use]
137    pub fn new(custom_id: impl Into<String>, max: u32) -> Component {
138        Component::FileUpload(Upload {
139            id: None,
140            custom_id: custom_id.into(),
141            min_values: None,
142            max_values: Some(max),
143            required: None,
144        })
145    }
146}
147
148/// A modal: up to five labelled inputs and texts.
149#[derive(Debug, Clone)]
150pub struct Modal {
151    /// Its `custom_id`.
152    pub custom_id: String,
153    /// Its title.
154    pub title: String,
155    /// Its labels and texts.
156    pub components: Vec<Component>,
157}
158
159impl Modal {
160    /// An empty modal.
161    #[must_use]
162    pub fn new(custom_id: impl Into<String>, title: impl Into<String>) -> Self {
163        Self {
164            custom_id: custom_id.into(),
165            title: title.into(),
166            components: Vec::new(),
167        }
168    }
169
170    /// Adds any input (radio, checkbox, file upload, …) under `label`.
171    #[must_use]
172    pub fn field(mut self, label: impl Into<String>, input: Component) -> Self {
173        self.components.push(Component::Label(Label {
174            id: None,
175            label: label.into(),
176            description: None,
177            component: Box::new(input),
178        }));
179        self
180    }
181
182    /// Adds a text field under `label`.
183    #[must_use]
184    pub fn input(self, label: impl Into<String>, input: TextInput) -> Self {
185        self.field(label, Component::TextInput(input.0))
186    }
187
188    /// Adds a select under `label`.
189    #[must_use]
190    pub fn select(self, label: impl Into<String>, select: Select) -> Self {
191        self.field(label, select.build())
192    }
193
194    /// Adds markdown text.
195    #[must_use]
196    pub fn text(mut self, content: impl Into<String>) -> Self {
197        self.components.push(super::TextDisplay::new(content));
198        self
199    }
200}