shoelace/src/components/checkbox/checkbox.ts

230 wiersze
7.7 KiB
TypeScript
Czysty Zwykły widok Historia

2023-01-13 20:43:55 +00:00
import '../icon/icon';
2021-09-29 12:40:26 +00:00
import { classMap } from 'lit/directives/class-map.js';
2023-01-13 20:43:55 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
import { defaultValue } from '../../internal/default-value';
import { FormControlController } from '../../internal/form';
2023-01-13 20:43:55 +00:00
import { html } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
2022-03-24 12:01:09 +00:00
import { watch } from '../../internal/watch';
2023-01-13 20:43:55 +00:00
import ShoelaceElement from '../../internal/shoelace-element';
2021-07-10 00:45:44 +00:00
import styles from './checkbox.styles';
import type { CSSResultGroup } from 'lit';
2023-01-13 20:43:55 +00:00
import type { ShoelaceFormControl } from '../../internal/shoelace-element';
2021-02-26 14:09:13 +00:00
/**
Enrich components `@summary` with description from docs (#962) * keep header styles with repositioned description text * `animated-image` move description to component * code style * `avatar` add summary from docs * `badge` add summary from docs * `breadcrumb` add summary from docs * `button` add summary from docs * lead sentence is now part of the header * `button-group` add summary from docs * `card` add summary from docs * `checkbox` add summary from docs * `color-picker` add summary from docs * `details` add summary from docs * `dialog` add summary from docs * `divider` add summary from docs * `drawer` add summary from docs * `dropdown` add summary from docs * `format-bytes` add summary from docs * `format-date` add summary from docs * `format-number` add summary from docs * `icon` add summary from docs * `icon-button` add summary from docs * `image-comparer` add summary from docs * `include` add summary from docs * `input` add summary from docs * `menu` add summary from docs * `menu-item` add summary from docs * `menu-label` add summary from docs * `popup` add summary from docs * `progressbar` add summary from docs * `progress-ring` add summary from docs * `radio` add summary from docs * `radio-button` add summary from docs * `range` add summary from docs * `rating` add summary from docs * `relative-time` add summary from docs * `select` add summary from docs * `skeleton` add summary from docs * `spinner` add summary from docs * `split-panel` add summary from docs * `switch` add summary from docs * `tab-group` add summary from docs * `tag` add summary from docs * `textarea` add summary from docs * `tooltip` add summary from docs * `visually-hidden` add summary from docs * `animation` add summary from docs * `breadcrumb-item` add summary from docs * `mutation-observer` add summary from docs * `radio-group` add summary from docs * `resize-observer` add summary from docs * `tab` add summary from docs * `tab-panel` add summary from docs * `tree` add summary from docs * `tree-item` add summary from docs * remove `title` for further usage of `Sl` classnames in docs * revert: use markdown parser for component summary
2022-10-21 13:56:35 +00:00
* @summary Checkboxes allow the user to toggle an option on or off.
2023-01-12 15:26:25 +00:00
* @documentation https://shoelace.style/components/checkbox
2021-02-26 14:09:13 +00:00
* @status stable
2023-01-12 15:26:25 +00:00
* @since 2.0
2021-02-26 14:09:13 +00:00
*
2022-11-10 17:27:03 +00:00
* @dependency sl-icon
*
2021-06-25 20:25:46 +00:00
* @slot - The checkbox's label.
2021-02-26 14:09:13 +00:00
*
2022-12-06 16:18:14 +00:00
* @event sl-blur - Emitted when the checkbox loses focus.
* @event sl-change - Emitted when the checked state changes.
* @event sl-focus - Emitted when the checkbox gains focus.
2022-12-07 22:40:46 +00:00
* @event sl-input - Emitted when the checkbox receives input.
*
2022-12-06 16:18:14 +00:00
* @csspart base - The component's base wrapper.
* @csspart control - The square container that wraps the checkbox's checked state.
* @csspart control--checked - Matches the control part when the checkbox is checked.
* @csspart control--indeterminate - Matches the control part when the checkbox is indeterminate.
* @csspart checked-icon - The checked icon, an `<sl-icon>` element.
* @csspart indeterminate-icon - The indeterminate icon, an `<sl-icon>` element.
* @csspart label - The container that wraps the checkbox's label.
2021-02-26 14:09:13 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-checkbox')
export default class SlCheckbox extends ShoelaceElement implements ShoelaceFormControl {
static styles: CSSResultGroup = styles;
2021-03-06 17:01:39 +00:00
private readonly formControlController = new FormControlController(this, {
value: (control: SlCheckbox) => (control.checked ? control.value || 'on' : undefined),
defaultValue: (control: SlCheckbox) => control.defaultChecked,
setValue: (control: SlCheckbox, checked: boolean) => (control.checked = checked)
});
2021-03-06 17:01:39 +00:00
2023-01-03 20:04:07 +00:00
@query('input[type="checkbox"]') input: HTMLInputElement;
@state() private hasFocus = false;
2023-01-03 20:04:07 +00:00
2022-11-30 20:45:21 +00:00
@property() title = ''; // make reactive to pass through
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The name of the checkbox, submitted as a name/value pair with form data. */
@property() name = '';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The current value of the checkbox, submitted as a name/value pair with form data. */
2021-04-02 11:47:25 +00:00
@property() value: string;
2021-02-26 14:09:13 +00:00
2022-12-13 14:28:12 +00:00
/** The checkbox's size. */
@property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium';
2022-12-06 16:18:14 +00:00
/** Disables the checkbox. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-02-26 14:09:13 +00:00
/** Draws the checkbox in a checked state. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) checked = false;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/**
* Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents a "select
* all/none" behavior when associated checkboxes have a mix of checked and unchecked states.
*/
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) indeterminate = false;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The default value of the form control. Primarily used for resetting the form control. */
@defaultValue('checked') defaultChecked = false;
2023-01-13 17:34:33 +00:00
/**
* By default, form controls are associated with the nearest containing `<form>` element. This attribute allows you
* to place the form control outside of a form and associate it with the form that has this `id`. The form must be in
* the same document or shadow root for this to work.
*/
@property({ reflect: true }) form = '';
/** Makes the checkbox a required field. */
@property({ type: Boolean, reflect: true }) required = false;
2021-03-06 17:01:39 +00:00
firstUpdated() {
this.formControlController.updateValidity();
2021-02-26 14:09:13 +00:00
}
2023-01-03 20:04:07 +00:00
private handleClick() {
this.checked = !this.checked;
this.indeterminate = false;
this.emit('sl-change');
}
private handleBlur() {
this.hasFocus = false;
this.emit('sl-blur');
}
private handleInput() {
this.emit('sl-input');
}
private handleFocus() {
this.hasFocus = true;
this.emit('sl-focus');
}
@watch('disabled', { waitUntilFirstUpdate: true })
handleDisabledChange() {
// Disabled form controls are always valid
this.formControlController.setValidity(this.disabled);
2023-01-03 20:04:07 +00:00
}
@watch(['checked', 'indeterminate'], { waitUntilFirstUpdate: true })
2023-01-03 20:04:07 +00:00
handleStateChange() {
this.input.checked = this.checked; // force a sync update
this.input.indeterminate = this.indeterminate; // force a sync update
this.formControlController.updateValidity();
2023-01-03 20:04:07 +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();
}
2022-12-06 16:18:14 +00:00
/** Checks for validity but does not show a validation message. Returns true when valid and false when invalid. */
checkValidity() {
return this.input.checkValidity();
}
2022-12-06 16:18:14 +00:00
/** Checks for validity and shows a validation message if the control is invalid. */
2021-02-26 14:09:13 +00:00
reportValidity() {
return this.input.reportValidity();
}
2022-12-06 16:18:14 +00:00
/**
* Sets a custom validation message. The value provided will be shown to the user when the form is submitted. To clear
* the custom validation message, call this method with an empty string.
*/
2021-02-26 14:09:13 +00:00
setCustomValidity(message: string) {
this.input.setCustomValidity(message);
this.formControlController.updateValidity();
2021-02-26 14:09:13 +00:00
}
render() {
//
// NOTE: we use a <div> around the label slot because of this Chrome bug.
//
// https://bugs.chromium.org/p/chromium/issues/detail?id=1413733
//
2021-02-26 14:09:13 +00:00
return html`
<label
part="base"
class=${classMap({
checkbox: true,
'checkbox--checked': this.checked,
'checkbox--disabled': this.disabled,
'checkbox--focused': this.hasFocus,
2022-12-13 14:28:12 +00:00
'checkbox--indeterminate': this.indeterminate,
'checkbox--small': this.size === 'small',
'checkbox--medium': this.size === 'medium',
'checkbox--large': this.size === 'large'
2021-02-26 14:09:13 +00:00
})}
>
2021-08-27 12:38:11 +00:00
<input
2021-08-27 12:50:31 +00:00
class="checkbox__input"
2021-08-27 12:38:11 +00:00
type="checkbox"
title=${this.title /* An empty title prevents browser validation tooltips from appearing on hover */}
name=${this.name}
2021-08-27 12:38:11 +00:00
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}
2022-12-07 22:40:46 +00:00
@input=${this.handleInput}
2021-08-27 12:38:11 +00:00
@blur=${this.handleBlur}
@focus=${this.handleFocus}
/>
<span
part="control${this.checked ? ' control--checked' : ''}${this.indeterminate ? ' control--indeterminate' : ''}"
class="checkbox__control"
>
2022-12-13 14:28:12 +00:00
${this.checked
? html`
<sl-icon part="checked-icon" class="checkbox__checked-icon" library="system" name="check"></sl-icon>
`
: ''}
2021-02-26 14:09:13 +00:00
${!this.checked && this.indeterminate
2022-12-13 14:28:12 +00:00
? html`
<sl-icon
part="indeterminate-icon"
class="checkbox__indeterminate-icon"
library="system"
name="indeterminate"
></sl-icon>
`
2021-02-26 14:09:13 +00:00
: ''}
</span>
<div part="label" class="checkbox__label">
<slot></slot>
</div>
2021-02-26 14:09:13 +00:00
</label>
`;
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-checkbox': SlCheckbox;
}
}