Skip to content

Commit 592ad2c

Browse files
committed
Enhance JavaScript API with global GUI object and improved event handling\n\n- Add window.gui object with session state and navigation utilities\n- Improve event handler loading and script execution\n- Update button component properties\n- Add form submission and navigation capabilities
1 parent 7c89d6b commit 592ad2c

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

app/app.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
useActionData,
1818
useFetcher,
1919
useLoaderData,
20+
useNavigate,
2021
useSearchParams,
2122
useSubmit,
2223
} from "@remix-run/react";
@@ -193,11 +194,27 @@ function App() {
193194
const [searchParams] = useSearchParams();
194195
const loaderData = useLoaderData<typeof loader>();
195196
const actionData = useActionData<typeof action>();
196-
const submit = useSubmit();
197197
const { base64Body, children, state, channels } = actionData ?? loaderData;
198198
const formRef = useRef<HTMLFormElement>(null);
199199
const realtimeEvent = useRealtimeChannels({ channels });
200200
const fetcher = useFetcher();
201+
const submit = useSubmit();
202+
const navigate = useNavigate();
203+
204+
if (typeof window !== "undefined") {
205+
// @ts-ignore
206+
window.gui = {
207+
session_state: state,
208+
navigate,
209+
fetcher,
210+
submit() {
211+
if (formRef.current) submit(formRef.current, ...arguments);
212+
},
213+
rerun() {
214+
if (formRef.current) submit(formRef.current);
215+
},
216+
};
217+
}
201218

202219
useEffect(() => {
203220
if (!base64Body) return;

app/gooeyInput.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect, useRef, useState } from "react";
22
import { RenderedMarkdown } from "~/renderedMarkdown";
33
import { TooltipPlacement } from "./components/GooeyTooltip";
4+
import { useSubmit } from "@remix-run/react";
45

56
export function InputLabel({
67
label,
@@ -242,17 +243,15 @@ export function useGooeyCheckedInput({
242243
}
243244

244245
export function loadEventHandlers<T>(
245-
value: T,
246-
setValue: (value: T) => void,
246+
value?: T,
247+
setValue?: (value: T) => void,
247248
args?: Record<string, any>
248249
) {
249250
if (!args) return;
250251
for (const [attr, body] of Object.entries(args)) {
251-
if (!attr.startsWith("on")) continue;
252+
if (!attr.startsWith("on") || typeof body !== "string") continue;
252253
args[attr] = (event: React.SyntheticEvent) => {
253-
// new Function(...args, body)
254254
const fn = new Function("event", "value", "setValue", body);
255-
// fn.call(thisArg, ...args)
256255
return fn.call(event.currentTarget, event.nativeEvent, value, setValue);
257256
};
258257
}

app/renderer.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
GooeyRadio,
1313
GooeyTextarea,
1414
InputLabel,
15+
loadEventHandlers,
1516
} from "~/gooeyInput";
1617
import { useJsonFormInput } from "~/jsonFormInput";
1718
import { RenderedHTML } from "~/renderedHTML";
@@ -133,6 +134,7 @@ function RenderedTreeNode({
133134
state: Record<string, any>;
134135
}) {
135136
const { name, props, children } = node;
137+
loadEventHandlers(undefined, undefined, props);
136138

137139
switch (name) {
138140
case "":
@@ -469,7 +471,7 @@ function RenderedTreeNode({
469471
}
470472
case "script": {
471473
const { src, args } = props;
472-
return <ExecJs src={src} args={args}></ExecJs>;
474+
return <ExecJs state={state} src={src} args={args}></ExecJs>;
473475
}
474476
case "plotly-chart": {
475477
const { chart } = props;
@@ -512,20 +514,20 @@ function RenderedTreeNode({
512514
}
513515
}
514516

515-
function ExecJs({ src, args }: { args: any; src: any }) {
516-
const submit = useSubmit();
517-
518-
args.gooeyRefresh = () => {
519-
const elem = document.getElementById("gooey-form");
520-
if (elem) submit(elem as HTMLFormElement, ...arguments);
521-
};
522-
517+
function ExecJs({
518+
src,
519+
args,
520+
state,
521+
}: {
522+
src: string;
523+
args: Record<string, any>;
524+
state: Record<string, any>;
525+
}) {
523526
useEffect(() => {
524527
// eslint-disable-next-line no-new-func
525528
const fn = new Function(...Object.keys(args), src);
526529
fn(...Object.values(args));
527530
}, [src, args]);
528-
529531
return null;
530532
}
531533

py/gooey_gui/components/common.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,6 @@ def button(
581581
value="yes",
582582
name=key,
583583
label=dedent(label),
584-
help=help,
585-
tooltipPlacement=tooltip_placement,
586584
disabled=disabled,
587585
className=className,
588586
**props,

0 commit comments

Comments
 (0)