Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes inline value label not updating #136

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading