All files / src/form FieldElement.ts

99.65% Statements 293/294
97.14% Branches 34/35
90.9% Functions 10/11
99.65% Lines 293/294

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 29593x 196x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 340x 69x 69x 69x 340x 340x 340x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 925x 93x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 1456x 93x 2848x 2848x 2848x 1183x 1183x 1183x 1183x 1183x 2848x 2848x 1703x 1703x 2848x 1610x 1610x 1145x 1610x 1610x 1610x 2848x 93x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 48x 48x 48x 48x 48x 48x 46x 46x 48x 48x 93x 93x 46x 46x 46x 50x 50x 50x 93x 93x 93x 93x 93x 808x 808x 93x 230x 230x 230x 69x 69x 69x 69x 69x 368x 93x 93x 93x  
import { TemplateResult, html, css } from 'lit';
import { property } from 'lit/decorators.js';
import { RapidElement } from '../RapidElement';
import { renderMarkdownInline } from '../markdown';
 
/**
 * FieldElement is a base class for form components that provides built-in
 * field wrapper functionality, eliminating the need for manual temba-field embedding.
 *
 * Components extending this class only need to implement renderWidget() with their
 * specific widget content, and the field wrapper (label, errors, help text) is
 * automatically handled.
 */
export abstract class FieldElement extends RapidElement {
  @property({ type: String })
  name = '';
 
  @property({ type: String, attribute: 'help_text' })
  helpText: string;
 
  @property({ type: Boolean, attribute: 'widget_only' })
  widgetOnly: boolean;
 
  @property({ type: Array })
  errors: string[];
 
  // Use @property with custom getter/setter to handle both attribute and programmatic access
  private _value: any = '';
 
  @property({ type: String })
  public get value() {
    return this._value;
  }
 
  public set value(value) {
    this._value = value;
  }
 
  @property({ attribute: false })
  inputRoot: HTMLElement = this;
 
  @property({ type: Boolean })
  disabled = false;
 
  static formAssociated = true;
 
  protected internals: ElementInternals;
 
  @property({ type: Boolean })
  hideErrors = false;
 
  @property({ type: Boolean, attribute: 'hide_label' })
  hideLabel: boolean;
 
  @property({ type: String })
  label: string;
 
  constructor() {
    super();
    this.internals = this.attachInternals();
  }
 
