Skip to content

Commit

Permalink
Support debouncing value when using 'defaultValue' (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoussefHenna authored Aug 22, 2024
1 parent 60a4073 commit 67382c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/components/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const NumberInput = React.forwardRef<NativeTextInput, Props>(
const [currentStringNumberValue, setCurrentStringNumberValue] =
useState("");

const delayedValue = useDebounce(value, changeTextDelay);
const [valueToDebounce, setValueToDebounce] = React.useState(value);
const delayedValue = useDebounce(valueToDebounce, changeTextDelay);

const formatValueToStringNumber = (valueToFormat?: number | string) => {
if (valueToFormat != null) {
Expand Down Expand Up @@ -92,7 +93,10 @@ const NumberInput = React.forwardRef<NativeTextInput, Props>(
ref={ref}
keyboardType="numeric"
value={currentStringNumberValue}
onChangeText={handleChangeText}
onChangeText={(newValue) => {
handleChangeText(newValue);
setValueToDebounce(newValue);
}}
{...props}
/>
);
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/components/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const TextInput = React.forwardRef<NativeTextInput, TextInputProps>(
disabled = false,
editable = true,
value,
onChangeText,
...rest
},
ref
) => {
const theme = useTheme();
const delayedValue = useDebounce(value, changeTextDelay);
const [valueToDebounce, setValueToDebounce] = React.useState(value);
const delayedValue = useDebounce(valueToDebounce, changeTextDelay);

useOnUpdate(() => {
if (delayedValue !== undefined) {
Expand All @@ -49,6 +51,10 @@ const TextInput = React.forwardRef<NativeTextInput, TextInputProps>(
{ color: theme.colors.text.strong },
style,
]}
onChangeText={(newValue) => {
onChangeText?.(newValue);
setValueToDebounce(newValue);
}}
{...rest}
/>
);
Expand Down

0 comments on commit 67382c1

Please sign in to comment.