Skip to content

Commit f6719b8

Browse files
committed
load inline js event handlers for inputs
1 parent 4273f0c commit f6719b8

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

app/components/CodeEditor.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,13 @@ export default function CodeEditor({
4141
onChange: OnChange;
4242
state: Record<string, any>;
4343
}) {
44-
const {
45-
name,
46-
defaultValue,
47-
height,
48-
label,
49-
help,
50-
tooltipPlacement,
51-
...restProps
52-
} = props;
44+
const { name, defaultValue, height, label, help, tooltipPlacement, ...args } =
45+
props;
5346
const [inputRef, value, setValue] = useGooeyStringInput<HTMLTextAreaElement>({
5447
state,
5548
name,
5649
defaultValue,
50+
args,
5751
});
5852
const handleChange = (val: string) => {
5953
setValue(val);
@@ -98,7 +92,7 @@ export default function CodeEditor({
9892
value={value}
9993
onChange={handleChange}
10094
extensions={[javascript(), lintGutter(), jsLinter(lintOptions)]}
101-
{...restProps}
95+
{...args}
10296
/>
10397
</div>
10498
);

app/components/GooeySelect.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function GooeySelect({
2525
defaultValue,
2626
name,
2727
onChange,
28+
args,
2829
});
2930

3031
// if the state value is changed by the server code, then update the value

app/gooeyInput.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function GooeyTextarea({
3737
state,
3838
name,
3939
defaultValue,
40+
args,
4041
});
4142
return (
4243
<div className="gui-input gui-input-textarea">
@@ -74,6 +75,7 @@ export function GooeyInput({
7475
state,
7576
name,
7677
defaultValue,
78+
args,
7779
});
7880
return (
7981
<div className={className}>
@@ -100,10 +102,12 @@ export function useGooeyStringInput<
100102
state,
101103
name,
102104
defaultValue,
105+
args,
103106
}: {
104107
state: Record<string, any>;
105108
name: string;
106109
defaultValue: string;
110+
args?: Record<string, any>;
107111
}): [
108112
inputRef: React.RefObject<E>,
109113
value: string,
@@ -117,6 +121,8 @@ export function useGooeyStringInput<
117121
// but to avoid reloading the page on every change with onChange (gets very annoying when typing), we need to use this extra state variable with a useEffect
118122
const [value, setValue] = useState<string>(state[name] || defaultValue);
119123

124+
loadEventHandlers(value, setValue, args);
125+
120126
useEffect(() => {
121127
const element = inputRef.current;
122128
if (
@@ -234,3 +240,20 @@ export function useGooeyCheckedInput({
234240

235241
return inputRef;
236242
}
243+
244+
export function loadEventHandlers<T>(
245+
value: T,
246+
setValue: (value: T) => void,
247+
args?: Record<string, any>
248+
) {
249+
if (!args) return;
250+
for (const [attr, body] of Object.entries(args)) {
251+
if (!attr.startsWith("on")) continue;
252+
args[attr] = (event: React.SyntheticEvent) => {
253+
// new Function(...args, body)
254+
const fn = new Function("event", "value", "setValue", body);
255+
// fn.call(thisArg, ...args)
256+
return fn.call(event.currentTarget, event.nativeEvent, value, setValue);
257+
};
258+
}
259+
}

app/jsonFormInput.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import React, { useEffect, useRef, useState } from "react";
2+
import { loadEventHandlers } from "./gooeyInput";
23

34
export function useJsonFormInput<T>({
45
defaultValue,
56
name,
67
onChange,
78
encode = JSON.stringify,
89
decode = JSON.parse,
10+
args,
911
}: {
1012
defaultValue: T;
1113
name: string;
1214
onChange?: () => void;
1315
encode?: (value: T) => string;
1416
decode?: (value: string) => T;
17+
args?: Record<string, any>;
1518
}): [React.FunctionComponent, T, (value: T) => void] {
1619
const [value, setValue] = useState(defaultValue);
1720
const ref = useRef<HTMLInputElement>(null);
1821

22+
loadEventHandlers(value, setValue, args);
23+
1924
useEffect(() => {
2025
if (!ref.current) return;
2126
if (!ref.current.value) {

app/renderer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ export function GuiExpander({
621621
defaultValue: open,
622622
name,
623623
onChange,
624+
args: props,
624625
});
625626

626627
return (

0 commit comments

Comments
 (0)