euv_ui/component/command/view/
fn.rs1use super::*;
2
3#[component]
17pub fn euv_command(node: VirtualNode<EuvCommandProps>) -> VirtualNode {
18 let EuvCommandProps {
19 value: _value,
20 label,
21 }: EuvCommandProps = node.try_get_props().unwrap_or_default();
22 let children: VirtualNode = node.get_children().into();
23 let base_class: &Css = c_euv_command();
24 html! {
25 div {
26 role: "group"
27 "aria-label": label
28 class: base_class
29 "data-slot": "command"
30 children
31 }
32 }
33}
34
35#[component]
48pub fn euv_command_input(node: VirtualNode<EuvCommandInputProps>) -> VirtualNode {
49 let EuvCommandInputProps {
50 value,
51 placeholder,
52 disabled,
53 }: EuvCommandInputProps = node.try_get_props().unwrap_or_default();
54 let disabled_signal: Signal<bool> = Signal::create(disabled);
55 let text: String = value.get();
56 html! {
57 div {
58 class: c_euv_command_input_wrapper()
59 "data-slot": "command-input-wrapper"
60 input {
61 type: "text"
62 role: "combobox"
63 "aria-expanded": "true"
64 value: text
65 placeholder: placeholder
66 disabled: disabled_signal
67 class: c_euv_command_input()
68 "data-slot": "command-input"
69 oninput: on_command_input(value)
70 }
71 }
72 }
73}
74
75fn on_command_input(value: Signal<String>) -> Option<Rc<dyn Fn(Event)>> {
85 Some(Rc::new(move |event: Event| {
86 let Some(target) = event.target() else {
87 return;
88 };
89 let Ok(input) = target.dyn_into::<HtmlInputElement>() else {
90 return;
91 };
92 value.set(input.value());
93 }))
94}
95
96#[component]
109pub fn euv_command_list(node: VirtualNode<EuvCommandListProps>) -> VirtualNode {
110 let EuvCommandListProps { label }: EuvCommandListProps =
111 node.try_get_props().unwrap_or_default();
112 let children: VirtualNode = node.get_children().into();
113 html! {
114 div {
115 role: "listbox"
116 "aria-label": label
117 class: c_euv_command_list()
118 "data-slot": "command-list"
119 children
120 }
121 }
122}
123
124#[component]
134pub fn euv_command_empty(node: VirtualNode<EuvCommandEmptyProps>) -> VirtualNode {
135 let EuvCommandEmptyProps { text }: EuvCommandEmptyProps =
136 node.try_get_props().unwrap_or_default();
137 html! {
138 div {
139 class: c_euv_command_empty()
140 "data-slot": "command-empty"
141 {
142 text
143 }
144 }
145 }
146}
147
148#[component]
158pub fn euv_command_group(node: VirtualNode<EuvCommandGroupProps>) -> VirtualNode {
159 let EuvCommandGroupProps { heading }: EuvCommandGroupProps =
160 node.try_get_props().unwrap_or_default();
161 let children: VirtualNode = node.get_children().into();
162 html! {
163 div {
164 role: "group"
165 class: c_euv_command_group()
166 "data-slot": "command-group"
167 div {
168 class: c_euv_command_group_heading()
169 {
170 heading
171 }
172 }
173 children
174 }
175 }
176}
177
178#[component]
191pub fn euv_command_item(node: VirtualNode<EuvCommandItemProps>) -> VirtualNode {
192 let EuvCommandItemProps {
193 value,
194 label,
195 selected,
196 disabled,
197 on_select,
198 }: EuvCommandItemProps = node.try_get_props().unwrap_or_default();
199 let children: VirtualNode = node.get_children().into();
200 let item_class: fn() -> &'static Css = if disabled {
201 c_euv_command_item_disabled
202 } else if selected {
203 c_euv_command_item_selected
204 } else {
205 c_euv_command_item
206 };
207 let on_click: Option<Rc<dyn Fn(Event)>> = if disabled {
208 None
209 } else {
210 Some(Rc::new(move |_: Event| {
211 if let Some(callback) = &on_select {
212 callback(value);
213 }
214 }))
215 };
216 html! {
217 div {
218 role: "option"
219 "aria-selected": if selected { "true" } else { "false" }
220 "aria-disabled": if disabled { "true" } else { "false" }
221 class: command_item_class(item_class)
222 "data-slot": "command-item"
223 onclick: on_click
224 { label }
225 children
226 }
227 }
228}
229
230#[component]
240pub fn euv_command_separator(node: VirtualNode<EuvCommandSeparatorProps>) -> VirtualNode {
241 let EuvCommandSeparatorProps { label }: EuvCommandSeparatorProps =
242 node.try_get_props().unwrap_or_default();
243 html! {
244 div {
245 role: "separator"
246 class: c_euv_command_separator()
247 "data-slot": "command-separator"
248 "aria-label": label
249 }
250 }
251}
252
253#[component]
263pub fn euv_command_shortcut(node: VirtualNode<EuvCommandShortcutProps>) -> VirtualNode {
264 let EuvCommandShortcutProps { text }: EuvCommandShortcutProps =
265 node.try_get_props().unwrap_or_default();
266 html! {
267 span {
268 class: c_euv_command_shortcut()
269 "data-slot": "command-shortcut"
270 {
271 text
272 }
273 }
274 }
275}
276
277fn command_item_class(state_class: fn() -> &'static Css) -> String {
292 let base_class: &str = c_euv_command_item().get_name();
293 let mut class_name: String = String::with_capacity(64);
294 class_name.push_str(base_class);
295 class_name.push(' ');
296 class_name.push_str(state_class().get_name());
297 class_name
298}