Initial version

This commit is contained in:
trading_peter 2022-03-13 16:12:24 +01:00
parent 556941b24e
commit aed3f7eda9
5 changed files with 110 additions and 40 deletions

View File

@ -1 +1 @@
# tp-element # tp-multi-toggle

13
package-lock.json generated Normal file
View File

@ -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=="
}
}
}

View File

@ -1,18 +1,19 @@
{ {
"name": "@tp/tp-element", "name": "@tp/tp-multi-toggle",
"version": "0.0.1", "version": "1.0.0",
"description": "", "description": "",
"main": "tp-element.js", "main": "tp-multi-toggle.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "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", "author": "trading_peter",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@tp/helpers": "^1.0.1",
"lit": "^2.2.0" "lit": "^2.2.0"
} }
} }

View File

@ -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);

91
tp-multi-toggle.js Normal file
View File

@ -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`
<slot></slot>
`;
}
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);