Skip to content

Commit

Permalink
fix(list): fix filter being cleared on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Mar 19, 2024
1 parent 3923fef commit a19cfe9
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import i18n from '@dhis2/d2-i18n'
import { Input, InputEventPayload } from '@dhis2/ui'
import React, { useEffect, useState } from 'react'
import {
useDebounce,
IDENTIFIABLE_FILTER_KEY,
useSectionListFilter,
} from '../../../../lib'
import { useDebouncedCallback } from 'use-debounce'
import { IDENTIFIABLE_FILTER_KEY, useSectionListFilter } from '../../../../lib'
import css from './Filters.module.css'

export const IdentifiableFilter = () => {
const [filter, setFilter] = useSectionListFilter(IDENTIFIABLE_FILTER_KEY)
const [value, setValue] = useState(filter || '')
const debouncedValue = useDebounce<typeof filter>(value, 200)

useEffect(() => {
setFilter(debouncedValue || undefined) // convert empty string to undefined
}, [debouncedValue, setFilter])
const debouncedSetFilter = useDebouncedCallback(
(debouncedFilter: string) =>
// convert empty string to undefined
// to prevent empty-value like "identifiable=" in URL
setFilter(debouncedFilter),
200
)

const handleSetValue = (event: InputEventPayload) => {
const eventValue = event.value ?? ''
setValue(eventValue)
debouncedSetFilter(eventValue)
}

useEffect(() => {
// clear input-value when "Clear all filters"
if (!filter) {
setValue('')
}
Expand All @@ -28,9 +35,7 @@ export const IdentifiableFilter = () => {
<Input
className={css.identifiableSelectionFilter}
placeholder={i18n.t('Search by name, code or ID')}
onChange={(value: InputEventPayload) =>
setValue(value.value ?? '')
}
onChange={handleSetValue}
value={value}
dataTest="input-search-name"
dense
Expand Down

0 comments on commit a19cfe9

Please sign in to comment.