shoelace/src/components/radio/radio.ts

194 wiersze
5.4 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
2021-04-02 11:47:25 +00:00
import { ifDefined } from 'lit-html/directives/if-defined';
import { emit } from '../../internal/event';
import { watch } from '../../internal/watch';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./radio.scss';
2020-07-15 21:30:37 +00:00
let id = 0;
/**
2020-07-17 10:09:10 +00:00
* @since 2.0
2020-07-15 21:30:37 +00:00
* @status stable
*
* @slot default The radio's label.
2020-07-15 21:30:37 +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.
*
* @csspart base The component's base wrapper.
* @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')
2021-03-09 00:14:32 +00:00
export default class SlRadio extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
@query('input[type="radio"]') input: HTMLInputElement;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
private inputId = `radio-${++id}`;
private labelId = `radio-label-${id}`;
2021-03-06 17:01:39 +00:00
@state() private hasFocus = false;
2020-07-15 21:30:37 +00:00
/** The radio's name attribute. */
2021-04-02 11:47:25 +00:00
@property() name: string;
2020-07-15 21:30:37 +00:00
/** The radio's value attribute. */
2021-04-02 11:47:25 +00:00
@property() value: string;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
/** Disables the radio. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
/** Draws the radio in a checked state. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) checked = false;
2020-07-15 21:30:37 +00:00
/**
* This will be true when the control is in an invalid state. Validity in range inputs is determined by the message
* provided by the `setCustomValidity` method.
*/
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) invalid = false;
2021-04-01 12:40:48 +00:00
/** Simulates a click on the radio. */
click() {
this.input.click();
}
2020-07-15 21:30:37 +00:00
/** Sets focus on the radio. */
focus(options?: FocusOptions) {
2021-01-07 15:13:08 +00:00
this.input.focus(options);
2020-07-15 21:30:37 +00:00
}
/** Removes focus from the radio. */
blur() {
2020-07-15 21:30:37 +00:00
this.input.blur();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */
2021-02-26 14:09:13 +00:00
reportValidity() {
return this.input.reportValidity();
}
/** Sets a custom validation message. If `message` is not empty, the field will be considered invalid. */
2021-02-26 14:09:13 +00:00
setCustomValidity(message: string) {
this.input.setCustomValidity(message);
this.invalid = !this.input.checkValidity();
}
2020-07-15 21:30:37 +00:00
getAllRadios() {
2021-04-09 12:15:33 +00:00
const radioGroup = this.closest('sl-radio-group');
2020-07-15 21:30:37 +00:00
2021-04-09 12:15:33 +00:00
// Radios must be part of a radio group
if (!radioGroup) {
return [];
}
2020-07-15 21:30:37 +00:00
2021-04-09 12:15:33 +00:00
return [...radioGroup.querySelectorAll('sl-radio')].filter((radio: this) => radio.name === this.name) as this[];
2020-07-15 21:30:37 +00:00
}
getSiblingRadios() {
2021-02-26 14:09:13 +00:00
return this.getAllRadios().filter(radio => radio !== this) as this[];
2020-07-15 21:30:37 +00:00
}
2021-06-15 13:26:35 +00:00
@watch('checked', { waitUntilFirstUpdate: true })
2021-03-06 19:39:48 +00:00
handleCheckedChange() {
if (this.checked) {
this.getSiblingRadios().map(radio => (radio.checked = false));
2021-03-06 19:39:48 +00:00
}
emit(this, 'sl-change');
2021-03-06 19:39:48 +00:00
}
2020-07-15 21:30:37 +00:00
handleClick() {
2021-03-06 17:01:39 +00:00
this.checked = true;
2020-07-15 21:30:37 +00:00
}
handleBlur() {
this.hasFocus = false;
emit(this, 'sl-blur');
2020-07-15 21:30:37 +00:00
}
handleFocus() {
this.hasFocus = true;
emit(this, 'sl-focus');
2020-07-15 21:30:37 +00:00
}
handleKeyDown(event: KeyboardEvent) {
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) {
const radios = this.getAllRadios().filter(radio => !radio.disabled);
const incr = ['ArrowUp', 'ArrowLeft'].includes(event.key) ? -1 : 1;
2021-02-26 14:09:13 +00:00
let index = radios.indexOf(this) + incr;
2020-07-15 21:30:37 +00:00
if (index < 0) index = radios.length - 1;
if (index > radios.length - 1) index = 0;
this.getAllRadios().map(radio => (radio.checked = false));
radios[index].focus();
2020-07-15 21:30:37 +00:00
radios[index].checked = true;
event.preventDefault();
}
}
handleMouseDown(event: MouseEvent) {
// Prevent clicks on the label from briefly blurring the input
event.preventDefault();
this.input.focus();
}
render() {
2021-02-26 14:09:13 +00:00
return html`
2020-07-15 21:30:37 +00:00
<label
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
})}
for=${this.inputId}
2021-03-06 17:01:39 +00:00
@keydown=${this.handleKeyDown}
@mousedown=${this.handleMouseDown}
2020-07-15 21:30:37 +00:00
>
<span part="control" class="radio__control">
<span part="checked-icon" class="radio__icon">
<svg 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>
</g>
</g>
</svg>
</span>
<input
2021-02-26 14:09:13 +00:00
id=${this.inputId}
2020-07-15 21:30:37 +00:00
type="radio"
2021-04-02 11:47:25 +00:00
name=${ifDefined(this.name)}
value=${ifDefined(this.value)}
2021-06-17 20:45:49 +00:00
.checked=${this.checked}
.disabled=${this.disabled}
2021-02-26 14:09:13 +00:00
aria-checked=${this.checked ? 'true' : 'false'}
2021-04-09 12:15:33 +00:00
aria-disabled=${this.disabled ? 'true' : 'false'}
2021-02-26 14:09:13 +00:00
aria-labelledby=${this.labelId}
2021-03-06 17:01:39 +00:00
@click=${this.handleClick}
@blur=${this.handleBlur}
@focus=${this.handleFocus}
2020-07-15 21:30:37 +00:00
/>
</span>
2021-02-26 14:09:13 +00:00
<span part="label" id=${this.labelId} 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:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-radio': SlRadio;
}
}