shoelace/src/components/radio/radio.ts

228 wiersze
6.3 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } from 'lit';
2021-05-27 21:00:43 +00:00
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';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import { emit } from '../../internal/event';
import { FormSubmitController } from '../../internal/form-control';
import { watch } from '../../internal/watch';
2021-07-10 00:45:44 +00:00
import styles from './radio.styles';
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-25 20:25:46 +00:00
* @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-07-10 00:45:44 +00:00
static styles = styles;
2021-03-06 17:01:39 +00:00
@query('input[type="radio"]') input: HTMLInputElement;
2020-07-15 21:30:37 +00:00
// @ts-expect-error -- Controller is currently unused
private readonly formSubmitController = new FormSubmitController(this, {
value: (control: SlRadio) => (control.checked ? control.value : undefined)
});
@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-07-01 00:04:46 +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-07-01 00:04:46 +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-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) invalid = false;
2021-03-06 17:01:39 +00:00
2021-12-30 21:32:57 +00:00
firstUpdated() {
this.updateComplete.then(() => {
const radios = this.getAllRadios();
const checkedRadio = radios.find(radio => radio.checked);
radios.forEach(radio => {
2021-12-30 21:32:57 +00:00
radio.input.tabIndex = -1;
});
2022-01-19 14:37:07 +00:00
if (checkedRadio) {
checkedRadio.input.tabIndex = 0;
} else if (radios.length > 0) {
radios[0].input.tabIndex = 0;
2021-12-30 21:32:57 +00:00
}
});
}
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();
}
getAllRadios() {
const radioGroup = this.closest('sl-radio-group');
// Radios must be part of a radio group
if (radioGroup === null) {
return [this];
}
return [...radioGroup.querySelectorAll('sl-radio')].filter((radio: this) => radio.name === this.name) as this[];
}
getSiblingRadios() {
return this.getAllRadios().filter(radio => radio !== this);
}
2021-06-25 23:22:05 +00:00
handleBlur() {
this.hasFocus = false;
2021-06-25 23:40:12 +00:00
emit(this, 'sl-blur');
2021-06-25 23:22:05 +00:00
}
@watch('checked', { waitUntilFirstUpdate: true })
handleCheckedChange() {
if (this.checked) {
2021-12-30 21:32:57 +00:00
this.input.tabIndex = 0;
this.getSiblingRadios().forEach(radio => {
2021-12-30 21:32:57 +00:00
radio.input.tabIndex = -1;
radio.checked = false;
});
}
}
2020-07-15 21:30:37 +00:00
handleClick() {
2021-03-06 17:01:39 +00:00
this.checked = true;
emit(this, 'sl-change');
2020-07-15 21:30:37 +00:00
}
@watch('disabled', { waitUntilFirstUpdate: true })
2021-06-25 23:22:05 +00:00
handleDisabledChange() {
// Disabled form controls are always valid, so we need to recheck validity when the state changes
this.input.disabled = this.disabled;
this.invalid = !this.input.checkValidity();
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;
let index = radios.indexOf(this) + incr;
if (index < 0) {
index = radios.length - 1;
}
if (index > radios.length - 1) {
index = 0;
}
this.getAllRadios().forEach(radio => {
2021-12-30 21:32:57 +00:00
radio.checked = false;
radio.input.tabIndex = -1;
});
radios[index].focus();
radios[index].checked = true;
2021-12-30 21:32:57 +00:00
radios[index].input.tabIndex = 0;
emit(radios[index], 'sl-change');
event.preventDefault();
}
}
2020-07-15 21:30:37 +00:00
render() {
2021-12-30 21:32:57 +00:00
this.setAttribute('role', 'radio');
this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
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
})}
@keydown=${this.handleKeyDown}
2020-07-15 21:30:37 +00:00
>
2021-08-27 12:54:00 +00:00
<input
class="radio__input"
type="radio"
name=${ifDefined(this.name)}
value=${ifDefined(this.value)}
.checked=${live(this.checked)}
.disabled=${this.disabled}
2021-12-30 21:32:57 +00:00
aria-hidden="true"
2021-08-27 12:54:00 +00:00
@click=${this.handleClick}
@blur=${this.handleBlur}
@focus=${this.handleFocus}
/>
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>
</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:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-radio': SlRadio;
}
}