-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
1 parent
5596d8d
commit 464b4e2
Showing
33 changed files
with
683 additions
and
524 deletions.
There are no files selected for viewing
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
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
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,4 @@ | ||
from django.db import models | ||
|
||
|
||
class TodoItem(models.Model): ... |
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,14 @@ | ||
from pyscript import document, window | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def root(): | ||
def on_click(event): | ||
my_element = document.querySelector("#example") | ||
my_element.innerText = window.location.hostname | ||
|
||
return html.div( | ||
{"id": "example"}, | ||
html.button({"onClick": on_click}, "Click Me!"), | ||
) |
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
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
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
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
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,64 @@ | ||
import { DjangoFormProps } from "./types"; | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
/** | ||
* Interface used to bind a ReactPy node to React. | ||
*/ | ||
export function bind(node) { | ||
return { | ||
create: (type, props, children) => | ||
React.createElement(type, props, ...children), | ||
render: (element) => { | ||
ReactDOM.render(element, node); | ||
}, | ||
unmount: () => ReactDOM.unmountComponentAtNode(node), | ||
}; | ||
} | ||
|
||
export function DjangoForm({ | ||
onSubmitCallback, | ||
formId, | ||
}: DjangoFormProps): null { | ||
React.useEffect(() => { | ||
const form = document.getElementById(formId) as HTMLFormElement; | ||
|
||
// Submission event function | ||
const onSubmitEvent = (event) => { | ||
event.preventDefault(); | ||
const formData = new FormData(form); | ||
|
||
// Convert the FormData object to a plain object by iterating through it | ||
// If duplicate keys are present, convert the value into an array of values | ||
const entries = formData.entries(); | ||
const formDataArray = Array.from(entries); | ||
const formDataObject = formDataArray.reduce((acc, [key, value]) => { | ||
if (acc[key]) { | ||
if (Array.isArray(acc[key])) { | ||
acc[key].push(value); | ||
} else { | ||
acc[key] = [acc[key], value]; | ||
} | ||
} else { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
onSubmitCallback(formDataObject); | ||
}; | ||
|
||
// Bind the event listener | ||
if (form) { | ||
form.addEventListener("submit", onSubmitEvent); | ||
} | ||
|
||
// Unbind the event listener when the component dismounts | ||
return () => { | ||
if (form) { | ||
form.removeEventListener("submit", onSubmitEvent); | ||
} | ||
}; | ||
}, []); | ||
|
||
return null; | ||
} |
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,2 @@ | ||
export { DjangoForm, bind } from "./components"; | ||
export { mountComponent } from "./mount"; |
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
Oops, something went wrong.