Skip to content

Commit

Permalink
fixes inline value label not updating (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabiGrin authored Aug 11, 2024
1 parent e73774f commit 071114b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion stdlib/src/Values/CodeExpression.flyde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const CodeExpression: MacroNode<CodeExpressionConfig> = {
},
defaultData: {
value: "inputs.a + inputs.b",
label: '"inputs.a + inputs.b"',
label: "inputs.a + inputs.b",
},
editorConfig: {
type: "custom",
Expand Down
30 changes: 16 additions & 14 deletions stdlib/src/Values/CodeExpression.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { FormGroup, InputGroup, TextArea } from "@blueprintjs/core";
import type { InlineValueConfig } from "./InlineValue.flyde";
import React, { useCallback } from "react";
import React, { useCallback, useState } from "react";
import { getVariables } from "./getInlineVariables";
import { MacroEditorComp } from "@flyde/core";

const labelMaxLength = 20;
const labelMaxLength = 50;

function valToLabel(val: any): string {
try {
const label = JSON.stringify(val);
if (label.length > labelMaxLength) {
return `${label.slice(0, labelMaxLength)}...`;
if (val.length > labelMaxLength) {
return `${val.slice(0, labelMaxLength)}...`;
}
return label;
return val;
} catch (e) {
return `Value`;
}
Expand All @@ -21,21 +20,24 @@ function valToLabel(val: any): string {
const CodeExpressionEditor: MacroEditorComp<InlineValueConfig> =
function CodeExpressionEditor(props) {
const { value, onChange } = props;
const [isLabelCustom, setIsLabelCustom] = useState(false);

const changeValue = useCallback(
(_val) => {
const newLabel = valToLabel(_val);
const oldLabel = valToLabel(value.value);

const wasUsingDefaultLabel = value.label === oldLabel || !value.label;

const labelToUse = wasUsingDefaultLabel ? newLabel : value.label;

const labelToUse = isLabelCustom ? value.label : valToLabel(_val);
onChange({ ...value, value: _val, label: labelToUse });
},
[value, onChange]
);

const changeLabel = useCallback(
(newLabel) => {
setIsLabelCustom(newLabel ? true : false);
onChange({ ...value, label: newLabel });
},
[value, onChange]
);

const vars = getVariables(value.value ?? "");

return (
Expand Down Expand Up @@ -67,7 +69,7 @@ const CodeExpressionEditor: MacroEditorComp<InlineValueConfig> =
<InputGroup
type="text"
value={value.label}
onChange={(e) => onChange({ ...value, label: e.target.value })}
onChange={(e) => changeLabel(e.target.value)}
placeholder="Code Expression"
/>
</FormGroup>
Expand Down
22 changes: 13 additions & 9 deletions stdlib/src/Values/InlineValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TextArea,
} from "@blueprintjs/core";
import type { InlineValueConfig } from "./InlineValue.flyde";
import React, { useCallback, useMemo } from "react";
import React, { useCallback, useMemo, useState } from "react";
import { MacroEditorComp } from "@flyde/core";

const types: InlineValueConfig["type"][] = [
Expand All @@ -25,7 +25,7 @@ const defaultValuePerType = {
expression: (currValue: any) => currValue,
};

const labelMaxLength = 20;
const labelMaxLength = 50;

function valToLabel(val: any): string {
try {
Expand All @@ -42,6 +42,7 @@ function valToLabel(val: any): string {
const InlineValueEditor: MacroEditorComp<InlineValueConfig> =
function InlineValueEditor(props) {
const { value, onChange } = props;
const [isLabelCustom, setIsLabelCustom] = useState(false);

const changeType = useCallback(
(type) => {
Expand All @@ -60,18 +61,21 @@ const InlineValueEditor: MacroEditorComp<InlineValueConfig> =

const changeValue = useCallback(
(_val) => {
const newLabel = valToLabel(_val);
const oldLabel = valToLabel(value.value);

const wasUsingDefaultLabel = value.label === oldLabel || !value.label;

const labelToUse = wasUsingDefaultLabel ? newLabel : value.label;
const labelToUse = isLabelCustom ? value.label : valToLabel(_val);

onChange({ ...value, value: _val, label: labelToUse });
},
[value, onChange]
);

const changeLabel = useCallback(
(newLabel) => {
setIsLabelCustom(newLabel ? true : false);
onChange({ ...value, label: newLabel });
},
[value, onChange]
);

const editorPanel = useMemo(() => {
switch (value.type) {
case "string":
Expand Down Expand Up @@ -138,7 +142,7 @@ const InlineValueEditor: MacroEditorComp<InlineValueConfig> =
<InputGroup
type="text"
value={value.label}
onChange={(e) => onChange({ ...value, label: e.target.value })}
onChange={(e) => changeLabel(e.target.value)}
/>
</FormGroup>
</div>
Expand Down

0 comments on commit 071114b

Please sign in to comment.