Skip to content

Commit

Permalink
feat: try calling onchange in the filterinput component
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshigo345 committed Jan 2, 2025
1 parent d7a07bd commit 0619976
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/(private)/roles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const Page = (): JSX.Element => {
id="name"
label={t('name')}
value={filters.name}
onChange={(e) => handleFilterChange('name', e.target.value)}
onChange={(value) => handleFilterChange('name', value)}
/>
</div>
<DataTable
Expand Down
18 changes: 15 additions & 3 deletions src/components/ui/filter-input.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { cn } from '@/lib/utils'
import { debounce } from 'lodash'
import { useMemo, useState } from 'react'

interface FilterInputProps {
id: string
label: string
value: string | number | undefined
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void
onChange: (value: string) => void
placeholder?: string
type?: string
}
Expand All @@ -19,16 +21,26 @@ const FilterInput: React.FC<FilterInputProps> = ({
placeholder = '',
type = 'text',
}) => {
const [localValue, setLocalValue] = useState(value ?? '')

// Debounce the onChange callback to minimize frequent calls
const debouncedChange = useMemo(
() => debounce((newValue: string) => onChange(newValue), 300),
[onChange]
)

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
onChange(e)
const newValue = e.target.value
setLocalValue(newValue) // Update the local state immediately for user feedback
debouncedChange(newValue) // Call the debounced onChange function
}

return (
<div className="relative">
<Input
id={id}
placeholder={placeholder}
value={value ?? ''}
value={localValue}
onChange={handleChange}
type={type}
className={cn(
Expand Down

0 comments on commit 0619976

Please sign in to comment.