@@ -2,7 +2,16 @@ import { dedupeMixin } from '@lion/core';
22import { formRegistrarManager } from './formRegistrarManager.js' ;
33
44/**
5- * This will forward
5+ * This allows to register fields within a form even though they are not within the same dom tree.
6+ * It does that by redispatching the event on the registration target.
7+ * Neither form or field need to know about the portal. It acts as if the field is part of the dom tree.
8+ *
9+ * @example
10+ * <my-form></my-form>
11+ * <my-portal .registrationTarget=${document.querySelector('my-form')}>
12+ * <my-field></my-field>
13+ * </my-portal>
14+ * // my-field will be registered within my-form
615 */
716export const FormRegistrarPortalMixin = dedupeMixin (
817 superclass =>
@@ -11,6 +20,7 @@ export const FormRegistrarPortalMixin = dedupeMixin(
1120 constructor ( ) {
1221 super ( ) ;
1322 this . formElements = [ ] ;
23+ this . registrationTarget = undefined ;
1424 this . __readyForRegistration = false ;
1525 this . registrationReady = new Promise ( resolve => {
1626 this . __resolveRegistrationReady = resolve ;
@@ -21,13 +31,16 @@ export const FormRegistrarPortalMixin = dedupeMixin(
2131 if ( super . connectedCallback ) {
2232 super . connectedCallback ( ) ;
2333 }
34+ this . __checkRegistrationTarget ( ) ;
35+
2436 formRegistrarManager . add ( this ) ;
37+
2538 this . __redispatchEventForFormRegistrarPortalMixin = ev => {
2639 ev . stopPropagation ( ) ;
27- // TODO: fire event with changed ev.target
28- this . dispatchEvent (
40+ // TODO: change ev.target to original registering element
41+ this . registrationTarget . dispatchEvent (
2942 new CustomEvent ( 'form-element-register' , {
30- detail : { element : ev . element } ,
43+ detail : { element : ev . detail . element } ,
3144 bubbles : true ,
3245 } ) ,
3346 ) ;
@@ -43,6 +56,10 @@ export const FormRegistrarPortalMixin = dedupeMixin(
4356 super . disconnectedCallback ( ) ;
4457 }
4558 formRegistrarManager . remove ( this ) ;
59+ this . removeEventListener (
60+ 'form-element-register' ,
61+ this . __redispatchEventForFormRegistrarPortalMixin ,
62+ ) ;
4663 }
4764
4865 firstUpdated ( changedProperties ) {
@@ -51,5 +68,11 @@ export const FormRegistrarPortalMixin = dedupeMixin(
5168 this . __readyForRegistration = true ;
5269 formRegistrarManager . becomesReady ( this ) ;
5370 }
71+
72+ __checkRegistrationTarget ( ) {
73+ if ( ! this . registrationTarget ) {
74+ throw new Error ( 'A FormRegistrarPortal element requires a .registrationTarget' ) ;
75+ }
76+ }
5477 } ,
5578) ;
0 commit comments