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

173 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-08-25 13:51:55 +02:00
import {
customElement,
2023-08-07 20:02:18 +02:00
type TemplateResult,
2021-08-25 13:51:55 +02:00
property,
html,
2021-08-26 21:30:35 +02:00
css,
2023-08-07 20:02:18 +02:00
cssManager,
2023-08-07 19:13:29 +02:00
} from '@design.estate/dees-element';
import { DeesInputBase } from './dees-input-base.js';
import { demoFunc } from './dees-input-checkbox.demo.js';
2020-09-13 16:24:48 +00:00
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')
export class DeesInputCheckbox extends DeesInputBase<DeesInputCheckbox> {
2021-08-20 00:25:14 +02:00
// STATIC
public static demo = demoFunc;
2021-08-25 13:51:55 +02:00
2021-08-20 00:25:14 +02:00
// INSTANCE
2020-09-13 16:24:48 +00:00
2021-08-25 13:51:55 +02:00
@property({
type: Boolean,
})
2020-09-13 16:24:48 +00:00
public value: boolean = false;
2021-08-20 00:25:14 +02:00
constructor() {
super();
this.labelPosition = 'right'; // Checkboxes default to label on the right
}
2021-08-26 21:30:35 +02:00
public static styles = [
...DeesInputBase.baseStyles,
cssManager.defaultStyles,
css`
2020-09-13 16:24:48 +00:00
* {
box-sizing: border-box;
}
:host {
position: relative;
2023-12-26 21:21:18 +01:00
cursor: default;
}
:host(:hover) {
filter: ${cssManager.bdTheme('brightness(0.95)', 'brightness(1.1)')};
2020-09-13 16:24:48 +00:00
}
.maincontainer {
padding: 5px 0px;
color: ${cssManager.bdTheme('#333', '#ccc')};
2020-09-13 16:24:48 +00:00
}
.maincontainer:hover {
color: ${cssManager.bdTheme('#000', '#fff')};
2020-09-13 16:24:48 +00:00
}
input:focus {
outline: none;
border-bottom: 1px solid #e4002b;
}
.checkbox {
transition: all 0.1s;
box-sizing: border-box;
border: 1px solid ${cssManager.bdTheme('#CCC', '#999')};
2020-09-13 16:24:48 +00:00
border-radius: 2px;
height: 24px;
width: 24px;
display: inline-block;
background: ${cssManager.bdTheme('#fafafa', '#222')};
2020-09-13 16:24:48 +00:00
}
.checkbox.selected {
2024-01-10 05:11:55 +01:00
background: #0050b9;
border: 1px solid #0050b9;
2020-09-13 16:24:48 +00:00
}
2021-08-26 21:30:35 +02:00
.checkbox.disabled {
background: none;
border: 1px dashed ${cssManager.bdTheme('#666666', '#666666')};
}
2021-08-27 13:38:08 +02:00
.checkbox .checkmark {
2020-09-13 16:24:48 +00:00
display: inline-block;
width: 22px;
height: 22px;
-ms-transform: rotate(45deg); /* IE 9 */
-webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
transform: rotate(45deg);
}
2021-08-27 13:38:08 +02:00
.checkbox .checkmark_stem {
2020-09-13 16:24:48 +00:00
position: absolute;
width: 3px;
height: 9px;
2021-08-27 13:38:08 +02:00
background-color: #fff;
2020-09-13 16:24:48 +00:00
left: 11px;
top: 6px;
}
2021-08-27 13:38:08 +02:00
.checkbox .checkmark_kick {
2020-09-13 16:24:48 +00:00
position: absolute;
width: 3px;
height: 3px;
background-color: #fff;
left: 8px;
top: 12px;
}
2021-08-27 13:38:08 +02:00
.checkbox.disabled .checkmark_stem, .checkbox.disabled .checkmark_kick {
background-color: ${cssManager.bdTheme('#333', '#fff')};
}
2020-09-13 16:24:48 +00:00
img {
padding: 4px;
}
`,
];
public render(): TemplateResult {
return html`
<div class="input-wrapper">
<div class="maincontainer" @click="${this.toggleSelected}">
<div class="checkbox ${this.value ? 'selected' : ''} ${this.disabled ? 'disabled' : ''}" tabindex="0">
${this.value
? html`
<span class="checkmark">
<div class="checkmark_stem"></div>
<div class="checkmark_kick"></div>
</span>
`
: html``}
</div>
2020-09-13 16:24:48 +00:00
</div>
<dees-label .label=${this.label}></dees-label>
2020-09-13 16:24:48 +00:00
</div>
`;
}
public async toggleSelected() {
2021-08-26 21:30:35 +02:00
if (this.disabled) {
return;
}
2020-09-13 16:24:48 +00:00
this.value = !this.value;
this.dispatchEvent(
new CustomEvent('newValue', {
detail: this.value,
bubbles: true,
})
);
2021-08-20 00:25:14 +02:00
this.changeSubject.next(this);
2020-09-13 16:24:48 +00:00
}
2023-08-19 11:47:45 +02:00
public getValue(): boolean {
return this.value;
}
public setValue(value: boolean): void {
this.value = value;
}
2023-08-19 11:47:45 +02:00
public focus(): void {
const checkboxDiv = this.shadowRoot.querySelector('.checkbox');
if (checkboxDiv) {
(checkboxDiv as any).focus();
}
}
2020-09-13 16:24:48 +00:00
}