Skip to content

Commit

Permalink
Debounce validations to prevent input lag
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoha committed Jan 9, 2025
1 parent 39162c7 commit dc5e501
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@phosphor-icons/react": "^2.1.7",
"@types/lodash": "^4.17.14",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
Expand All @@ -31,6 +32,7 @@
"formik-antd": "^3.0.0-beta.9",
"highcharts": "^11.4.8",
"highcharts-react-official": "^3.2.1",
"lodash": "^4.17.21",
"package-lock.json": "^1.0.0",
"postcss": "^8.4.47",
"react": "^18.3.1",
Expand Down
11 changes: 10 additions & 1 deletion src/components/filterform/FiltersForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
isValidFilter,
validateFilterField,
} from './filterHelpers'
import { debounce } from 'lodash'
import { useEffect } from 'react'

type Props = {
filterConfigs: FilterConfig[]
Expand Down Expand Up @@ -56,6 +58,7 @@ export const FiltersForm = ({
},
])
}}
validateOnChange={false}
validate={(values) => {
if (values.filters.every((filter) => isEmptyFilter(filter))) {
return
Expand Down Expand Up @@ -94,7 +97,13 @@ type FilterFormFieldsProps = {
}

const FilterFormFields = ({ filterConfigs }: FilterFormFieldsProps) => {
const { values, resetForm } = useFormikContext<FilterForm>()
const { values, resetForm, validateForm } = useFormikContext<FilterForm>()

const debouncedValidateForm = debounce(validateForm, 1000)

useEffect(() => {
debouncedValidateForm()
}, [values])

return (
<div>
Expand Down

0 comments on commit dc5e501

Please sign in to comment.