Skip to content

Commit

Permalink
fix(lld): use a debounce on memotag field
Browse files Browse the repository at this point in the history
  • Loading branch information
KVNLS committed Dec 5, 2024
1 parent 1447c15 commit d653fab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-ravens-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ledger-live-desktop": minor
---

LLD: LIVE-15143 use a debounce on the memotag field
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { render, screen, fireEvent } from "tests/testUtils";
import MemoTagField from "../components/MemoTagField";

describe("MemoTagField", () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

it("renders MemoTagField with label and text field", () => {
render(<MemoTagField showLabel={true} />);
expect(screen.getByText(/Tag \/ Memo/gi)).toBeInTheDocument();
Expand All @@ -25,6 +33,8 @@ describe("MemoTagField", () => {
fireEvent.change(screen.getByPlaceholderText(/Enter Tag \/ Memo/gi), {
target: { value: "new memo" },
});
expect(handleChange).not.toHaveBeenCalled();
jest.runAllTimers();
expect(handleChange).toHaveBeenCalledTimes(1);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import React, { useState, useEffect } from "react";
import Input, { Props as InputBaseProps } from "~/renderer/components/Input";
import { useDebounce } from "@ledgerhq/live-common//hooks/useDebounce";
import Label from "~/renderer/components/Label";
import Box from "~/renderer/components/Box";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -46,6 +47,13 @@ const MemoTagField = ({
tooltipText,
}: MemoTagFieldProps) => {
const { t } = useTranslation();
const [memoValue, setMemoValue] = useState(value);
const debouncedMemoValue = useDebounce(memoValue, 300);

useEffect(() => {
if (debouncedMemoValue !== value) onChange?.(debouncedMemoValue || "");
}, [debouncedMemoValue, onChange, value]);

return (
<Box flow={1}>
{showLabel && (
Expand All @@ -68,10 +76,10 @@ const MemoTagField = ({
<Flex justifyContent="end">{CaracterCountComponent && <CaracterCountComponent />}</Flex>
<Input
placeholder={placeholder ?? t("MemoTagField.placeholder")}
onChange={onChange}
onChange={setMemoValue}
warning={warning}
error={error}
value={value}
value={memoValue}
spellCheck="false"
ff="Inter"
maxMemoLength={maxMemoLength}
Expand Down

0 comments on commit d653fab

Please sign in to comment.