Skip to content

Commit

Permalink
fix: infinite loop when injected with module federation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWielga committed Aug 11, 2024
1 parent 1673d92 commit b8fd128
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
26 changes: 13 additions & 13 deletions src/components/codeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ const CodeBox = styled("div")(({ theme: t }) => ({
},
}));

export const CodeEditor = forwardRef<
HTMLDivElement,
{
value: string;
onChange: (code: string) => void;
rows: number | string;
disabled?: boolean;
autoFocus?: boolean;
formatter?: (code: string) => string;
}
>(function CodeEditor(props, forwardedRef) {
export type CodeEditorProps = {
className?: string;
value: string;
onChange: (code: string) => void;
rows: number | string;
disabled?: boolean;
autoFocus?: boolean;
formatter?: (code: string) => string;
};

export const CodeEditor = forwardRef<HTMLDivElement, CodeEditorProps>(function CodeEditor(props, forwardedRef) {
const { value, onChange, rows, disabled, autoFocus, formatter, ...passProps } = props;

const editorRef = useRef<HTMLDivElement>(null);
const ref = useForkRef(forwardedRef, editorRef);

const jarRef = useRef<CodeJar>(null);
const positionRef = useRef<Position>(null);
const [code, setCode] = useState<string>(null);
const [code, setCode] = useState<string>(value);

//init CodeJar
useEffect(() => {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const CodeEditor = forwardRef<
sx={{
maxHeight: rows && `${parseInt(rows.toString()) * 1.45}em`,
}}
onBlurCapture={formatter && (() => jarRef.current?.updateCode(formatter(value)))}
onBlurCapture={formatter && (() => jarRef.current?.updateCode(formatter(code)))}
>
{value}
</CodeBox>
Expand Down
14 changes: 9 additions & 5 deletions src/components/jsonTextField.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useField } from "formik";
import { TextField } from "formik-mui";
import { TextFieldProps } from "formik-mui/dist/TextField";
import React, { useMemo } from "react";
import { CodeEditor } from "./codeEditor";
import { CodeEditor, CodeEditorProps } from "./codeEditor";

function formatJson(value: string): string {
try {
Expand All @@ -11,18 +12,21 @@ function formatJson(value: string): string {
}
}

const FieldCodeEditor = ({ name, ...props }: CodeEditorProps & { name: string }) => {
const [{ value, onChange }] = useField(name);
return <CodeEditor {...props} value={value} onChange={onChange} />;
};

export function JsonTextField(props: TextFieldProps) {
const {
field: { name },
form: { setFieldValue },
} = props;

const inputProps = useMemo(
() => ({
inputComponent: CodeEditor as any,
onChange: (code) => setFieldValue(name, code),
inputComponent: (props) => <FieldCodeEditor name={name} {...props} />,
}),
[name, setFieldValue],
[name],
);

const inputLabelProps = useMemo(() => ({ shrink: true }), []);
Expand Down

0 comments on commit b8fd128

Please sign in to comment.