2025-08-06 10:53:17 +02:00
|
|
|
// * Packages
|
2025-10-29 10:13:54 +01:00
|
|
|
import { DeleteIcon } from "lucide-react";
|
2026-01-27 16:48:50 +01:00
|
|
|
import { FormEvent, ReactNode, useState } from "react";
|
2025-08-06 10:53:17 +02:00
|
|
|
import { route } from "ziggy-js";
|
|
|
|
|
// * Other
|
2025-09-16 16:44:49 +02:00
|
|
|
import SimpleString from "@/back/Components/Inputs/SimpleString";
|
2026-01-21 12:31:07 +01:00
|
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
|
|
|
import InputError from "@/back/Components/InputError";
|
2025-08-06 10:53:17 +02:00
|
|
|
import useTrans from "@/hooks/use-translations";
|
2026-02-10 15:52:48 +01:00
|
|
|
import addFilterToUrl from "@/lib/filters";
|
2025-08-06 10:53:17 +02:00
|
|
|
|
2026-01-20 15:25:48 +01:00
|
|
|
type DataTableSearchProps = {
|
|
|
|
|
modelRoute: string;
|
|
|
|
|
searchFields: string;
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-06 10:53:17 +02:00
|
|
|
export default function DataTableSearch({
|
2025-09-16 16:44:49 +02:00
|
|
|
modelRoute,
|
2025-08-06 10:53:17 +02:00
|
|
|
searchFields,
|
2026-01-20 15:25:48 +01:00
|
|
|
children,
|
|
|
|
|
}: DataTableSearchProps) {
|
2026-02-10 15:52:48 +01:00
|
|
|
const [search, setSearch] = useState<string | null>(
|
|
|
|
|
route().params.search ?? null,
|
|
|
|
|
);
|
2026-01-27 16:48:50 +01:00
|
|
|
|
2026-02-10 15:52:48 +01:00
|
|
|
const submit = (e: FormEvent<HTMLFormElement>) => {
|
|
|
|
|
if (!search) return;
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const params = {
|
|
|
|
|
...route().params,
|
|
|
|
|
search,
|
|
|
|
|
};
|
|
|
|
|
addFilterToUrl({ modelName: modelRoute, params });
|
|
|
|
|
};
|
|
|
|
|
const onDeleteSearch = () => {
|
|
|
|
|
setSearch(null);
|
|
|
|
|
const params = {
|
|
|
|
|
...route().params,
|
|
|
|
|
search: undefined,
|
2026-01-27 16:48:50 +01:00
|
|
|
};
|
2026-02-10 15:52:48 +01:00
|
|
|
addFilterToUrl({ modelName: modelRoute, params });
|
|
|
|
|
};
|
2025-08-06 10:53:17 +02:00
|
|
|
return (
|
2026-01-21 12:31:07 +01:00
|
|
|
<>
|
2026-02-10 15:52:48 +01:00
|
|
|
<form
|
|
|
|
|
onSubmit={submit}
|
|
|
|
|
className="flex justify-between items-end gap-2 mb-4"
|
|
|
|
|
>
|
2026-01-21 12:31:07 +01:00
|
|
|
<SimpleString
|
|
|
|
|
name="search"
|
|
|
|
|
label={useTrans("back.table_search", {
|
|
|
|
|
params: { searchFields },
|
|
|
|
|
})}
|
|
|
|
|
placeholder="ex: John Doe"
|
2026-01-27 16:48:50 +01:00
|
|
|
value={search ?? undefined}
|
2026-01-21 12:31:07 +01:00
|
|
|
wordCounter={false}
|
2026-01-27 16:48:50 +01:00
|
|
|
setData={(name, value) => {
|
|
|
|
|
setSearch(value);
|
|
|
|
|
}}
|
2026-01-21 12:31:07 +01:00
|
|
|
className="w-[calc(100%-40px-0.5rem)]"
|
|
|
|
|
/>
|
|
|
|
|
{children}
|
|
|
|
|
<BtnOptionnalLinkWithTooltip
|
|
|
|
|
content={<DeleteIcon />}
|
2026-02-10 15:52:48 +01:00
|
|
|
tooltipContent={
|
|
|
|
|
<p>{useTrans("back.table_search_reset")}</p>
|
|
|
|
|
}
|
2026-01-21 12:31:07 +01:00
|
|
|
type="button"
|
|
|
|
|
variant="destructive"
|
2026-01-27 16:48:50 +01:00
|
|
|
onClick={onDeleteSearch}
|
2026-01-21 12:31:07 +01:00
|
|
|
/>
|
|
|
|
|
</form>
|
|
|
|
|
<InputError all />
|
|
|
|
|
</>
|
2025-08-06 10:53:17 +02:00
|
|
|
);
|
|
|
|
|
}
|