All files / src/form Checkbox.ts

98.46% Statements 257/261
91.3% Branches 42/46
87.5% Functions 7/8
98.46% Lines 257/261

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262125x 189x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 110x 110x 110x 110x 110x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 424x 424x 424x 125x 500x 500x 122x 122x     122x 122x   122x 122x 500x 500x 500x 500x 500x 459x 459x 459x 459x 95x 364x 364x 364x 364x 364x 459x 459x 459x 459x 35x 35x 125x   8x 8x 8x 7x 7x 3x 3x 3x 500x 1x 1x 1x 1x 1x 1x 500x 15x 15x 15x 15x 15x 15x 402x 402x 402x 499x 499x 500x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 500x 500x 500x 165x 500x 165x 165x 165x 10x 500x 500x 7x 7x 7x 7x 7x 7x 7x 500x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
import { TemplateResult, html, css } from 'lit';
import { FieldElement } from './FieldElement';
import { property } from 'lit/decorators.js';
import { Icon } from '../Icons';
import { renderMarkdownInline } from '../markdown';
 
export class Checkbox extends FieldElement {
  static get styles() {
    return css`
      ${super.styles}
 
      :host {
        color: var(--color-text);
        display: inline-block;
      }
 
      :host([label]) {
        width: 100%;
      }
 
      .checkbox-background {
        position: absolute;
        margin-top: 2px;
        margin-left: 2px;
        width: 12px;
        height: 12px;
        background: var(--checkbox-background, rgba(255, 255, 255, 0.8));
        border-radius: var(--r-xs);
      }
 
      .wrapper.label {
        padding: var(--checkbox-padding, 10px);
        border-radius: var(--curvature);
      }
 
      .wrapper.label:hover {
        background: var(--checkbox-hover-bg, var(--sunken));
      }
 
      .checkbox-container {
        position: relative;
        cursor: pointer;
        display: flex;
        align-items: flex-start;
        user-select: none;
        -webkit-user-select: none;
      }
 
      .checkbox-container temba-icon {
        align-self: flex-start;
        vertical-align: top;
        line-height: 1;
      }
 
      .label-and-help {
        flex-grow: 1;
        margin-left: 8px;
      }
 
      .checkbox-label {
        font-family: var(--font-family);
        padding: 0px;
        margin: 0px;
        font-size: 14px;
        line-height: 19px;
      }
 
      .checkbox-help-text {
        font-family: var(--font-family);
        font-size: var(--help-text-size, 0.85em);
        line-height: normal;
        color: var(--color-text-help);
        margin-top: 4px;
        opacity: 1;
      }
 
      /* Checkbox help text should align with the checkbox icon, not indented */
      .help-text {
        margin-left: 0;
      }
 
      .far {
        height: 16px;
        margin-top: 1px;
      }
 
      .disabled {
        cursor: not-allowed;
        --icon-color: #ccc;
      }
 
      /* While a change driven by this checkbox is in flight, the glyph
         is swapped for spinning arrows to signal work is underway, and the
         white checkbox backing is hidden so the circular arrows aren't
         framed by a square. Clicks are ignored in handleClick so the user
         can't re-fire the same toggle. */
      .checkbox-container.busy .checkbox-background {
        display: none;
      }
    `;
  }
 
  @property({ type: String })
  name = '';
 
  @property({ type: Boolean })
  checked: boolean;
 
  @property({ type: Boolean })
  partial: boolean;
 
  @property({ type: Boolean })
  disabled = false;
 
  // When set, the checkbox glyph is replaced by spinning arrows to indicate
  // a change it drives is in flight, and clicks are ignored until the host
  // clears it.
  @property({ type: Boolean })
  busy = false;
 
  @property({ type: Number })
  size = 1.2;
 
  @property({ type: String })
  animateChange = 'pulse';
 
  private initialized = false;
 
  public connectedCallback() {
    super.connectedCallback();
  }
 
  public updated(changes: Map<string, any>) {
    super.updated(changes);
 
    // Normalize label property changes
    if (changes.has('label')) {
      // Normalize whitespace labels to empty string for proper behavior
      if (
        this.label &&
        typeof this.label === 'string' &&
        this.label.trim() === ''
      ) {
        this.label = '';
      }
      // Ensure undefined labels remain as null to match test expectations
      if (this.label === undefined) {
        this.label = null;
      }
    }
 
    if (changes.has('checked') || changes.has('value')) {
      if (this.checked || this.partial) {
        this.internals.setFormValue(this.value || '1');
      } else {
        this.internals.setFormValue(undefined);
      }
      // Only fire change after the initial render
      if (this.initialized) {
        this.fireEvent('change');
      }
    }
 
    this.initialized = true;
  }
 
  public serializeValue(value: any): string {
    return value;
  }
 
  private handleClick(): void {
    if (!this.disabled && !this.busy) {
      this.checked = !this.checked;
    }
  }
 
  public click(): void {
    this.handleClick();
    super.click();
  }
 
  protected renderWidget(): TemplateResult {
    const icon = this.busy
      ? html`<temba-icon
          name="${Icon.progress_spinner}"
          size="${this.size}"
          spin
        ></temba-icon>`
      : html`<temba-icon
          name="${this.checked
            ? Icon.checkbox_checked
            : this.partial
              ? Icon.checkbox_partial
              : Icon.checkbox}"
          size="${this.size}"
          animatechange="${this.animateChange}"
        ></temba-icon>`;
 
    return html`
      <div
        class="wrapper ${this.label ? 'label' : ''}"
        @click=${this.handleClick}
      >
        <div
          class="checkbox-container ${this.disabled ? 'disabled' : ''} ${this
            .busy
            ? 'busy'
            : ''}"
        >
          <div class="checkbox-background"></div>
          ${icon}
 
          <div class="label-and-help">
            ${this.label && String(this.label).trim()
              ? html`<div class="checkbox-label">${this.label}</div>`
              : null}
            ${this.helpText && this.helpText !== 'None'
              ? html` <div class="checkbox-help-text">${this.helpText}</div> `
              : null}
          </div>
        </div>
      </div>
    `;
  }
 
  protected renderField(): TemplateResult {
    // Use standard FieldElement behavior but skip the field label since checkbox renders its own inline
    const hasErrors = !this.hideErrors && this.errors && this.errors.length > 0;
    const errors = hasErrors
      ? this.errors.map((error: string) => {
          return html`
            <div class="alert-error">${renderMarkdownInline(error)}</div>
          `;
        })
      : [];
 
    if (this.widgetOnly) {
      return html`
        <div class="${this.disabled ? 'disabled' : ''}">
          ${this.renderWidget()}
        </div>
        ${errors}
      `;
    }
 
    // This matches FieldElement.renderField() but without the field label
    return html`
      <div
        class="field ${this.disabled ? 'disabled' : ''} ${hasErrors
          ? 'has-error'
          : ''}"
      >
        <div class="widget">${this.renderWidget()} ${errors}</div>
      </div>
    `;
  }
 
  public render(): TemplateResult {
    return this.renderField();
  }
}