shoelace/src/components/radio/radio.ts

183 wiersze
5.1 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, internalProperty, property, query, unsafeCSS } from 'lit-element';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
import { event, EventEmitter, tag, watch } from '../../internal/decorators';
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 - The radio's label.
*
* @part base - The component's base wrapper.
* @part control - The radio control.
* @part checked-icon - The container the wraps the checked icon.
* @part label - The radio label.
*/
@tag('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
@internalProperty() private hasFocus = false;
2020-07-15 21:30:37 +00:00
/** The radio's name attribute. */
2021-03-06 17:01:39 +00:00
@property() name: string;
2020-07-15 21:30:37 +00:00
/** The radio's value attribute. */
2021-03-06 17:01:39 +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;
/** Emitted when the control loses focus. */
@event('sl-blur') slBlur: EventEmitter<void>;
/** Emitted when the control's checked state changes. */
@event('sl-change') slChange: EventEmitter<void>;
/** Emitted when the control gains focus. */
@event('sl-focus') slFocus: EventEmitter<void>;
2020-07-15 21:30:37 +00:00
/** Sets focus on the radio. */
2021-02-26 14:09:13 +00:00
setFocus(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. */
2021-02-26 14:09:13 +00:00
removeFocus() {
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-02-26 14:09:13 +00:00
const form = this.closest('sl-form, form') || document.body;
2020-07-15 21:30:37 +00:00
if (!this.name) return [];
2021-02-26 14:09:13 +00:00
return [...form.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-03-06 19:39:48 +00:00
@watch('checked')
handleCheckedChange() {
if (this.checked) {
this.getSiblingRadios().map(radio => (radio.checked = false));
2021-03-06 19:39:48 +00:00
}
this.input.checked = this.checked;
this.slChange.emit();
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;
2021-03-06 17:01:39 +00:00
this.slBlur.emit();
2020-07-15 21:30:37 +00:00
}
handleFocus() {
this.hasFocus = true;
2021-03-06 17:01:39 +00:00
this.slFocus.emit();
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].setFocus();
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-02-26 14:09:13 +00:00
name=${this.name}
.value=${this.value}
2021-03-06 17:01:39 +00:00
?checked=${this.checked}
?disabled=${this.disabled}
2020-08-27 20:51:31 +00:00
role="radio"
2021-02-26 14:09:13 +00:00
aria-checked=${this.checked ? 'true' : 'false'}
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
}
}