From 8c1272fa9cf5f4eca189daf7c01bffa58a43ad1c Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 25 May 2024 03:21:27 +0300 Subject: [PATCH] Added web-sys and wasm-bindgen dependencies to web crate, fixed matrix cell oninput event --- web/Cargo.toml | 4 ++++ web/src/main.rs | 1 + web/src/matrix.rs | 43 ++++++++++++++++--------------------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/web/Cargo.toml b/web/Cargo.toml index 347d3e6..ae2abcb 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -5,4 +5,8 @@ edition = "2021" [dependencies] math = { path = "../math" } +log = "0.4.21" +wasm-bindgen = "0.2.92" +wasm-logger = "0.2.0" +web-sys = "0.3.69" yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } diff --git a/web/src/main.rs b/web/src/main.rs index 732cb49..79bfef9 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -13,5 +13,6 @@ fn App() -> Html { } fn main() { + wasm_logger::init(wasm_logger::Config::default()); yew::Renderer::::new().render(); } diff --git a/web/src/matrix.rs b/web/src/matrix.rs index 4178382..5bce0a2 100644 --- a/web/src/matrix.rs +++ b/web/src/matrix.rs @@ -1,5 +1,7 @@ use math::matrix::Matrix; use yew::prelude::*; +use web_sys::HtmlInputElement; +use wasm_bindgen::JsCast; pub enum MatrixMsg { CellChange(Cell), @@ -9,8 +11,13 @@ pub enum MatrixMsg { RemoveColumn } +#[derive(PartialEq, Clone, Copy)] +pub struct Cell { + i: usize, j: usize, value: i32 +} + pub struct MatrixComponent { - matrix: Matrix + matrix: Matrix } impl Component for MatrixComponent { @@ -42,17 +49,22 @@ impl Component for MatrixComponent { fn view(&self, ctx: &Context) -> Html { let on_size_change = ctx.link().callback(|msg| msg); + log::info!("{:?}", &self.matrix); html! {
{ self.matrix.iter_rows().enumerate().map(|(i, row)| html! { { row.iter().enumerate().map(|(j, e)| { - let cell = Cell { i, j, value: *e }; - let on_cell_change = ctx.link().callback(MatrixMsg::CellChange); + let oninput = ctx.link().callback(move |e: InputEvent| { + let input = e.target().unwrap().unchecked_into::(); + let value = input.value().trim().parse::().unwrap_or_default(); + log::info!("{}", value); + MatrixMsg::CellChange(Cell { i, j, value }) + }); html! { } }).collect::() @@ -67,29 +79,6 @@ impl Component for MatrixComponent { } } -#[derive(PartialEq, Properties)] -pub struct MatrixCellProps { - pub cell: Cell, - pub on_cell_change: Callback -} - -#[derive(PartialEq, Clone, Copy)] -pub struct Cell { - i: usize, j: usize, value: f64 -} - -#[function_component] -pub fn MatrixCellComponent(props: &MatrixCellProps) -> Html { - let cell = props.cell.clone(); - let oninput = props.on_cell_change - .reform(move |e: InputEvent| - Cell { i: cell.i, j: cell.j, value: e.as_f64().unwrap_or_default() } - ); - html! { - - } -} - #[derive(PartialEq, Properties)] pub struct MatrixSizeControlsProps { on_size_change: Callback
- +