Skip to content

Commit

Permalink
Only update action names and project on input blur
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed Nov 13, 2024
1 parent 1ee7e4b commit be764d4
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/components/ActionNameCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Input,
useToast,
} from "@chakra-ui/react";
import { useCallback } from "react";
import { useCallback, useState } from "react";
import { useIntl } from "react-intl";
import { useStore } from "../store";
import { tourElClassname } from "../tours";
Expand Down Expand Up @@ -40,7 +40,8 @@ const ActionNameCard = ({
const toastId = "name-too-long-toast";
const setActionName = useStore((s) => s.setActionName);
const setActionIcon = useStore((s) => s.setActionIcon);
const { name, icon, ID: id } = value;
const { icon, ID: id } = value;
const [localName, setLocalName] = useState<string>(value.name);
const predictionResult = useStore((s) => s.predictionResult);
const isTriggered = readOnly
? predictionResult?.detected?.ID === value.ID
Expand All @@ -64,11 +65,15 @@ const ActionNameCard = ({
});
return;
}
setActionName(id, name);
setLocalName(name);
},
[id, intl, setActionName, toast]
[intl, toast]
);

const onBlur = useCallback(() => {
setActionName(id, localName);
}, [id, localName, setActionName]);

const handleIconSelected = useCallback(
(icon: MakeCodeIcon) => {
setActionIcon(id, icon);
Expand Down Expand Up @@ -98,7 +103,7 @@ const ActionNameCard = ({
borderRadius="sm"
aria-label={intl.formatMessage(
{ id: "delete-action-aria" },
{ action: name }
{ action: localName }
)}
/>
)}
Expand All @@ -113,10 +118,10 @@ const ActionNameCard = ({
{!readOnly && <LedIconPicker onIconSelected={handleIconSelected} />}
</HStack>
<Input
autoFocus={name.length === 0}
autoFocus={localName.length === 0}
isTruncated
readOnly={readOnly}
value={name}
value={localName}
borderWidth={0}
maxLength={18}
{...(readOnly
Expand All @@ -126,6 +131,7 @@ const ActionNameCard = ({
placeholder={intl.formatMessage({
id: "action-name-placeholder",
})}
onBlur={onBlur}
onChange={onChange}
/>
</HStack>
Expand Down

0 comments on commit be764d4

Please sign in to comment.