2024-12-20 18:24:20 +03:00
|
|
|
use leptos::*;
|
2024-12-23 17:56:34 +03:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
|
2024-12-20 18:24:20 +03:00
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn TagEditor(
|
|
|
|
tags: Vec<(String, String)>,
|
|
|
|
on_add: impl Fn(String, String) + 'static,
|
2024-12-23 17:56:34 +03:00
|
|
|
on_remove: Arc<Mutex<dyn FnMut(usize) + Send + Sync>>,
|
2024-12-20 18:24:20 +03:00
|
|
|
) -> impl IntoView {
|
|
|
|
let (key, set_key) = create_signal(String::new());
|
|
|
|
let (value, set_value) = create_signal(String::new());
|
|
|
|
|
2024-12-23 17:56:34 +03:00
|
|
|
let add_tag = move |_| {
|
2024-12-20 18:24:20 +03:00
|
|
|
if !key.get().is_empty() && !value.get().is_empty() {
|
|
|
|
on_add(key.get(), value.get());
|
2024-12-23 17:56:34 +03:00
|
|
|
set_key.set(String::new());
|
|
|
|
set_value.set(String::new());
|
2024-12-20 18:24:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
view! {
|
|
|
|
<div>
|
|
|
|
<ul>
|
|
|
|
{tags.iter().enumerate().map(|(index, (k, v))| {
|
2024-12-23 17:56:34 +03:00
|
|
|
let on_remove = on_remove.clone();
|
2024-12-20 18:24:20 +03:00
|
|
|
view! {
|
|
|
|
<li>
|
|
|
|
{format!("{}: {}", k, v)}
|
2024-12-23 17:56:34 +03:00
|
|
|
<button on:click=move |_| {
|
|
|
|
let mut on_remove = on_remove.lock().unwrap();
|
|
|
|
on_remove(index);
|
|
|
|
}>
|
|
|
|
{ "Remove" }
|
|
|
|
</button>
|
2024-12-20 18:24:20 +03:00
|
|
|
</li>
|
|
|
|
}
|
|
|
|
}).collect::<Vec<_>>()}
|
|
|
|
</ul>
|
|
|
|
<input
|
|
|
|
placeholder="Key"
|
|
|
|
value={key.get()}
|
2024-12-23 17:56:34 +03:00
|
|
|
on:input=move |e| set_key.set(event_target_value(&e))
|
2024-12-20 18:24:20 +03:00
|
|
|
/>
|
|
|
|
<input
|
|
|
|
placeholder="Value"
|
|
|
|
value={value.get()}
|
2024-12-23 17:56:34 +03:00
|
|
|
on:input=move |e| set_value.set(event_target_value(&e))
|
2024-12-20 18:24:20 +03:00
|
|
|
/>
|
|
|
|
<button on:click=add_tag>{ "Add Tag" }</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|