/** * Copyright Aquera Inc 2023 * * This source code is licensed under the BSD-3-Clause license found in the * LICENSE file in the root directory of this source tree. */ import { LitElement, html, CSSResultArray, TemplateResult, } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { styles } from './nile-checkbox.css'; import { classMap } from 'lit/directives/class-map.js'; import { query, state } from 'lit/decorators.js'; import { defaultValue } from '../internal/default-value'; import { ifDefined } from 'lit/directives/if-defined.js'; import { live } from 'lit/directives/live.js'; import { watch } from '../internal/watch'; import NileElement from '../internal/nile-element'; import type { CSSResultGroup } from 'lit'; /** * @summary Checkboxes allow the user to toggle an option on or off. * * @dependency nile-icon * * @slot - The checkbox's label. * * @event nile-blur - Emitted when the checkbox loses focus. * @event nile-change - Emitted when the checked state changes. * @event nile-focus - Emitted when the checkbox gains focus. * @event nile-input - Emitted when the checkbox receives input. * @event nile-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied. * * @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 `` element. * @csspart indeterminate-icon - The indeterminate icon, an `` element. * @csspart label - The container that wraps the checkbox's label. */ /** * Nile icon component. * * @tag nile-checkbox * */ @customElement('nile-checkbox') export class NileCheckbox extends NileElement { constructor() { super(); } static styles: CSSResultGroup = styles; @query('input[type="checkbox"]') input: HTMLInputElement; @state() private hasFocus = false; @property() title = ''; // make reactive to pass through /** The name of the checkbox, submitted as a name/value pair with form data. */ @property() name = ''; /** The current value of the checkbox, submitted as a name/value pair with form data. */ @property() value: boolean; /** The checkbox's size. */ @property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium'; /** Disables the checkbox. */ @property({ type: Boolean, reflect: true }) disabled = false; /** Draws the checkbox in a checked state. */ @property({ type: Boolean, reflect: true }) checked = false; /** Label, declared this property for backward compatibility of old component */ @property({ type: String, reflect: true }) label = ''; /** * 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. */ @property({ type: Boolean, reflect: true }) indeterminate = false; /** The default value of the form control. Primarily used for resetting the form control. */ @defaultValue('checked') defaultChecked = false; @property({ attribute: 'help-text', reflect: true }) helpText = ''; @property({ attribute: 'error-message', reflect: true }) errorMessage = ''; @property({ type: Boolean }) showHelpText = false; /** * By default, form controls are associated with the nearest containing `
` 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 = ''; /** Associates this checkbox with a nile-checkbox-group via a shared group name. */ @property({ reflect: true }) group = ''; /** Makes the checkbox a required field. */ @property({ type: Boolean, reflect: true }) required = false; private toggleHelpText() { this.showHelpText = !this.showHelpText; } private handleClick() { this.checked = !this.checked; this.indeterminate = false; this.emit('nile-change',{checked: this.checked}) this.emit('valueChange',{checked: this.checked}) } private handleBlur() { this.hasFocus = false; this.emit('blur'); } private handleInput() { this.emit('input'); } private handleFocus() { this.hasFocus = true; this.emit('focus'); } @watch(['checked', 'indeterminate'], { waitUntilFirstUpdate: true }) handleStateChange() { this.input.checked = this.checked; // force a sync update this.input.indeterminate = this.indeterminate; // force a sync update } /** 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(); } connectedCallback() { super.connectedCallback(); this.updateHostClass(); this.emit('nile-init'); } disconnectedCallback() { super.disconnectedCallback(); this.emit('nile-destroy'); } updated(changedProperties: Map) { super.updated(changedProperties); if (changedProperties.has('helpText')) { this.updateHostClass(); } } private updateHostClass() { if (this.helpText) { this.classList.add('full-width'); } else { this.classList.remove('full-width'); } } render() { const hasHelpText = this.helpText ? true : false; const hasErrorMessage = this.errorMessage ? true : false; return html` ${hasHelpText ? html` ${this.helpText} ` : ``} ${hasErrorMessage ? html` ${this.errorMessage} ` : ``} `; } } export default NileCheckbox; declare global { interface HTMLElementTagNameMap { 'nile-checkbox': NileCheckbox; } }