1- import { DjangoFormProps , HttpRequestProps } from "./types" ;
2- import React from "react " ;
3- import ReactDOM from "react-dom " ;
1+ import type { DjangoFormProps , HttpRequestProps } from "./types" ;
2+ import { useEffect } from "preact/hooks " ;
3+ import { type ComponentChildren , render , createElement } from "preact " ;
44/**
55 * Interface used to bind a ReactPy node to React.
66 */
7- export function bind ( node ) {
7+ export function bind ( node : HTMLElement | Element | Node ) {
88 return {
9- create : ( type , props , children ) =>
10- React . createElement ( type , props , ...children ) ,
11- render : ( element ) => {
12- ReactDOM . render ( element , node ) ;
9+ create : (
10+ type : string ,
11+ props : Record < string , unknown > ,
12+ children : ComponentChildren [ ] ,
13+ ) => createElement ( type , props , ...children ) ,
14+ render : ( element : HTMLElement | Element | Node ) => {
15+ render ( element , node ) ;
1316 } ,
14- unmount : ( ) => ReactDOM . unmountComponentAtNode ( node ) ,
17+ unmount : ( ) => render ( null , node ) ,
1518 } ;
1619}
1720
1821export function DjangoForm ( {
1922 onSubmitCallback,
2023 formId,
2124} : DjangoFormProps ) : null {
22- React . useEffect ( ( ) => {
25+ useEffect ( ( ) => {
2326 const form = document . getElementById ( formId ) as HTMLFormElement ;
2427
2528 // Submission event function
26- const onSubmitEvent = ( event ) => {
29+ const onSubmitEvent = ( event : Event ) => {
2730 event . preventDefault ( ) ;
2831 const formData = new FormData ( form ) ;
2932
3033 // Convert the FormData object to a plain object by iterating through it
3134 // If duplicate keys are present, convert the value into an array of values
3235 const entries = formData . entries ( ) ;
3336 const formDataArray = Array . from ( entries ) ;
34- const formDataObject = formDataArray . reduce ( ( acc , [ key , value ] ) => {
35- if ( acc [ key ] ) {
36- if ( Array . isArray ( acc [ key ] ) ) {
37- acc [ key ] . push ( value ) ;
37+ const formDataObject = formDataArray . reduce < Record < string , unknown > > (
38+ ( acc , [ key , value ] ) => {
39+ if ( acc [ key ] ) {
40+ if ( Array . isArray ( acc [ key ] ) ) {
41+ acc [ key ] . push ( value ) ;
42+ } else {
43+ acc [ key ] = [ acc [ key ] , value ] ;
44+ }
3845 } else {
39- acc [ key ] = [ acc [ key ] , value ] ;
46+ acc [ key ] = value ;
4047 }
41- } else {
42- acc [ key ] = value ;
43- }
44- return acc ;
45- } , { } ) ;
48+ return acc ;
49+ } ,
50+ { } ,
51+ ) ;
4652
4753 onSubmitCallback ( formDataObject ) ;
4854 } ;
@@ -64,7 +70,7 @@ export function DjangoForm({
6470}
6571
6672export function HttpRequest ( { method, url, body, callback } : HttpRequestProps ) {
67- React . useEffect ( ( ) => {
73+ useEffect ( ( ) => {
6874 fetch ( url , {
6975 method : method ,
7076 body : body ,
0 commit comments