2025-06-19 09:41:00 +00:00
|
|
|
import {customElement, type TemplateResult, property, html, css, cssManager} from '@design.estate/dees-element';
|
|
|
|
import { DeesInputBase } from './dees-input-base.js';
|
2025-06-19 11:39:16 +00:00
|
|
|
import { demoFunc } from './dees-input-radio.demo.js';
|
2020-09-13 16:24:48 +00:00
|
|
|
|
2021-03-06 15:48:02 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'dees-input-radio': DeesInputRadio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 16:24:48 +00:00
|
|
|
@customElement('dees-input-radio')
|
2025-06-19 09:41:00 +00:00
|
|
|
export class DeesInputRadio extends DeesInputBase<DeesInputRadio> {
|
2025-06-19 11:39:16 +00:00
|
|
|
public static demo = demoFunc;
|
2020-09-13 16:24:48 +00:00
|
|
|
|
2021-08-20 00:25:14 +02:00
|
|
|
// INSTANCE
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
|
|
@property()
|
|
|
|
public value: boolean = false;
|
|
|
|
|
2025-06-22 20:32:59 +00:00
|
|
|
@property({ type: String })
|
|
|
|
public name: string = '';
|
2021-08-26 21:30:35 +02:00
|
|
|
|
2021-08-25 13:51:55 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
2025-06-19 09:41:00 +00:00
|
|
|
this.labelPosition = 'right'; // Radio buttons default to label on the right
|
2021-08-25 13:51:55 +02:00
|
|
|
}
|
|
|
|
|
2025-06-19 09:41:00 +00:00
|
|
|
public static styles = [
|
|
|
|
...DeesInputBase.baseStyles,
|
|
|
|
cssManager.defaultStyles,
|
|
|
|
css`
|
2020-09-13 16:24:48 +00:00
|
|
|
* {
|
|
|
|
box-sizing: border-box;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
.maincontainer {
|
|
|
|
transition: all 0.3s;
|
|
|
|
padding: 5px 0px;
|
|
|
|
color: #ccc;
|
|
|
|
}
|
|
|
|
|
|
|
|
.maincontainer:hover {
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
|
|
|
|
input:focus {
|
|
|
|
outline: none;
|
|
|
|
border-bottom: 1px solid #e4002b;
|
|
|
|
}
|
|
|
|
|
|
|
|
.checkbox {
|
|
|
|
transition: all 0.3s;
|
|
|
|
box-sizing: border-box;
|
|
|
|
border-radius: 20px;
|
|
|
|
border: 1px solid #999;
|
|
|
|
height: 24px;
|
|
|
|
width: 24px;
|
|
|
|
display: inline-block;
|
|
|
|
background: #222;
|
|
|
|
}
|
|
|
|
|
|
|
|
.checkbox.selected {
|
2024-01-10 05:11:55 +01:00
|
|
|
background: #0050b9;
|
|
|
|
border: 1px solid #0050b9;
|
2020-09-13 16:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.maincontainer:hover .checkbox.selected {
|
|
|
|
background: #03A9F4;
|
|
|
|
}
|
|
|
|
|
|
|
|
.innercircle {
|
|
|
|
transition: all 0.3s;
|
|
|
|
margin: 6px 0px 0px 6px;
|
|
|
|
background: #222;
|
|
|
|
width: 10px;
|
|
|
|
height: 10px;
|
|
|
|
border-radius: 10px;
|
|
|
|
}
|
2025-06-19 09:41:00 +00:00
|
|
|
`,
|
|
|
|
];
|
|
|
|
|
|
|
|
public render(): TemplateResult {
|
|
|
|
return html`
|
|
|
|
<div class="input-wrapper">
|
|
|
|
<div class="maincontainer" @click="${this.toggleSelected}">
|
|
|
|
<div class="checkbox ${this.value ? 'selected' : ''}">
|
|
|
|
${this.value ? html`<div class="innercircle"></div>`: html``}
|
|
|
|
</div>
|
2020-09-13 16:24:48 +00:00
|
|
|
</div>
|
2025-06-19 09:41:00 +00:00
|
|
|
<dees-label .label=${this.label}></dees-label>
|
2020-09-13 16:24:48 +00:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async toggleSelected () {
|
2025-06-22 20:32:59 +00:00
|
|
|
// Radio buttons can only be selected, not deselected by clicking
|
|
|
|
if (this.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this radio has a name, find and deselect other radios in the same group
|
|
|
|
if (this.name) {
|
|
|
|
// Try to find a form container first, then fall back to document
|
|
|
|
const container = this.closest('dees-form') ||
|
|
|
|
this.closest('dees-demowrapper') ||
|
|
|
|
this.closest('.radio-group')?.parentElement ||
|
|
|
|
document;
|
|
|
|
const allRadios = container.querySelectorAll(`dees-input-radio[name="${this.name}"]`);
|
|
|
|
allRadios.forEach((radio: DeesInputRadio) => {
|
|
|
|
if (radio !== this && radio.value) {
|
|
|
|
radio.value = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.value = true;
|
2020-09-13 16:24:48 +00:00
|
|
|
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
|
|
|
}
|
2025-06-19 09:41:00 +00:00
|
|
|
|
|
|
|
public getValue(): boolean {
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setValue(value: boolean): void {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2020-09-13 16:24:48 +00:00
|
|
|
}
|