@@ -2,21 +2,6 @@ import { ReactPyDjangoClient } from "./client";
22import React from "react" ;
33import ReactDOM from "react-dom" ;
44import { Layout } from "@reactpy/client/src/components" ;
5- import { DjangoFormProps } from "./types" ;
6-
7- /**
8- * Interface used to bind a ReactPy node to React.
9- */
10- export function bind ( node ) {
11- return {
12- create : ( type , props , children ) =>
13- React . createElement ( type , props , ...children ) ,
14- render : ( element ) => {
15- ReactDOM . render ( element , node ) ;
16- } ,
17- unmount : ( ) => ReactDOM . unmountComponentAtNode ( node ) ,
18- } ;
19- }
205
216export function mountComponent (
227 mountElement : HTMLElement ,
@@ -84,7 +69,7 @@ export function mountComponent(
8469 // Replace the prerender element with the real element on the first layout update
8570 if ( client . prerenderElement ) {
8671 client . onMessage ( "layout-update" , ( { path, model } ) => {
87- if ( client . prerenderElement ) {
72+ if ( client . prerenderElement && client . mountElement ) {
8873 client . prerenderElement . replaceWith ( client . mountElement ) ;
8974 client . prerenderElement = null ;
9075 }
@@ -94,51 +79,3 @@ export function mountComponent(
9479 // Start rendering the component
9580 ReactDOM . render ( < Layout client = { client } /> , client . mountElement ) ;
9681}
97-
98- export function DjangoForm ( {
99- onSubmitCallback,
100- formId,
101- } : DjangoFormProps ) : null {
102- React . useEffect ( ( ) => {
103- const form = document . getElementById ( formId ) as HTMLFormElement ;
104-
105- // Submission event function
106- const onSubmitEvent = ( event ) => {
107- event . preventDefault ( ) ;
108- const formData = new FormData ( form ) ;
109-
110- // Convert the FormData object to a plain object by iterating through it
111- // If duplicate keys are present, convert the value into an array of values
112- const entries = formData . entries ( ) ;
113- const formDataArray = Array . from ( entries ) ;
114- const formDataObject = formDataArray . reduce ( ( acc , [ key , value ] ) => {
115- if ( acc [ key ] ) {
116- if ( Array . isArray ( acc [ key ] ) ) {
117- acc [ key ] . push ( value ) ;
118- } else {
119- acc [ key ] = [ acc [ key ] , value ] ;
120- }
121- } else {
122- acc [ key ] = value ;
123- }
124- return acc ;
125- } , { } ) ;
126-
127- onSubmitCallback ( formDataObject ) ;
128- } ;
129-
130- // Bind the event listener
131- if ( form ) {
132- form . addEventListener ( "submit" , onSubmitEvent ) ;
133- }
134-
135- // Unbind the event listener when the component dismounts
136- return ( ) => {
137- if ( form ) {
138- form . removeEventListener ( "submit" , onSubmitEvent ) ;
139- }
140- } ;
141- } , [ ] ) ;
142-
143- return null ;
144- }
0 commit comments