shoelace/src/components/checkbox/checkbox.ts

198 wiersze
5.9 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
import { customElement, property, query, state } from 'lit/decorators';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
2021-03-18 13:04:23 +00:00
import { event, EventEmitter, watch } from '../../internal/decorators';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./checkbox.scss';
let id = 0;
/**
* @since 2.0
* @status stable
*
* @slot - The checkbox's label.
*
* @part base - The component's base wrapper.
* @part control - The checkbox control.
* @part checked-icon - The container the wraps the checked icon.
* @part indeterminate-icon - The container that wraps the indeterminate icon.
* @part label - The checkbox label.
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-checkbox')
2021-03-09 00:14:32 +00:00
export default class SlCheckbox extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
@query('input[type="checkbox"]') input: HTMLInputElement;
2021-02-26 14:09:13 +00:00
private inputId = `checkbox-${++id}`;
private labelId = `checkbox-label-${id}`;
2021-03-06 17:01:39 +00:00
@state() private hasFocus = false;
2021-02-26 14:09:13 +00:00
/** The checkbox's name attribute. */
2021-04-01 11:57:31 +00:00
@property() name = '';
2021-02-26 14:09:13 +00:00
/** The checkbox's value attribute. */
2021-04-01 11:57:31 +00:00
@property() value = '';
2021-02-26 14:09:13 +00:00
/** Disables the checkbox. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-02-26 14:09:13 +00:00
/** Makes the checkbox a required field. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) required = false;
2021-02-26 14:09:13 +00:00
/** Draws the checkbox in a checked state. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) checked = false;
2021-02-26 14:09:13 +00:00
/** Draws the checkbox in an indeterminate state. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) indeterminate = false;
2021-02-26 14:09:13 +00:00
/** This will be true when the control is in an invalid state. Validity is determined by the `required` prop. */
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>;
2021-02-26 14:09:13 +00:00
2021-03-06 17:01:39 +00:00
/** 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>;
firstUpdated() {
2021-02-26 14:09:13 +00:00
this.input.indeterminate = this.indeterminate;
}
2021-04-01 12:40:48 +00:00
/** Simulates a click on the checkbox. */
click() {
this.input.click();
}
2021-02-26 14:09:13 +00:00
/** Sets focus on the checkbox. */
focus(options?: FocusOptions) {
2021-02-26 14:09:13 +00:00
this.input.focus(options);
}
/** Removes focus from the checkbox. */
blur() {
2021-02-26 14:09:13 +00:00
this.input.blur();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() {
return this.input.reportValidity();
}
/** Sets a custom validation message. If `message` is not empty, the field will be considered invalid. */
setCustomValidity(message: string) {
this.input.setCustomValidity(message);
this.invalid = !this.input.checkValidity();
}
handleClick() {
2021-03-06 17:01:39 +00:00
this.checked = !this.checked;
2021-02-26 14:09:13 +00:00
this.indeterminate = false;
}
handleBlur() {
this.hasFocus = false;
2021-03-06 17:01:39 +00:00
this.slBlur.emit();
2021-02-26 14:09:13 +00:00
}
handleFocus() {
this.hasFocus = true;
2021-03-06 17:01:39 +00:00
this.slFocus.emit();
2021-02-26 14:09:13 +00:00
}
handleLabelMouseDown(event: MouseEvent) {
// Prevent clicks on the label from briefly blurring the input
event.preventDefault();
this.input.focus();
}
2021-03-11 17:51:41 +00:00
@watch('checked')
@watch('indeterminate')
2021-02-26 14:09:13 +00:00
handleStateChange() {
this.input.checked = this.checked;
this.input.indeterminate = this.indeterminate;
this.slChange.emit();
2021-02-26 14:09:13 +00:00
}
render() {
return html`
<label
part="base"
class=${classMap({
checkbox: true,
'checkbox--checked': this.checked,
'checkbox--disabled': this.disabled,
'checkbox--focused': this.hasFocus,
'checkbox--indeterminate': this.indeterminate
})}
for=${this.inputId}
2021-03-06 17:01:39 +00:00
@mousedown=${this.handleLabelMouseDown}
2021-02-26 14:09:13 +00:00
>
<span part="control" class="checkbox__control">
${this.checked
? html`
<span part="checked-icon" class="checkbox__icon">
<svg viewBox="0 0 16 16">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round">
<g stroke="currentColor" stroke-width="2">
<g transform="translate(3.428571, 3.428571)">
<path d="M0,5.71428571 L3.42857143,9.14285714"></path>
<path d="M9.14285714,0 L3.42857143,9.14285714"></path>
</g>
</g>
</g>
</svg>
</span>
`
: ''}
${!this.checked && this.indeterminate
? html`
<span part="indeterminate-icon" class="checkbox__icon">
<svg viewBox="0 0 16 16">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round">
<g stroke="currentColor" stroke-width="2">
<g transform="translate(2.285714, 6.857143)">
<path d="M10.2857143,1.14285714 L1.14285714,1.14285714"></path>
</g>
</g>
</g>
</svg>
</span>
`
: ''}
<input
id=${this.inputId}
type="checkbox"
name=${this.name}
2021-03-06 17:39:24 +00:00
.value=${this.value}
2021-03-06 17:01:39 +00:00
?checked=${this.checked}
?disabled=${this.disabled}
?required=${this.required}
2021-02-26 14:09:13 +00:00
role="checkbox"
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}
2021-02-26 14:09:13 +00:00
/>
</span>
<span part="label" id=${this.labelId} class="checkbox__label">
2021-03-06 17:01:39 +00:00
<slot></slot>
2021-02-26 14:09:13 +00:00
</span>
</label>
`;
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-checkbox': SlCheckbox;
}
}