2022-03-24 12:11:49 +00:00
|
|
|
import { html, LitElement } from 'lit';
|
|
|
|
import { customElement, property, query, state } from 'lit/decorators.js';
|
2021-09-29 12:40:26 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js';
|
2022-07-28 17:00:43 +00:00
|
|
|
import { emit } from 'src/internal/event';
|
2022-03-24 12:11:49 +00:00
|
|
|
import { watch } from '../../internal/watch';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './radio.styles';
|
2022-07-19 12:27:39 +00:00
|
|
|
import type { CSSResultGroup } from 'lit';
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-17 10:09:10 +00:00
|
|
|
* @since 2.0
|
2020-07-15 21:30:37 +00:00
|
|
|
* @status stable
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot - The radio's label.
|
2020-07-15 21:30:37 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-blur - Emitted when the control loses focus.
|
|
|
|
* @event sl-change - Emitted when the control's checked state changes.
|
|
|
|
* @event sl-focus - Emitted when the control gains focus.
|
2021-06-24 22:24:54 +00:00
|
|
|
*
|
2022-03-09 20:54:18 +00:00
|
|
|
* @csspart base - The component's internal wrapper.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart control - The radio control.
|
|
|
|
* @csspart checked-icon - The container the wraps the checked icon.
|
|
|
|
* @csspart label - The radio label.
|
2020-07-15 21:30:37 +00:00
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-radio')
|
2022-03-24 12:11:49 +00:00
|
|
|
export default class SlRadio extends LitElement {
|
2022-07-19 12:27:39 +00:00
|
|
|
static styles: CSSResultGroup = styles;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
2022-03-24 12:11:49 +00:00
|
|
|
@query('.radio__input') input: HTMLInputElement;
|
|
|
|
|
|
|
|
@state() protected hasFocus = false;
|
2022-07-28 17:00:43 +00:00
|
|
|
|
|
|
|
@state() checked = false;
|
2022-03-24 12:11:49 +00:00
|
|
|
|
|
|
|
/** The radio's name attribute. */
|
2022-07-28 17:00:43 +00:00
|
|
|
@property({ reflect: true }) name: string;
|
2022-03-24 12:11:49 +00:00
|
|
|
|
|
|
|
/** The radio's value attribute. */
|
|
|
|
@property() value: string;
|
|
|
|
|
|
|
|
/** Disables the radio. */
|
|
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
|
|
|
super.connectedCallback();
|
2022-07-28 17:00:43 +00:00
|
|
|
this.setInitialAttributes();
|
|
|
|
this.addEventListeners();
|
2022-03-24 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
@watch('checked')
|
|
|
|
handleCheckedChange() {
|
|
|
|
this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
|
|
|
|
this.setAttribute('tabindex', this.checked ? '0' : '-1');
|
2022-03-24 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
@watch('disabled', { waitUntilFirstUpdate: true })
|
|
|
|
handleDisabledChange() {
|
|
|
|
this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
|
2022-03-24 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
private handleBlur() {
|
2022-03-24 12:11:49 +00:00
|
|
|
this.hasFocus = false;
|
|
|
|
emit(this, 'sl-blur');
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
private handleClick() {
|
2022-03-24 12:11:49 +00:00
|
|
|
if (!this.disabled) {
|
|
|
|
this.checked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
private handleFocus() {
|
2022-03-24 12:11:49 +00:00
|
|
|
this.hasFocus = true;
|
|
|
|
emit(this, 'sl-focus');
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
private addEventListeners() {
|
|
|
|
this.addEventListener('blur', () => this.handleBlur())
|
|
|
|
this.addEventListener('click', () => this.handleClick())
|
|
|
|
this.addEventListener('focus', () => this.handleFocus())
|
2022-03-24 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:00:43 +00:00
|
|
|
private setInitialAttributes() {
|
|
|
|
this.setAttribute('role', 'radio');
|
|
|
|
this.setAttribute('tabindex', '-1');
|
2022-03-24 12:11:49 +00:00
|
|
|
this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
render() {
|
2021-02-26 14:09:13 +00:00
|
|
|
return html`
|
2022-07-28 17:00:43 +00:00
|
|
|
<span
|
2020-07-15 21:30:37 +00:00
|
|
|
part="base"
|
2021-02-26 14:09:13 +00:00
|
|
|
class=${classMap({
|
2020-07-15 21:30:37 +00:00
|
|
|
radio: true,
|
|
|
|
'radio--checked': this.checked,
|
|
|
|
'radio--disabled': this.disabled,
|
|
|
|
'radio--focused': this.hasFocus
|
2021-02-26 14:09:13 +00:00
|
|
|
})}
|
2020-07-15 21:30:37 +00:00
|
|
|
>
|
|
|
|
<span part="control" class="radio__control">
|
2022-06-29 12:09:25 +00:00
|
|
|
<svg part="checked-icon" class="radio__icon" viewBox="0 0 16 16">
|
|
|
|
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
|
|
<g fill="currentColor">
|
|
|
|
<circle cx="8" cy="8" r="3.42857143"></circle>
|
2020-07-15 21:30:37 +00:00
|
|
|
</g>
|
2022-06-29 12:09:25 +00:00
|
|
|
</g>
|
|
|
|
</svg>
|
2020-07-15 21:30:37 +00:00
|
|
|
</span>
|
|
|
|
|
2021-12-30 21:32:57 +00:00
|
|
|
<span part="label" class="radio__label">
|
2021-03-06 17:01:39 +00:00
|
|
|
<slot></slot>
|
2020-07-15 21:30:37 +00:00
|
|
|
</span>
|
|
|
|
</label>
|
2021-02-26 14:09:13 +00:00
|
|
|
`;
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-radio': SlRadio;
|
|
|
|
}
|
|
|
|
}
|