From aed3f7eda9bfdf5d93a051e90501013b552d3407 Mon Sep 17 00:00:00 2001 From: trading_peter Date: Sun, 13 Mar 2022 16:12:24 +0100 Subject: [PATCH] Initial version --- README.md | 2 +- package-lock.json | 13 +++++++ package.json | 9 +++-- tp-element.js | 35 ------------------ tp-multi-toggle.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 40 deletions(-) create mode 100644 package-lock.json delete mode 100644 tp-element.js create mode 100644 tp-multi-toggle.js diff --git a/README.md b/README.md index 1ab27b7..e77b885 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# tp-element +# tp-multi-toggle diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7ce8c38 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "@tp/tp-multi-toggle", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@tp/helpers": { + "version": "1.0.1", + "resolved": "https://verdaccio.codeblob.work/@tp%2fhelpers/-/helpers-1.0.1.tgz", + "integrity": "sha512-f6pDPw4QpjWnmVkYgOHjMXQXtGB4vbA45eZV9DjCF9OoCXsa+Pz32H2rLQRKbdpsfFllywOBI+GMGPYDJyrG/Q==" + } + } +} diff --git a/package.json b/package.json index c39fdff..c5c00e5 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,19 @@ { - "name": "@tp/tp-element", - "version": "0.0.1", + "name": "@tp/tp-multi-toggle", + "version": "1.0.0", "description": "", - "main": "tp-element.js", + "main": "tp-multi-toggle.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "https://gitea.codeblob.work/tp-elements/tp-element.git" + "url": "https://gitea.codeblob.work/tp-elements/tp-multi-toggle.git" }, "author": "trading_peter", "license": "Apache-2.0", "dependencies": { + "@tp/helpers": "^1.0.1", "lit": "^2.2.0" } } diff --git a/tp-element.js b/tp-element.js deleted file mode 100644 index 6a92a2f..0000000 --- a/tp-element.js +++ /dev/null @@ -1,35 +0,0 @@ -/** -@license -Copyright (c) 2022 trading_peter -This program is available under Apache License Version 2.0 -*/ - -import { LitElement, html, css } from 'lit'; - -class TpElement extends LitElement { - static get styles() { - return [ - css` - :host { - display: block; - } - ` - ]; - } - - render() { - const { } = this; - - return html` - - `; - } - - static get properties() { - return { }; - } - - -} - -window.customElements.define('tp-element', TpElement); diff --git a/tp-multi-toggle.js b/tp-multi-toggle.js new file mode 100644 index 0000000..4076d8a --- /dev/null +++ b/tp-multi-toggle.js @@ -0,0 +1,91 @@ +/** +@license +Copyright (c) 2022 trading_peter +This program is available under Apache License Version 2.0 +*/ + +import { FormElement } from '@tp/helpers/form-element.js'; +import { LitElement, html, css } from 'lit'; +import { closest } from '@tp/helpers/closest.js'; + +class TpMultiToggle extends FormElement(LitElement) { + static get styles() { + return [ + css` + :host { + display: flex; + flex-direction: row; + align-items: center; + } + + ::slotted(tp-button) { + border-radius: 0; + flex: 1; + } + + ::slotted(tp-button:not([selected])) { + border-radius: 0; + } + + ::slotted(tp-button:first-of-type) { + border-radius: 4px 0 0 4px; + } + + ::slotted(tp-button:last-of-type) { + border-radius: 0 4px 4px 0; + } + + ::slotted(tp-button:not(:last-of-type)) { + margin-right: 1px; + } + ` + ]; + } + + render() { + return html` + + `; + } + + static get properties() { + return { + attr: { type: String }, + }; + } + + get value() { + const btn = this.querySelector('tp-button[selected]'); + + if (btn) { + return btn.getAttribute(this.attr || 'id'); + } + } + + constructor() { + super(); + this.boundClick = this.onClick.bind(this); + } + + connectedCallback() { + super.connectedCallback(); + this.addEventListener('click', this.boundClick); + } + + disconnectedCallback() { + super.disconnectedCallback(); + this.removeEventListener('click', this.boundClick); + } + + onClick(e) { + const btn = closest(e.target, 'tp-button', true); + + if (btn) { + this.querySelectorAll('tp-button').forEach(el => el.removeAttribute('selected')); + btn.setAttribute('selected', ''); + this.dispatchEvent(new CustomEvent('selection-changed', { detail: this.value, bubbles: true, composed: true })); + } + } +} + +window.customElements.define('to-multi-toggle', TpMultiToggle);