kopia lustrzana https://github.com/shoelace-style/shoelace
180 wiersze
5.9 KiB
TypeScript
180 wiersze
5.9 KiB
TypeScript
import { html } from 'lit';
|
|
import { customElement, property, query, state } from 'lit/decorators.js';
|
|
import { classMap } from 'lit/directives/class-map.js';
|
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
import { live } from 'lit/directives/live.js';
|
|
import { defaultValue } from '../../internal/default-value';
|
|
import { FormSubmitController } from '../../internal/form';
|
|
import ShoelaceElement from '../../internal/shoelace-element';
|
|
import { watch } from '../../internal/watch';
|
|
import '../icon/icon';
|
|
import styles from './checkbox.styles';
|
|
import type { CSSResultGroup } from 'lit';
|
|
|
|
/**
|
|
* @summary Checkboxes allow the user to toggle an option on or off.
|
|
*
|
|
* @since 2.0
|
|
* @status stable
|
|
*
|
|
* @dependency sl-icon
|
|
*
|
|
* @slot - The checkbox's label.
|
|
*
|
|
* @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 internal wrapper.
|
|
* @csspart control - The checkbox control.
|
|
* @csspart checked-icon - The checked icon.
|
|
* @csspart indeterminate-icon - The indeterminate icon.
|
|
* @csspart label - The checkbox label.
|
|
*/
|
|
@customElement('sl-checkbox')
|
|
export default class SlCheckbox extends ShoelaceElement {
|
|
static styles: CSSResultGroup = styles;
|
|
|
|
@query('input[type="checkbox"]') input: HTMLInputElement;
|
|
|
|
// @ts-expect-error -- Controller is currently unused
|
|
private readonly formSubmitController = new FormSubmitController(this, {
|
|
value: (control: SlCheckbox) => (control.checked ? control.value || 'on' : undefined),
|
|
defaultValue: (control: SlCheckbox) => control.defaultChecked,
|
|
setValue: (control: SlCheckbox, checked: boolean) => (control.checked = checked)
|
|
});
|
|
|
|
@state() private hasFocus = false;
|
|
|
|
/** Name of the HTML form control. Submitted with the form as part of a name/value pair. */
|
|
@property() name: string;
|
|
|
|
/** Value of the HTML form control. Primarily used to differentiate a list of related checkboxes that have the same name. */
|
|
@property() value: string;
|
|
|
|
/** Disables the checkbox (so the user can't check / uncheck it). */
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
|
|
|
/** Makes the checkbox a required field. */
|
|
@property({ type: Boolean, reflect: true }) required = false;
|
|
|
|
/** Draws the checkbox in a checked state. */
|
|
@property({ type: Boolean, reflect: true }) checked = false;
|
|
|
|
/** Draws the checkbox in an indeterminate state. Usually applies to a checkbox that represents "select all" or "select none" when the items to which it applies are a mix of selected and unselected. */
|
|
@property({ type: Boolean, reflect: true }) indeterminate = false;
|
|
|
|
/** This will be true when the control is in an invalid state. Validity is determined by the `required` prop. */
|
|
@property({ type: Boolean, reflect: true }) invalid = false;
|
|
|
|
/** Gets or sets the default value used to reset this element. The initial value corresponds to the one originally specified in the HTML that created this element. */
|
|
@defaultValue('checked')
|
|
defaultChecked = false;
|
|
|
|
firstUpdated() {
|
|
this.invalid = !this.input.checkValidity();
|
|
}
|
|
|
|
/** Simulates a click on the checkbox. */
|
|
click() {
|
|
this.input.click();
|
|
}
|
|
|
|
/** Sets focus on the checkbox. */
|
|
focus(options?: FocusOptions) {
|
|
this.input.focus(options);
|
|
}
|
|
|
|
/** Removes focus from the checkbox. */
|
|
blur() {
|
|
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() {
|
|
this.checked = !this.checked;
|
|
this.indeterminate = false;
|
|
this.emit('sl-change');
|
|
}
|
|
|
|
handleBlur() {
|
|
this.hasFocus = false;
|
|
this.emit('sl-blur');
|
|
}
|
|
|
|
@watch('disabled', { waitUntilFirstUpdate: true })
|
|
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();
|
|
}
|
|
|
|
handleFocus() {
|
|
this.hasFocus = true;
|
|
this.emit('sl-focus');
|
|
}
|
|
|
|
@watch('checked', { waitUntilFirstUpdate: true })
|
|
@watch('indeterminate', { waitUntilFirstUpdate: true })
|
|
handleStateChange() {
|
|
this.invalid = !this.input.checkValidity();
|
|
}
|
|
|
|
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
|
|
})}
|
|
>
|
|
<input
|
|
class="checkbox__input"
|
|
type="checkbox"
|
|
name=${ifDefined(this.name)}
|
|
value=${ifDefined(this.value)}
|
|
.indeterminate=${live(this.indeterminate)}
|
|
.checked=${live(this.checked)}
|
|
.disabled=${this.disabled}
|
|
.required=${this.required}
|
|
aria-checked=${this.checked ? 'true' : 'false'}
|
|
@click=${this.handleClick}
|
|
@blur=${this.handleBlur}
|
|
@focus=${this.handleFocus}
|
|
/>
|
|
|
|
<span part="control" class="checkbox__control">
|
|
${this.checked ? html` <sl-icon part="checked-icon" library="system" name="check"></sl-icon> ` : ''}
|
|
${!this.checked && this.indeterminate
|
|
? html` <sl-icon part="indeterminate-icon" library="system" name="indeterminate"></sl-icon> `
|
|
: ''}
|
|
</span>
|
|
|
|
<span part="label" class="checkbox__label">
|
|
<slot></slot>
|
|
</span>
|
|
</label>
|
|
`;
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
'sl-checkbox': SlCheckbox;
|
|
}
|
|
}
|