From ac92e9f16a236c103b990842348f3a57cd1b406b Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Wed, 20 Mar 2024 15:56:10 +0100 Subject: [PATCH] Started working on selection system --- application/Cargo.toml | 11 +++++++- application/public/styles.scss | 6 +++++ application/src/pages/times/add.rs | 42 +++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/application/Cargo.toml b/application/Cargo.toml index d6e3b26..a9294a1 100644 --- a/application/Cargo.toml +++ b/application/Cargo.toml @@ -24,11 +24,20 @@ strsim = "0.11.0" # strum = { version = "0.25", features = ["derive", "strum_macros"] } # strum_macros = "0.25" +[dependencies.web-sys] +version = "0.3" +features = [ + "Document", + "Window", + "Element", + "ScrollIntoViewOptions", + "ScrollLogicalPosition", + "ScrollBehavior", +] [dev-dependencies] wasm-bindgen = "0.2" wasm-bindgen-test = "0.3" -web-sys = { version = "0.3", features = ["Document", "Window"] } [profile.release] diff --git a/application/public/styles.scss b/application/public/styles.scss index 0df1144..5158ac9 100644 --- a/application/public/styles.scss +++ b/application/public/styles.scss @@ -13,6 +13,8 @@ html, body { height: 100vh; max-width: 100vw; + display: flex; + flex-direction: column; margin: 0; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; } @@ -170,3 +172,7 @@ form { cursor: pointer; background-color: $secondary-bg-color-lighter; } + +.participants-container .selected { + border: 1px solid $primary-color; +} diff --git a/application/src/pages/times/add.rs b/application/src/pages/times/add.rs index 7cfca3a..16d3308 100644 --- a/application/src/pages/times/add.rs +++ b/application/src/pages/times/add.rs @@ -1,5 +1,7 @@ -use leptos::*; +use leptos::{ev::keydown, *}; +use leptos_use::*; use strsim::normalized_damerau_levenshtein; +use web_sys::{ScrollIntoViewOptions, ScrollLogicalPosition}; use crate::util::surrealdb::Participant; @@ -37,12 +39,43 @@ pub fn Add() -> impl IntoView { let websocket = expect_context::(); let participants = expect_context::(); + let container_ref: NodeRef = create_node_ref(); + let (name, set_name) = create_signal(String::from("")); let (error, set_error) = create_signal(String::from("")); + let (selected, set_selected) = create_signal::(0); let participants_sorted = create_memo(move |_| sort_participants(participants.read.get(), name.get())); + let _ = use_event_listener(use_document(), keydown, move |evt| { + if evt.key() == "ArrowDown".to_string() { + set_selected.update(|x| { + let len = participants.read.get_untracked().len(); + if *x != len { + *x += 1; + } + }); + } else if evt.key() == "ArrowUp".to_string() { + set_selected.update(|x| { + if *x != 0 { + *x -= 1 + } + }); + } + + let el: web_sys::Element = container_ref + .get_untracked() + .unwrap() + .children() + .item(selected.get_untracked().try_into().unwrap()) + .expect("No element found"); + + el.scroll_into_view_with_scroll_into_view_options( + &ScrollIntoViewOptions::new().block(ScrollLogicalPosition::Center), + ); + }); + let on_submit = move |ev: leptos::ev::SubmitEvent| { ev.prevent_default(); set_error.set(String::from("")); @@ -61,12 +94,13 @@ pub fn Add() -> impl IntoView { placeholder="Name" on:input=move |ev| { set_name.set(event_target_value(&ev)); + set_selected.set(0); } prop:value=name /> -
    - {move || participants_sorted.get().into_iter().map(|participant| view! { -
  • {participant.name + " " + "(" + &participant.group[6..].to_string() + ")" }
  • +
      + {move || participants_sorted.get().into_iter().enumerate().map(|(i, participant)| view! { +
    • {participant.name + " " + "(" + &participant.group[6..].to_string() + ")" }
    • }).collect_view()}