dees-catalog/ts_web/elements/dees-input-checkbox.ts

156 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-08-25 11:51:55 +00:00
import {
customElement,
DeesElement,
TemplateResult,
property,
html,
} from '@designestate/dees-element';
2020-09-13 16:24:48 +00:00
import * as domtools from '@designestate/dees-domtools';
2021-03-06 15:48:02 +00:00
declare global {
interface HTMLElementTagNameMap {
'dees-input-checkbox': DeesInputCheckbox;
}
}
2020-09-13 16:24:48 +00:00
@customElement('dees-input-checkbox')
2020-12-09 23:05:13 +00:00
export class DeesInputCheckbox extends DeesElement {
2021-08-19 22:25:14 +00:00
// STATIC
2020-09-13 16:24:48 +00:00
public static demo = () => html`<dees-input-checkbox></dees-input-checkbox>`;
2021-08-25 11:51:55 +00:00
2021-08-19 22:25:14 +00:00
// INSTANCE
public changeSubject = new domtools.rxjs.Subject();
2020-09-13 16:24:48 +00:00
2021-08-25 11:51:55 +00:00
@property({
type: String,
})
2020-09-13 16:24:48 +00:00
public key: string;
2021-08-25 11:51:55 +00:00
@property({
type: String,
})
2020-09-13 16:24:48 +00:00
public label: string = 'Label';
2021-08-25 11:51:55 +00:00
@property({
type: Boolean,
})
2020-09-13 16:24:48 +00:00
public value: boolean = false;
2021-08-25 11:51:55 +00:00
@property({
type: Boolean,
})
public required: boolean = false;
2021-08-19 22:25:14 +00:00
2020-09-13 16:24:48 +00:00
public render(): TemplateResult {
return html`
${domtools.elementBasic.styles}
<style>
* {
box-sizing: border-box;
}
:host {
display: block;
position: relative;
margin: 20px 0px;
cursor: pointer;
}
.maincontainer {
display: grid;
grid-template-columns: 25px auto;
padding: 5px 0px;
2020-12-09 23:05:13 +00:00
color: ${this.goBright ? '#333' : '#ccc'};
2020-09-13 16:24:48 +00:00
}
.maincontainer:hover {
2020-12-09 23:05:13 +00:00
${this.goBright ? '#000' : '#ccc'};
2020-09-13 16:24:48 +00:00
}
.label {
margin-left: 15px;
line-height: 25px;
font-size: 14px;
font-weight: normal;
}
input:focus {
outline: none;
border-bottom: 1px solid #e4002b;
}
.checkbox {
transition: all 0.1s;
box-sizing: border-box;
2020-12-09 23:05:13 +00:00
border: 1px solid ${this.goBright ? '#CCC' : '#999'};
2020-09-13 16:24:48 +00:00
border-radius: 2px;
height: 24px;
width: 24px;
display: inline-block;
2020-12-09 23:05:13 +00:00
background: ${this.goBright ? '#fafafa' : '#222'};
2020-09-13 16:24:48 +00:00
}
.checkbox.selected {
background: #039BE5;
border: 1px solid #039BE5;
}
.checkmark {
display: inline-block;
width: 22px;
height: 22px;
-ms-transform: rotate(45deg); /* IE 9 */
-webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
transform: rotate(45deg);
}
.checkmark_stem {
position: absolute;
width: 3px;
height: 9px;
background-color: #ccc;
left: 11px;
top: 6px;
}
.checkmark_kick {
position: absolute;
width: 3px;
height: 3px;
background-color: #fff;
left: 8px;
top: 12px;
}
img {
padding: 4px;
}
</style>
<div class="maincontainer" @click="${this.toggleSelected}">
<div class="checkbox ${this.value ? 'selected' : ''}">
${this.value
? html`
<span class="checkmark">
<div class="checkmark_stem"></div>
<div class="checkmark_kick"></div>
</span>
`
: html``}
</div>
<div class="label">${this.label}</div>
</div>
`;
}
public async toggleSelected() {
this.value = !this.value;
this.dispatchEvent(
new CustomEvent('newValue', {
detail: this.value,
bubbles: true,
})
);
2021-08-19 22:25:14 +00:00
this.changeSubject.next(this);
2020-09-13 16:24:48 +00:00
}
}