Skip to content

Commit

Permalink
fix: debounce the syncing between value and localValue
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Aug 17, 2023
1 parent a68ab37 commit 3919ff5
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useCallback } from "react";
import { css } from "emotion";
import classNames from "classnames";
import omit from "lodash/omit";
import { Typography } from "@webiny/ui/Typography";
import { FormElementMessage } from "@webiny/ui/FormElementMessage";
import { COLORS } from "./StyledComponents";
import { Validation } from "@webiny/form";
import debounce from "lodash/debounce";

const inputStyle = css({
boxSizing: "border-box",
Expand Down Expand Up @@ -101,13 +102,21 @@ const InputField: React.FC<InputBoxProps> = ({
// the cursor to jump to the end of the input field, but we couldn't find a solution.
// This was mainly because of the async nature of the onChange callback. For example,
// if we removed the async validation in PropertySettings.tsx, the cursor would no longer jump.
const [localValue, setLocalValue] = useState(value);
const [localValue, setLocalValue] = useState<string | number | undefined>();

useEffect(() => {
if (localValue !== value) {
const debouncedSetLocalValue = useCallback(
debounce(value => {
setLocalValue(value);
}
}, [value]);
}, 100),
[]
);

// On all outside changes, we need to update the local value. Note the debounced
// `setLocalValue` call. This resolves the issue where the cursor would jump to
// the end of the input field while typing.
useEffect(() => {
debouncedSetLocalValue(value);
}, [localValue, value]);

return (
<React.Fragment>
Expand Down

0 comments on commit 3919ff5

Please sign in to comment.