diff --git a/src/context-selection/org-unit-selector-bar-item/debounced-search-input.js b/src/context-selection/org-unit-selector-bar-item/debounced-search-input.js index e40e266c3..974d92e5a 100644 --- a/src/context-selection/org-unit-selector-bar-item/debounced-search-input.js +++ b/src/context-selection/org-unit-selector-bar-item/debounced-search-input.js @@ -2,11 +2,11 @@ import i18n from '@dhis2/d2-i18n' import { InputField } from '@dhis2/ui' import PropTypes from 'prop-types' import React, { useEffect, useState } from 'react' -import useDebounce from './use-debounce.js' +import { useDebouncedValue } from '../../shared/index.js' export default function DebouncedSearchInput({ onChange, initialValue }) { const [value, setValue] = useState(initialValue) - const debouncedValue = useDebounce(value, 200) + const debouncedValue = useDebouncedValue(value, 200) useEffect(() => { onChange(debouncedValue) diff --git a/src/context-selection/org-unit-selector-bar-item/use-debounce.js b/src/context-selection/org-unit-selector-bar-item/use-debounce.js deleted file mode 100644 index 429e18de8..000000000 --- a/src/context-selection/org-unit-selector-bar-item/use-debounce.js +++ /dev/null @@ -1,13 +0,0 @@ -import { useEffect, useState } from 'react' - -/** Copied from https://usehooks.com/useDebounce/ */ -export default function useDebounce(value, delay) { - const [debouncedValue, setDebouncedValue] = useState(value) - - useEffect(() => { - const handler = setTimeout(() => setDebouncedValue(value), delay) - return () => clearTimeout(handler) - }, [value, delay]) - - return debouncedValue -} diff --git a/src/data-workspace/entry-form.js b/src/data-workspace/entry-form.js index 76113bdd1..e6dac530e 100644 --- a/src/data-workspace/entry-form.js +++ b/src/data-workspace/entry-form.js @@ -1,5 +1,6 @@ import PropTypes from 'prop-types' import React from 'react' +import { useDebounce } from '../shared/index.js' import { FORM_TYPES } from './constants.js' import { CustomForm } from './custom-form/index.js' import { DefaultForm } from './default-form.js' @@ -17,6 +18,7 @@ export const EntryForm = ({ dataSet }) => { const [globalFilterText, setGlobalFilterText] = React.useState('') const formType = dataSet.formType const Component = formTypeToComponent[formType] + const debouncedSetGlobalFilterText = useDebounce(setGlobalFilterText, 200) useCloseRightHandPanelOnSelectionChange() @@ -24,8 +26,7 @@ export const EntryForm = ({ dataSet }) => { <> {formType !== FORM_TYPES.CUSTOM && ( )} diff --git a/src/data-workspace/filter-field.js b/src/data-workspace/filter-field.js index f7034ef53..06bf1493e 100644 --- a/src/data-workspace/filter-field.js +++ b/src/data-workspace/filter-field.js @@ -2,12 +2,19 @@ import i18n from '@dhis2/d2-i18n' import { Button, InputField } from '@dhis2/ui' import classNames from 'classnames' import PropTypes from 'prop-types' -import React from 'react' +import React, { useState } from 'react' import { FORM_TYPES } from './constants.js' import styles from './entry-form.module.css' -export default function FilterField({ value, setFilterText, formType }) { +export default function FilterField({ onFilterChange, formType }) { + const [filterText, setFilterText] = useState('') const wrapperClasses = classNames(styles.filterWrapper, 'hide-for-print') + + const onChange = ({ value }) => { + setFilterText(value) + onFilterChange(value) + } + return (
setFilterText(value)} + value={filterText} + onChange={onChange} />