shoelace/src/components/checkbox/checkbox.ts

195 wiersze
5.9 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:./checkbox.scss';
let id = 0;
/**
* @since 2.0
* @status stable
*
* @slot default The checkbox's label.
2021-02-26 14:09:13 +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 checkbox control.
* @csspart checked-icon The container the wraps the checked icon.
* @csspart indeterminate-icon The container that wraps the indeterminate icon.
* @csspart label The checkbox label.
2021-02-26 14:09:13 +00:00
*/
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-02 11:47:25 +00:00
@property() name: string;
2021-02-26 14:09:13 +00:00
/** The checkbox's value attribute. */
2021-04-02 11:47:25 +00:00
@property() value: string;
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;
firstUpdated() {
2021-06-16 12:42:02 +00:00
this.invalid = !this.input.checkValidity();
2021-02-26 14:09:13 +00:00
}
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;
emit(this, 'sl-blur');
2021-02-26 14:09:13 +00:00
}
handleFocus() {
this.hasFocus = true;
emit(this, 'sl-focus');
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-06-15 13:26:35 +00:00
@watch('checked', { waitUntilFirstUpdate: true })
@watch('indeterminate', { waitUntilFirstUpdate: true })
2021-02-26 14:09:13 +00:00
handleStateChange() {
2021-06-16 12:42:02 +00:00
this.invalid = !this.input.checkValidity();
emit(this, 'sl-change');
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"
2021-04-02 11:47:25 +00:00
name=${ifDefined(this.name)}
value=${ifDefined(this.value)}
2021-06-17 20:45:49 +00:00
.indeterminate=${this.indeterminate}
.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;
}
}