-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/astro-form-elements/components/base-form-group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Observable, ReplaySubject } from "rxjs"; | ||
import type { FormGroupElementChangesValue } from "../types"; | ||
import FormElements from "../form-elements-registry"; | ||
|
||
export interface ElementOptions { | ||
elementName: string; | ||
} | ||
|
||
export interface CustomFormElement { | ||
data: any; | ||
value: any; | ||
} | ||
|
||
export default class BaseFormElement extends HTMLElement { | ||
constructor() { | ||
super(); | ||
const nameDirective = this.getAttribute(this._formElementDirective); | ||
if (!nameDirective) { | ||
throw Error( | ||
`formElementName id is required! | ||
` | ||
); | ||
} | ||
|
||
this.name = nameDirective; | ||
|
||
try { | ||
FormElements.registerElement(this, this.name); | ||
} catch (error) { | ||
throw Error( | ||
`Element with name ${this.name} already exist | ||
` | ||
); | ||
} | ||
} | ||
/** | ||
* Attribute to register a FormElement | ||
*/ | ||
private readonly _formElementDirective = "formElementName"; | ||
/** | ||
* WIP | ||
*/ | ||
private _lastEvent = ""; | ||
/** | ||
* Change whe the user interact with the UI | ||
*/ | ||
private _touched = false; | ||
/** | ||
* WIP | ||
*/ | ||
private _elementChanges$ = new ReplaySubject<FormGroupElementChangesValue>(); | ||
public value: any; | ||
public name: string; | ||
/** | ||
* callback called bt broswer when the element enter in page | ||
*/ | ||
connectedCallback(): void {} | ||
|
||
valueChanges(): Observable<FormElementChangesValue> { | ||
return this._elementChanges$.asObservable(); | ||
} | ||
|
||
/* registerEvent(e: Event | CustomEvent): void { | ||
let input = this.querySelector("[data-elementInput]"); | ||
input?.addEventListener(e.type, e.c); | ||
console.log(e); | ||
} */ | ||
|
||
private _elementChanges() { | ||
this._elementChanges$.next({ | ||
eventType: this._lastEvent, | ||
element: { | ||
name: this.name, | ||
}, | ||
data: { | ||
value: this.value, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
customElements.define("form-element", BaseFormElement); |