1- const template = document . getElementById ( 'profile-field' ) ;
1+ import { defineCustomElement } from '/define-custom-element.mjs' ;
22
33class ProfileField extends HTMLElement {
4- constructor ( ) {
5- super ( ) ;
6- this . attachShadow ( { mode : 'open' } ) ;
7- const content = template . content . cloneNode ( true ) ;
8- this . shadowRoot . append ( content ) ;
9- this . labelEl = this . shadowRoot . getElementById ( 'label' ) ;
10- this . inputEl = this . shadowRoot . getElementById ( 'input' ) ;
11- this . textareaEl = this . shadowRoot . getElementById ( 'textarea' ) ;
12- this . errorEl = this . shadowRoot . getElementById ( 'error' ) ;
13-
4+ connectedCallback ( ) {
145 const emit = ( ) => {
156 const event = new CustomEvent ( 'field-change' , {
167 detail : { name : this . fieldName , value : this . value } ,
@@ -19,15 +10,8 @@ class ProfileField extends HTMLElement {
1910 } ) ;
2011 this . dispatchEvent ( event ) ;
2112 } ;
22- this . inputEl . addEventListener ( 'input' , emit ) ;
23- this . textareaEl . addEventListener ( 'input' , emit ) ;
24- }
25-
26- static get observedAttributes ( ) {
27- return [ 'name' , 'label' , 'type' , 'value' , 'error' , 'multiline' , 'disabled' ] ;
28- }
29-
30- connectedCallback ( ) {
13+ this . elements . inputEl . addEventListener ( 'input' , emit ) ;
14+ this . elements . textareaEl . addEventListener ( 'input' , emit ) ;
3115 this . render ( ) ;
3216 }
3317
@@ -41,28 +25,51 @@ class ProfileField extends HTMLElement {
4125
4226 get value ( ) {
4327 return this . hasAttribute ( 'multiline' )
44- ? this . textareaEl . value
45- : this . inputEl . value ;
28+ ? this . elements . textareaEl . value
29+ : this . elements . inputEl . value ;
4630 }
4731
4832 render ( ) {
4933 const multiline = this . hasAttribute ( 'multiline' ) ;
5034 const disabled = this . hasAttribute ( 'disabled' ) ;
5135 const id = `field-${ this . fieldName } ` ;
5236
53- this . inputEl . hidden = multiline ;
54- this . textareaEl . hidden = ! multiline ;
37+ this . elements . inputEl . hidden = multiline ;
38+ this . elements . textareaEl . hidden = ! multiline ;
5539
56- const active = multiline ? this . textareaEl : this . inputEl ;
57- this . labelEl . setAttribute ( 'for' , id ) ;
40+ const active = multiline ? this . elements . textareaEl : this . elements . inputEl ;
41+ this . elements . labelEl . setAttribute ( 'for' , id ) ;
5842 active . id = id ;
59- this . labelEl . textContent = this . getAttribute ( 'label' ) || this . fieldName ;
43+ this . elements . labelEl . textContent =
44+ this . getAttribute ( 'label' ) || this . fieldName ;
6045
61- if ( ! multiline ) this . inputEl . type = this . getAttribute ( 'type' ) || 'text' ;
46+ if ( ! multiline ) {
47+ this . elements . inputEl . type = this . getAttribute ( 'type' ) || 'text' ;
48+ }
6249 active . value = this . getAttribute ( 'value' ) || '' ;
6350 active . disabled = disabled ;
64- this . errorEl . setAttribute ( 'message' , this . getAttribute ( 'error' ) || '' ) ;
51+ this . elements . errorEl . setAttribute (
52+ 'message' ,
53+ this . getAttribute ( 'error' ) || '' ,
54+ ) ;
6555 }
6656}
6757
68- customElements . define ( 'profile-field' , ProfileField ) ;
58+ defineCustomElement ( ProfileField , {
59+ name : 'profile-field' ,
60+ observedAttributes : [
61+ 'name' ,
62+ 'label' ,
63+ 'type' ,
64+ 'value' ,
65+ 'error' ,
66+ 'multiline' ,
67+ 'disabled' ,
68+ ] ,
69+ elements : {
70+ labelEl : 'label' ,
71+ inputEl : 'input' ,
72+ textareaEl : 'textarea' ,
73+ errorEl : 'error' ,
74+ } ,
75+ } ) ;
0 commit comments