{"version":3,"file":"odx-angular-cdk-custom-form-control.mjs","sources":["../../../../libs/angular/cdk/custom-form-control/src/lib/control.directive.ts","../../../../libs/angular/cdk/custom-form-control/src/lib/custom-form-control.ts","../../../../libs/angular/cdk/custom-form-control/src/lib/input-control.directive.ts","../../../../libs/angular/cdk/custom-form-control/src/odx-angular-cdk-custom-form-control.ts"],"sourcesContent":["import { Directive } from '@angular/core';\nimport { injectElement } from '@odx/angular/utils';\n\n@Directive({\n  standalone: true,\n  selector: '[odxControl]',\n  exportAs: 'odxControl',\n})\nexport class ControlDirective {\n  public readonly element = injectElement();\n}\n","import { booleanAttribute, Directive, inject, Input, ViewChild } from '@angular/core';\nimport { ControlValueAccessor, NgControl, RequiredValidator, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';\nimport { Controller, DisabledController, ReadonlyController } from '@odx/angular';\nimport { arrayAttribute, Pure } from '@odx/angular/utils';\nimport { ControlDirective } from './control.directive';\n\n/**\n * An abstract directive that serves as a base for custom form controls, integrating seamlessly with Angular Forms.\n * It incorporates common form control behaviors such as handling disabled and readonly states, validation, and more.\n *\n * This approach allows the creation of highly reusable and consistent form controls within Angular applications,\n * simplifying the integration with Angular Forms and the handling of common form behaviors.\n *\n * Extends Controller to provide a consistent API for managing state changes and subscriptions.\n *\n * @see {Controller}\n */\n@Directive({\n  host: {\n    '[class.has-error]': 'hasError',\n    '[class.is-disabled]': 'isDisabled',\n    '[class.is-readonly]': 'isReadonly',\n    '[class.is-required]': 'isRequired',\n  },\n})\nexport abstract class CustomFormControl<T> extends Controller implements ControlValueAccessor {\n  private static CURRENT_INDEX = 0;\n\n  private readonly requiredValidator = inject(RequiredValidator, { optional: true, self: true });\n  private internalValue: T;\n  protected readonly disabledController = DisabledController.inject();\n  protected readonly readonlyController = ReadonlyController.inject();\n  protected readonly ngControl = inject(NgControl, { self: true, optional: true });\n\n  /**\n   * A unique identifier for the custom form control. If not provided, a unique ID is generated automatically.\n   * This can be useful for associating form control with a label.\n   *\n   * @type {string}\n   */\n  @Input()\n  public id: string | null = null;\n\n  /**\n   * The name attribute for the custom form control. Defaults to a unique name if not provided.\n   * This is useful when the form control is part of a form and needs to be identified.\n   *\n   * @type {string}\n   */\n  @Input()\n  public name = `odx-form-control-${CustomFormControl.CURRENT_INDEX++}`;\n\n  /**\n   * The tabindex attribute for the custom form control. Can be used to control the order of focus navigation.\n   *\n   * @type {string | null}\n   */\n  @Input()\n  public tabindex: string | null = null;\n\n  /**\n   * An array of error codes to ignore. If an error code is included in this array, the form control will not\n   * consider it as an error state. Useful for ignoring specific validation errors.\n   *\n   * @type {string[]}\n   */\n  @Input({ transform: arrayAttribute })\n  public ignoreErrors: string[] = [];\n\n  @Input({ transform: booleanAttribute })\n  public required = false;\n\n  /**\n   * The current value of the form control. Setting this value updates the internal state and marks the control\n   * for checking changes. Getting this value returns the current internal value of the control.\n   *\n   * @example\n   * ```ts\n   * this.customInputControl.value = 'newValue'; // Sets the value\n   * const currentValue = this.customInputControl.value; // Gets the current value\n   * ```\n   */\n  public set value(value: T) {\n    if (this.internalValue !== value) {\n      this.internalValue = value;\n      this.triggerControllerChange();\n    }\n  }\n\n  /**\n   * Gets the value of the custom form control.\n   *\n   * @returns {T} - The value of the custom form control.\n   */\n  public get value(): T {\n    return this.internalValue;\n  }\n\n  /**\n   * Indicates whether the form control currently has an error. This takes into account any ignored errors.\n   *\n   * @returns {boolean} - True if the form control has an error, false otherwise.\n   *\n   * @example\n   * ```ts\n   * if (this.customInputControl.hasError) { ... }\n   * ```\n   */\n  public get hasError(): boolean {\n    return !!this.ngControl?.touched && !!this.ngControl?.invalid && this.hasNonIgnoredError(this.ngControl.errors);\n  }\n\n  /**\n   * Indicates whether the form control is currently disabled.\n   *\n   * @returns {boolean} - True if the form control is disabled, false otherwise.\n   *\n   * @example\n   * ```ts\n   * if (this.customInputControl.isDisabled) { ... }\n   * ```\n   */\n  public get isDisabled(): boolean {\n    return !!this.disabledController?.disabled;\n  }\n\n  /**\n   * Indicates whether the form control is currently in a readonly state.\n   *\n   * @returns {boolean} - True if the form control is readonly, false otherwise.\n   *\n   * @example\n   * ```ts\n   * if (this.customInputControl.isReadonly) { ... }\n   * ```\n   */\n  public get isReadonly(): boolean {\n    return !!this.readonlyController?.readonly;\n  }\n\n  /**\n   * Indicates whether the form control is marked as required, either through Validators.required, Validators.requiredTrue,\n   * or by a required attribute on a parent form field.\n   *\n   * @returns {boolean} - True if the form control is required, false otherwise.\n   *\n   * @example\n   * ````ts\n   * if (this.customInputControl.isRequired) { ... }\n   * ````\n   */\n  public get isRequired(): boolean {\n    return this.required || !!this.requiredValidator?.required || this.hasValidator(Validators.required) || this.hasValidator(Validators.requiredTrue);\n  }\n\n  /**\n   * A reference to the `ControlDirective` if it exists within the same host. This allows for direct interaction\n   * with the underlying form control directive.\n   *\n   * @type {ControlDirective | null}\n   *\n   * @example\n   * ```ts\n   * this.customInputControl.control?.focus();\n   * ```\n   */\n  @ViewChild(ControlDirective)\n  public readonly control: ControlDirective | null = null;\n\n  // eslint-disable-next-line @angular-eslint/prefer-inject\n  constructor(initialValue: T) {\n    super();\n    this.internalValue = initialValue;\n    if (this.ngControl) {\n      this.ngControl.valueAccessor = this;\n    }\n  }\n\n  /** @internal */\n  public onTouched() {\n    // implemented by control value accessor\n  }\n\n  /** @internal */\n  public onChange(_value: T) {\n    // implemented by control value accessor\n  }\n\n  /** @internal */\n  public registerOnChange(fn: (value: T) => void): void {\n    this.onChange = fn;\n  }\n  /** @internal */\n  public registerOnTouched(fn: () => void): void {\n    this.onTouched = fn;\n  }\n\n  /** @internal */\n  public setDisabledState(isDisabled: boolean): void {\n    this.disabledController?.setDisabledState(isDisabled);\n  }\n\n  /** @internal */\n  public writeValue(value: T): void {\n    this.value = value;\n    this.onChange(value);\n  }\n\n  /** @internal */\n  public updateValue(value: T): void {\n    if (this.isDisabled) return;\n    this.writeValue(value);\n    this.onTouched();\n  }\n\n  protected hasValidator(validator: ValidatorFn): boolean {\n    return !!this.ngControl?.control?.hasValidator(validator);\n  }\n\n  @Pure\n  private hasNonIgnoredError(errors: ValidationErrors | null): boolean {\n    if (!errors) return false;\n\n    return Object.keys(errors).some((error) => !this.ignoreErrors.includes(error));\n  }\n}\n","import { Directive } from '@angular/core';\nimport { injectElement, untilDestroyed } from '@odx/angular/utils';\nimport { distinctUntilChanged, fromEvent, map } from 'rxjs';\n\n/**\n * An abstract directive that facilitates handling input changes, focusing, and blurring programmatically for input elements.\n * It provides a reactive `valueChange$` observable to subscribe to input changes.\n * This directive can be extended to create more specific input handlers.\n *\n * @example\n * Extending InputControlDirective in a custom directive:\n * ```ts\n * @Directive({\n *   selector: '[appCustomInput]',\n *   standalone: true,\n * })\n * export class CustomInputDirective extends InputControlDirective {\n *   // Custom logic here\n * }\n * ```\n *\n * Using the directive in a component template:\n * ```html\n * <input appCustomInput type=\"text\" />\n * ```\n *\n * Subscribing to input changes in the component:\n * ```ts\n * @Component({...})\n * export class MyComponent implements AfterViewInit {\n *   @ViewChild(CustomInputDirective) customInputDirective!: CustomInputDirective;\n *\n *   ngAfterViewInit(): void {\n *     this.customInputDirective.valueChange$.subscribe(value => {\n *       console.log('Input value changed to:', value);\n *     });\n *   }\n * }\n *\n * // Focusing the input programmatically\n * this.customInputDirective.focus();\n *\n * // Blurring the input programmatically\n * this.customInputDirective.blur();\n *\n * // Resetting the input value programmatically\n * this.customInputDirective.reset();\n * ```\n */\n@Directive({\n  standalone: true,\n})\nexport abstract class InputControlDirective {\n  protected readonly takeUntilDestroyed = untilDestroyed();\n\n  public readonly element = injectElement<HTMLInputElement>();\n\n  /**\n   * Represents an observable stream of value changes for the input control.\n   *\n   * @emits {string} input value changes.\n   */\n  public valueChange$ = fromEvent(this.element.nativeElement, 'input').pipe(\n    distinctUntilChanged(),\n    map(() => this.nativeElementValue),\n  );\n\n  /**\n   * Gets the current value of the input element.\n   *\n   * @type {string} The current value of the input element.\n   */\n  public get nativeElementValue(): string {\n    return this.element.nativeElement.value;\n  }\n\n  /**\n   * Sets the value of the input element.\n   *\n   * @param {string} value - The value to set.\n   */\n  public set nativeElementValue(value: string) {\n    this.element.nativeElement.value = value;\n  }\n\n  /**\n   * Sets focus on the input element.\n   *\n   * @returns {void}\n   */\n  public focus(): void {\n    this.element.nativeElement.focus();\n  }\n\n  /**\n   * Removes focus from the input element.\n   *\n   * @returns {void}\n   */\n  public blur(): void {\n    this.element.nativeElement.blur();\n  }\n\n  /**\n   * Resets the input element value to an empty string.\n   *\n   * @returns {void}\n   */\n  public reset(): void {\n    this.element.nativeElement.value = '';\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;;MAQa,gBAAgB,CAAA;AAL7B,IAAA,WAAA,GAAA;QAMkB,IAAA,CAAA,OAAO,GAAG,aAAa,EAAE;AAC1C,IAAA;+GAFY,gBAAgB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,SAAA,EAAA,CAAA,CAAA;mGAAhB,gBAAgB,EAAA,YAAA,EAAA,IAAA,EAAA,QAAA,EAAA,cAAA,EAAA,QAAA,EAAA,CAAA,YAAA,CAAA,EAAA,QAAA,EAAA,EAAA,EAAA,CAAA,CAAA;;4FAAhB,gBAAgB,EAAA,UAAA,EAAA,CAAA;kBAL5B,SAAS;AAAC,YAAA,IAAA,EAAA,CAAA;AACT,oBAAA,UAAU,EAAE,IAAI;AAChB,oBAAA,QAAQ,EAAE,cAAc;AACxB,oBAAA,QAAQ,EAAE,YAAY;AACvB,iBAAA;;;ACDD;;;;;;;;;;AAUG;AASG,MAAgB,iBAAqB,SAAQ,UAAU,CAAA;aAC5C,IAAA,CAAA,aAAa,GAAG,CAAH,CAAK;AA8CjC;;;;;;;;;AASG;IACH,IAAW,KAAK,CAAC,KAAQ,EAAA;AACvB,QAAA,IAAI,IAAI,CAAC,aAAa,KAAK,KAAK,EAAE;AAChC,YAAA,IAAI,CAAC,aAAa,GAAG,KAAK;YAC1B,IAAI,CAAC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