From 7b3e8498c0109471b1d741594c2a69b0293403d5 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 13:48:53 +0200 Subject: [PATCH] Add custom text input to add lines --- src/app.js | 3 ++- src/components/custom-text.js | 19 +++++++++++++++++++ src/modules/store.js | 12 +++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/components/custom-text.js diff --git a/src/app.js b/src/app.js index a631381..885c1f0 100644 --- a/src/app.js +++ b/src/app.js @@ -2,6 +2,7 @@ import './components/main-window.js'; import './components/custom-button.js'; import './components/item-list.js'; import './components/custom-modal.js'; +import './components/custom-text.js'; import Store from './modules/store.js'; import text from './modules/text.js'; @@ -110,7 +111,7 @@ class AppBase extends HTMLElement {

${text.modal.description}

- + Add Cancel diff --git a/src/components/custom-text.js b/src/components/custom-text.js new file mode 100644 index 0000000..2a753b0 --- /dev/null +++ b/src/components/custom-text.js @@ -0,0 +1,19 @@ +import Store from '../modules/store.js'; + +class CustomText extends HTMLInputElement { + constructor() { + super(); + this.type = 'text'; + } + + connectedCallback() { + this.addEventListener('input', this.onInput.bind(this)); + } + + onInput() { + Store.publish(Store.actions.INPUT_TEXT, this.value); + } + +} + +customElements.define('custom-text', CustomText, { extends: 'input' }); diff --git a/src/modules/store.js b/src/modules/store.js index a0c57ca..2cffa98 100644 --- a/src/modules/store.js +++ b/src/modules/store.js @@ -6,12 +6,14 @@ export default class Store { SELECT: 'SELECT', OPEN_MODAL: 'OPEN_MODAL', CLOSE_MODAL: 'CLOSE_MODAL', + INPUT_TEXT: 'INPUT_TEXT', } constructor() { this.state = { records: [], selected: [], + input: "", modalActive: false, }; this.callbacks = [() => console.log(this.state)]; @@ -45,9 +47,11 @@ export default class Store { ...this.state, records: this.state.records.concat({ id: this.state.records.length, - text: 'asdf', + text: this.state.input, action: 'add', }), + input: "", + modalActive: false, }; break; case this.constructor.actions.REMOVE_LINE: @@ -89,6 +93,12 @@ export default class Store { modalActive: false, } break; + case this.constructor.actions.INPUT_TEXT: + this.state = { + ...this.state, + input: event.detail, + } + break; default: console.error(`Action "${event.type}" not implemented in eventHandler`); return;