2
0

fix(editor): 🐛 Debounce value on popover close

This commit is contained in:
Baptiste Arnaud
2022-03-25 11:28:49 +01:00
parent de784820eb
commit f3c5f6bea2
2 changed files with 15 additions and 20 deletions

View File

@@ -10,7 +10,7 @@ import {
InputProps, InputProps,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { useState, useRef, useEffect, ChangeEvent } from 'react' import { useState, useRef, useEffect, ChangeEvent } from 'react'
import { useDebounce } from 'use-debounce' import { useDebouncedCallback } from 'use-debounce'
type Props = { type Props = {
selectedItem?: string selectedItem?: string
@@ -28,8 +28,9 @@ export const SearchableDropdown = ({
}: Props) => { }: Props) => {
const { onOpen, onClose, isOpen } = useDisclosure() const { onOpen, onClose, isOpen } = useDisclosure()
const [inputValue, setInputValue] = useState(selectedItem ?? '') const [inputValue, setInputValue] = useState(selectedItem ?? '')
const [debouncedInputValue] = useDebounce( const debounced = useDebouncedCallback(
inputValue, // eslint-disable-next-line @typescript-eslint/no-empty-function
onValueChange ? onValueChange : () => {},
process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout
) )
const [filteredItems, setFilteredItems] = useState([ const [filteredItems, setFilteredItems] = useState([
@@ -60,16 +61,10 @@ export const SearchableDropdown = ({
handler: onClose, handler: onClose,
}) })
useEffect(() => {
onValueChange &&
debouncedInputValue !== selectedItem &&
onValueChange(debouncedInputValue)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedInputValue])
const onInputChange = (e: ChangeEvent<HTMLInputElement>) => { const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
if (!isOpen) onOpen() if (!isOpen) onOpen()
setInputValue(e.target.value) setInputValue(e.target.value)
debounced(e.target.value)
if (e.target.value === '') { if (e.target.value === '') {
setFilteredItems([...items.slice(0, 50)]) setFilteredItems([...items.slice(0, 50)])
return return
@@ -85,6 +80,7 @@ export const SearchableDropdown = ({
const handleItemClick = (item: string) => () => { const handleItemClick = (item: string) => () => {
setInputValue(item) setInputValue(item)
debounced(item)
onClose() onClose()
} }

View File

@@ -7,7 +7,7 @@ import {
NumberDecrementStepper, NumberDecrementStepper,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { useDebounce } from 'use-debounce' import { useDebouncedCallback } from 'use-debounce'
export const SmartNumberInput = ({ export const SmartNumberInput = ({
value, value,
@@ -20,26 +20,25 @@ export const SmartNumberInput = ({
onValueChange: (value?: number) => void onValueChange: (value?: number) => void
} & NumberInputProps) => { } & NumberInputProps) => {
const [currentValue, setCurrentValue] = useState(value?.toString() ?? '') const [currentValue, setCurrentValue] = useState(value?.toString() ?? '')
const [valueToReturn, setValueToReturn] = useState<number | undefined>( const debounced = useDebouncedCallback(
parseFloat(currentValue) onValueChange,
)
const [debouncedValue] = useDebounce(
valueToReturn,
process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout process.env.NEXT_PUBLIC_E2E_TEST ? 0 : debounceTimeout
) )
useEffect(() => { useEffect(() => {
onValueChange(debouncedValue) return () => {
debounced.flush()
}
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedValue]) }, [])
const handleValueChange = (value: string) => { const handleValueChange = (value: string) => {
setCurrentValue(value) setCurrentValue(value)
if (value.endsWith('.') || value.endsWith(',')) return if (value.endsWith('.') || value.endsWith(',')) return
if (value === '') return setValueToReturn(undefined) if (value === '') return debounced(undefined)
const newValue = parseFloat(value) const newValue = parseFloat(value)
if (isNaN(newValue)) return if (isNaN(newValue)) return
setValueToReturn(newValue) debounced(newValue)
} }
return ( return (