Skip to content

Commit

Permalink
fix(editor): 🐛 Debounce value on popover close
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 25, 2022
1 parent de78482 commit f3c5f6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
16 changes: 6 additions & 10 deletions apps/builder/components/shared/SearchableDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
InputProps,
} from '@chakra-ui/react'
import { useState, useRef, useEffect, ChangeEvent } from 'react'
import { useDebounce } from 'use-debounce'
import { useDebouncedCallback } from 'use-debounce'

type Props = {
selectedItem?: string
Expand All @@ -28,8 +28,9 @@ export const SearchableDropdown = ({
}: Props) => {
const { onOpen, onClose, isOpen } = useDisclosure()
const [inputValue, setInputValue] = useState(selectedItem ?? '')
const [debouncedInputValue] = useDebounce(
inputValue,
const debounced = useDebouncedCallback(
// eslint-disable-next-line @typescript-eslint/no-empty-function
onValueChange ? onValueChange : () => {},
process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout
)
const [filteredItems, setFilteredItems] = useState([
Expand Down Expand Up @@ -60,16 +61,10 @@ export const SearchableDropdown = ({
handler: onClose,
})

useEffect(() => {
onValueChange &&
debouncedInputValue !== selectedItem &&
onValueChange(debouncedInputValue)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedInputValue])

const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
if (!isOpen) onOpen()
setInputValue(e.target.value)
debounced(e.target.value)
if (e.target.value === '') {
setFilteredItems([...items.slice(0, 50)])
return
Expand All @@ -85,6 +80,7 @@ export const SearchableDropdown = ({

const handleItemClick = (item: string) => () => {
setInputValue(item)
debounced(item)
onClose()
}

Expand Down
19 changes: 9 additions & 10 deletions apps/builder/components/shared/SmartNumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NumberDecrementStepper,
} from '@chakra-ui/react'
import { useEffect, useState } from 'react'
import { useDebounce } from 'use-debounce'
import { useDebouncedCallback } from 'use-debounce'

export const SmartNumberInput = ({
value,
Expand All @@ -20,26 +20,25 @@ export const SmartNumberInput = ({
onValueChange: (value?: number) => void
} & NumberInputProps) => {
const [currentValue, setCurrentValue] = useState(value?.toString() ?? '')
const [valueToReturn, setValueToReturn] = useState<number | undefined>(
parseFloat(currentValue)
)
const [debouncedValue] = useDebounce(
valueToReturn,
const debounced = useDebouncedCallback(
onValueChange,
process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout
)

useEffect(() => {
onValueChange(debouncedValue)
return () => {
debounced.flush()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedValue])
}, [])

const handleValueChange = (value: string) => {
setCurrentValue(value)
if (value.endsWith('.') || value.endsWith(',')) return
if (value === '') return setValueToReturn(undefined)
if (value === '') return debounced(undefined)
const newValue = parseFloat(value)
if (isNaN(newValue)) return
setValueToReturn(newValue)
debounced(newValue)
}

return (
Expand Down

3 comments on commit f3c5f6b

@vercel
Copy link

@vercel vercel bot commented on f3c5f6b Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on f3c5f6b Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.