  static get styles() {
    return css`
      :host {
        font-family: var(--font-family);
      }
 
      label {
        margin-bottom: 5px;
        margin-left: 4px;
        display: block;
        font-weight: 400;
        font-size: var(--label-size);
        letter-spacing: 0.05em;
        line-height: normal;
        color: var(--color-label, #777);
      }
 
      .help-text {
        font-size: var(--help-text-size);
        line-height: normal;
        color: var(--color-text-help);
        margin-left: var(--help-text-margin-left);
        margin-top: 6px;
        opacity: 1;
      }
 
      .alert-error {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        z-index: 1000;
        background: white;
        border: 1px solid var(--color-error);
        color: var(--color-error);
        padding: 8px 12px;
        margin: 2px 0 0 0;
        border-radius: var(--curvature);
        box-shadow:
          0 4px 6px -1px rgba(0, 0, 0, 0.1),
          0 2px 4px -1px rgba(0, 0, 0, 0.06);
        font-size: 0.85em;
        line-height: 1.2;
        opacity: 0;
        visibility: hidden;
        transform: translateY(-12px);
        transition:
          opacity 0.2s ease-in-out,
          visibility 0.2s ease-in-out,
          transform 0.2s ease-in-out;
      }
 
      .field:hover .alert-error {
        opacity: 1;
        visibility: visible;
        transform: translateY(2px);
      }
 
      /* Hide error popup when widget is focused */
      .field:focus-within .alert-error {
        opacity: 0;
        visibility: hidden;
        transform: translateY(-4px);
      }
 
      .field.has-error {
        position: relative;
        /* Set CSS custom properties that form components can use */
        --color-widget-border: var(--color-error);
        --widget-box-shadow-focused: var(
          --widget-box-shadow-focused-error,
          0 0 0 3px rgba(255, 99, 71, 0.3)
        );
        --color-focus: var(--color-error);
      }
 
      .field.has-error .widget {
        border-radius: var(--curvature-widget);
        position: relative;
      }
 
      /* Force error styling with higher specificity */
      :host(.has-error) .field.has-error .widget .input-container,
      :host(.has-error) .field.has-error .widget .select-container,
      :host(.has-error) .field.has-error .widget .comp-container,
      :host(.has-error) .field.has-error .widget .checkbox-container,
      :host(.has-error) .field.has-error .widget .container,
      :host(.has-error) .field.has-error .widget .range-container,
      .field.has-error .widget .input-container,
      .field.has-error .widget .select-container,
      .field.has-error .widget .comp-container,
      .field.has-error .widget .checkbox-container,
      .field.has-error .widget .container,
      .field.has-error .widget .range-container {
        border-color: var(--color-error) !important;
      }
 
      /* When error field is focused, use error-colored focus ring */
      :host(.has-error) .field.has-error .widget .input-container:focus-within,
      :host(.has-error) .field.has-error .widget .select-container:focus-within,
      :host(.has-error) .field.has-error .widget .select-container.focused,
      :host(.has-error) .field.has-error .widget .comp-container:focus-within,
      :host(.has-error)
        .field.has-error
        .widget
        .checkbox-container:focus-within,
      :host(.has-error) .field.has-error .widget .container:focus-within,
      :host(.has-error) .field.has-error .widget .range-container:focus-within,
      .field.has-error .widget .input-container:focus-within,
      .field.has-error .widget .select-container:focus-within,
      .field.has-error .widget .select-container.focused,
      .field.has-error .widget .comp-container:focus-within,
      .field.has-error .widget .checkbox-container:focus-within,
      .field.has-error .widget .container:focus-within,
      .field.has-error .widget .range-container:focus-within {
        border-color: var(--color-error) !important;
        box-shadow: var(
          --widget-box-shadow-focused-error,
          0 0 0 3px rgba(255, 99, 71, 0.3)
        ) !important;
      }
 
      .alert-error p {
        margin: 0;
        padding: 0;
      }
 
      .disabled {
        opacity: var(--disabled-opacity) !important;
        pointer-events: none !important;
      }
    `;
  }
 
  updated(changedProperties: Map<string, any>): void {
    super.updated(changedProperties);
 
    if (changedProperties.has('value')) {
      this.internals.setFormValue(this.value);
    }
 
    if (
      changedProperties.has('errors') ||
      changedProperties.has('hideErrors')
    ) {
      const hasErrors =
        !this.hideErrors && this.errors && this.errors.length > 0;
      this.classList.toggle('has-error', hasErrors);
    }
  }
 
  get form() {
    return this.internals.form;
  }
 
  public setValue(value: any) {
    this.value = this.serializeValue(value);
  }
 
  public getDeserializedValue(): any {
    if (!this.value || this.value === '') {
      return null;
    }
    return JSON.parse(this.value);
  }
 
  public serializeValue(value: any): string {
    return JSON.stringify(value);
  }
 
  /**
   * Abstract method that components must implement to render their specific widget content.
   * This replaces the need to manually embed temba-field.
   */
  protected abstract renderWidget(): TemplateResult;
 
  /**
   * Renders the complete field including label, widget, errors, and help text.
   * Components can override this for custom layouts, but typically should just implement renderWidget().
   */
  protected renderField(): TemplateResult {
    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}
      `;
    }
 
    return html`
      <div
        class="field ${this.disabled ? 'disabled' : ''} ${hasErrors
          ? 'has-error'
          : ''}"
      >
        ${!!this.name && !this.hideLabel && !!this.label
          ? html`
              <label class="control-label" for="${this.name}"
                >${this.label}</label
              >
            `
          : null}
        <div class="widget">${this.renderWidget()} ${errors}</div>
        ${this.helpText && this.helpText !== 'None'
          ? html`
              <div class="help-text">
                ${renderMarkdownInline(this.helpText)}
              </div>
            `
          : null}
      </div>
    `;
  }
 
  /**
   * Main render method that automatically provides field wrapper functionality.
   * Components extending FieldElement should not override this unless they need custom field layouts.
   */
  public render(): TemplateResult {
    return this.renderField();
  }
}