2024-12-20 18:24:20 +03:00
|
|
|
use leptos::*;
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn EditableCell(
|
|
|
|
value: String,
|
|
|
|
on_input: impl Fn(String) + 'static,
|
2024-12-24 14:27:32 +03:00
|
|
|
#[prop(optional)] key: Option<String>, // Optional `key` prop
|
2024-12-20 18:24:20 +03:00
|
|
|
) -> impl IntoView {
|
|
|
|
let (input_value, set_input_value) = create_signal(value.clone());
|
2024-12-24 14:27:32 +03:00
|
|
|
let (has_focus, set_has_focus) = create_signal(false); // Track focus state locally
|
2024-12-20 18:24:20 +03:00
|
|
|
|
|
|
|
let handle_input = move |e: web_sys::Event| {
|
|
|
|
let new_value = event_target_value(&e);
|
2024-12-23 17:56:34 +03:00
|
|
|
set_input_value.set(new_value.clone());
|
2024-12-20 18:24:20 +03:00
|
|
|
on_input(new_value);
|
|
|
|
};
|
|
|
|
|
2024-12-24 14:27:32 +03:00
|
|
|
let handle_focus = move |_: web_sys::FocusEvent| {
|
|
|
|
set_has_focus.set(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
let handle_blur = move |_: web_sys::FocusEvent| {
|
|
|
|
set_has_focus.set(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use key to force updates only when necessary
|
|
|
|
let _key = key.unwrap_or_default();
|
|
|
|
|
2024-12-20 18:24:20 +03:00
|
|
|
view! {
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={input_value.get()}
|
|
|
|
on:input=handle_input
|
2024-12-24 14:27:32 +03:00
|
|
|
on:focus=handle_focus
|
|
|
|
on:blur=handle_blur
|
|
|
|
class={if has_focus.get() { "focused" } else { "not-focused" }}
|
2024-12-20 18:24:20 +03:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
}
|