diff --git a/components/tablelist/TableList.tsx b/components/tablelist/TableList.tsx index 64a63ce..f11b790 100644 --- a/components/tablelist/TableList.tsx +++ b/components/tablelist/TableList.tsx @@ -10,11 +10,10 @@ import { SortingState, useReactTable, } from "@tanstack/react-table"; -import React, { useCallback, useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; import { Button, Label, Table } from "flowbite-react"; import { HiChevronLeft, HiChevronRight, HiPlus } from "react-icons/hi"; import { useLocal } from "@/lib/utils/use-local"; -import { debouncedHandler } from "@/lib/utils/debounceHandler"; import { FaSort, FaSortDown, FaSortUp } from "react-icons/fa6"; import Link from "next/link"; import { init_column } from "./lib/column"; @@ -196,7 +195,6 @@ export const TableList = ({ {"Loading..."} ); - console.log(local.fieldResultFilter); if (typeof onCount === "function") { const params = await events("onload-param", { take: 1, @@ -273,6 +271,8 @@ export const TableList = ({ sort: local.sort, take, paging: local.paging, + ...local.filter, + ...local.fieldResultFilter, }); if (!autoPagination) { res = paginateArray(res, take, local.paging); @@ -360,6 +360,7 @@ export const TableList = ({ paging: 1, search: local.search, ...local.filter, + ...local.fieldResultFilter, }); const res = await onCount(params); local.count = res; @@ -383,6 +384,8 @@ export const TableList = ({ sort: local.sort, take, paging: 1, + ...local.filter, + ...local.fieldResultFilter, }); if (!autoPagination) { res = paginateArray(res, take, 1); @@ -552,12 +555,6 @@ export const TableList = ({ onStateChange: setState, debugTable: state.pagination.pageIndex > 2, })); - const handleSearch = useCallback( - debouncedHandler(() => { - local.refresh(); - }, 1000), - [] - ); return ( <>
@@ -586,7 +583,7 @@ export const TableList = ({
-
{ e.preventDefault(); await local.reload(); @@ -597,19 +594,19 @@ export const TableList = ({
{ const value = e.target.value; local.search = value; local.render(); - handleSearch(); + local.refresh(); }} />
- +
{mode === "table" && filter && local?.fieldFilter?.length ? (
diff --git a/components/ui/input-search.tsx b/components/ui/input-search.tsx index 218ab4f..5798491 100644 --- a/components/ui/input-search.tsx +++ b/components/ui/input-search.tsx @@ -1,39 +1,48 @@ import * as React from "react"; - import { cn } from "@/lib/utils"; import { HiSearch } from "react-icons/hi"; +import debounce from "lodash.debounce"; -const InputSearch = React.forwardRef< - HTMLInputElement, - React.ComponentProps<"input"> ->(({ className, type, ...props }, ref) => { - return ( -
- - -
- ); -}); -InputSearch.displayName = "Input"; +interface InputSearchProps extends React.ComponentProps<"input"> { + delay?: number; +} + +const InputSearch = React.forwardRef( + ({ className, type, onChange, delay = 100, ...props }, ref) => { + const debouncedLoadOptions = React.useMemo( + () => + debounce((event: React.ChangeEvent) => { + if (onChange) onChange(event); + }, delay), + [delay, onChange] + ); + + return ( +
+ + { + if (event.key === "Enter" && onChange) { + event.stopPropagation(); + event.preventDefault(); // Mencegah submit form default jika ada + onChange(event as any); // Panggil `onChange` langsung saat Enter ditekan + } + }} + {...props} + /> +
+ ); + } +); + +InputSearch.displayName = "InputSearch"; export { InputSearch };