|
| 1 | +use crate::hooks::use_bool_toggle::use_bool_toggle; |
| 2 | +use crate::state::Entry as Item; |
| 3 | +use web_sys::{HtmlInputElement, MouseEvent}; |
| 4 | +use yew::events::{Event, FocusEvent, KeyboardEvent}; |
| 5 | +use yew::{function_component, html, Callback, Classes, Properties, TargetCast}; |
| 6 | + |
| 7 | +#[derive(PartialEq, Properties, Clone)] |
| 8 | +pub struct EntryProps { |
| 9 | + pub entry: Item, |
| 10 | + pub ontoggle: Callback<usize>, |
| 11 | + pub onremove: Callback<usize>, |
| 12 | + pub onedit: Callback<(usize, String)>, |
| 13 | +} |
| 14 | + |
| 15 | +#[function_component(Entry)] |
| 16 | +pub fn entry(props: &EntryProps) -> Html { |
| 17 | + let id = props.entry.id; |
| 18 | + let mut class = Classes::from("todo"); |
| 19 | + |
| 20 | + // We use the `use_bool_toggle` hook and set the default value to `false` |
| 21 | + // as the default we are not editing the the entry. When we want to edit the |
| 22 | + // entry we can call the toggle method on the `UseBoolToggleHandle` |
| 23 | + // which will trigger a re-render with the toggle value being `true` for that |
| 24 | + // render and after that render the value of toggle will be flipped back to |
| 25 | + // its default (`false`). |
| 26 | + // We are relying on the behavior of `onblur` and `onkeypress` to cause |
| 27 | + // another render so that this component will render again with the |
| 28 | + // default value of toggle. |
| 29 | + let edit_toggle = use_bool_toggle(false); |
| 30 | + let is_editing = *edit_toggle; |
| 31 | + |
| 32 | + if is_editing { |
| 33 | + class.push("editing"); |
| 34 | + } |
| 35 | + |
| 36 | + if props.entry.completed { |
| 37 | + class.push("completed"); |
| 38 | + } |
| 39 | + |
| 40 | + html! { |
| 41 | + <li {class}> |
| 42 | + <div class="view"> |
| 43 | + <input |
| 44 | + type="checkbox" |
| 45 | + class="toggle" |
| 46 | + checked={props.entry.completed} |
| 47 | + onclick={props.ontoggle.reform(move |_| id)} |
| 48 | + /> |
| 49 | + <label ondblclick={Callback::once(move |_| { |
| 50 | + edit_toggle.toggle(); |
| 51 | + })}> |
| 52 | + { &props.entry.description } |
| 53 | + </label> |
| 54 | + <button class="destroy" onclick={props.onremove.reform(move |_| id)} /> |
| 55 | + </div> |
| 56 | + <EntryEdit entry={props.entry.clone()} onedit={props.onedit.clone()} editing={is_editing} /> |
| 57 | + </li> |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(PartialEq, Properties, Clone)] |
| 62 | +pub struct EntryEditProps { |
| 63 | + pub entry: Item, |
| 64 | + pub onedit: Callback<(usize, String)>, |
| 65 | + pub editing: bool, |
| 66 | +} |
| 67 | + |
| 68 | +#[function_component(EntryEdit)] |
| 69 | +pub fn entry_edit(props: &EntryEditProps) -> Html { |
| 70 | + let id = props.entry.id; |
| 71 | + |
| 72 | + let target_input_value = |e: &Event| { |
| 73 | + let input: HtmlInputElement = e.target_unchecked_into(); |
| 74 | + input.value() |
| 75 | + }; |
| 76 | + |
| 77 | + let onblur = { |
| 78 | + let edit = props.onedit.clone(); |
| 79 | + |
| 80 | + move |e: FocusEvent| { |
| 81 | + let value = target_input_value(&e); |
| 82 | + edit.emit((id, value)) |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + let onkeypress = { |
| 87 | + let edit = props.onedit.clone(); |
| 88 | + |
| 89 | + move |e: KeyboardEvent| { |
| 90 | + if e.key() == "Enter" { |
| 91 | + let value = target_input_value(&e); |
| 92 | + edit.emit((id, value)) |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + let onmouseover = |e: MouseEvent| { |
| 98 | + e.target_unchecked_into::<HtmlInputElement>() |
| 99 | + .focus() |
| 100 | + .unwrap_or_default(); |
| 101 | + }; |
| 102 | + |
| 103 | + if props.editing { |
| 104 | + html! { |
| 105 | + <input |
| 106 | + class="edit" |
| 107 | + type="text" |
| 108 | + value={props.entry.description.clone()} |
| 109 | + {onmouseover} |
| 110 | + {onblur} |
| 111 | + {onkeypress} |
| 112 | + /> |
| 113 | + } |
| 114 | + } else { |
| 115 | + html! { <input type="hidden" /> } |
| 116 | + } |
| 117 | +} |
0 commit comments