From 65aa41d7807871d7813acce57b4482729e617bae Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sun, 22 Aug 2021 14:18:16 -0400 Subject: [PATCH] Add rust-form-multi to liveview-dev --- liveview-rust/js/liveview-dev.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/liveview-rust/js/liveview-dev.js b/liveview-rust/js/liveview-dev.js index def3db1..ebaf6a8 100644 --- a/liveview-rust/js/liveview-dev.js +++ b/liveview-rust/js/liveview-dev.js @@ -70,19 +70,38 @@ function hexToRgb(v) { function getFormData(form) { // let ret = {}; return Array.from(form.elements).reduce((acc, e) => { + var encodedValue; + switch(e.type) { case "color": - acc[e.name] = hexToRgb(e.value); + encodedValue = hexToRgb(e.value); break; case "number": - acc[e.name] = parseInt(e.value); + encodedValue = parseInt(e.value); break; case "checkbox": - acc[e.name] = e.value === "on"; + encodedValue = e.value === "on"; break; default: break; } + + let multiName = e.getAttribute("rust-form-multi"); + + // This is a multivalue + if (multiName) { + if (acc[multiName]) { + // Push to existing array + acc[multiName].push(encodedValue); + } else { + // This is the first element in the multi-array + acc[multiName] = [encodedValue]; + } + } else { + // This is a regular value + acc[e.name] = encodedValue; + } + return acc; }, {}); // return ret;