1use crate::types::{self, Label, LabelChild, ModalItem, TextInputStyle};
4use crate::{Select, layout::TextDisplay};
5
6#[derive(Debug, Clone)]
8pub struct TextInput(types::TextInput);
9
10impl TextInput {
11 fn styled(custom_id: impl Into<String>, style: TextInputStyle) -> Self {
12 Self(types::TextInput {
13 id: None,
14 custom_id: custom_id.into(),
15 style,
16 min_length: None,
17 max_length: None,
18 required: None,
19 value: None,
20 placeholder: None,
21 })
22 }
23
24 #[must_use]
26 pub fn short(custom_id: impl Into<String>) -> Self {
27 Self::styled(custom_id, TextInputStyle::Short)
28 }
29
30 #[must_use]
32 pub fn paragraph(custom_id: impl Into<String>) -> Self {
33 Self::styled(custom_id, TextInputStyle::Paragraph)
34 }
35
36 #[must_use]
38 pub fn length(mut self, min: u16, max: u16) -> Self {
39 (self.0.min_length, self.0.max_length) = (Some(min), Some(max));
40 self
41 }
42
43 #[must_use]
45 pub fn required(mut self, required: bool) -> Self {
46 self.0.required = Some(required);
47 self
48 }
49
50 #[must_use]
52 pub fn placeholder(mut self, text: impl Into<String>) -> Self {
53 self.0.placeholder = Some(text.into());
54 self
55 }
56
57 #[must_use]
59 pub fn value(mut self, text: impl Into<String>) -> Self {
60 self.0.value = Some(text.into());
61 self
62 }
63}
64
65#[derive(Debug, Clone)]
67pub struct Modal {
68 custom_id: String,
69 title: String,
70 items: Vec<ModalItem>,
71}
72
73impl Modal {
74 #[must_use]
76 pub fn new(custom_id: impl Into<String>, title: impl Into<String>) -> Self {
77 Self {
78 custom_id: custom_id.into(),
79 title: title.into(),
80 items: Vec::new(),
81 }
82 }
83
84 fn labelled(mut self, label: String, component: LabelChild) -> Self {
85 let label = Label {
86 id: None,
87 label,
88 description: None,
89 component,
90 };
91 self.items.push(ModalItem::Label(label));
92 self
93 }
94
95 #[must_use]
97 pub fn input(self, label: impl Into<String>, input: TextInput) -> Self {
98 self.labelled(label.into(), LabelChild::TextInput(input.0))
99 }
100
101 #[must_use]
103 pub fn select(self, label: impl Into<String>, select: Select) -> Self {
104 let child = match select {
105 Select::Strings(s) => LabelChild::StringSelect(s),
106 Select::Entities(s) => LabelChild::EntitySelect(s),
107 };
108 self.labelled(label.into(), child)
109 }
110
111 #[must_use]
113 pub fn text(mut self, content: impl Into<String>) -> Self {
114 self.items
115 .push(ModalItem::TextDisplay(TextDisplay::new(content)));
116 self
117 }
118
119 #[must_use]
121 pub fn build(self) -> types::Modal {
122 types::Modal {
123 custom_id: self.custom_id,
124 title: self.title,
125 components: self.items,
126 }
127 }
128}