2026-03-30 14:17:31 +02:00
|
|
|
import { DeleteIcon } from "lucide-react";
|
2026-10-19 17:43:33 +02:00
|
|
|
import { type ReactNode, type SubmitEvent, useState } from "react";
|
2026-10-16 12:07:08 +02:00
|
|
|
|
2026-03-30 14:17:31 +02:00
|
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
|
|
|
import InputError from "@/back/Components/InputError";
|
2026-10-16 12:07:08 +02:00
|
|
|
import SimpleString from "@/back/Components/Inputs/SimpleString";
|
|
|
|
|
import AddFilterToUrl from "@/back/lib/filters";
|
2026-10-19 17:43:33 +02:00
|
|
|
import trans from "@/hooks/use-translations";
|
2026-03-30 14:17:31 +02:00
|
|
|
|
2026-10-16 12:07:08 +02:00
|
|
|
interface DataTableSearchProps {
|
2026-03-30 14:17:31 +02:00
|
|
|
modelRoute: string;
|
2026-10-16 12:07:08 +02:00
|
|
|
prefixRoute: string;
|
2026-03-30 14:17:31 +02:00
|
|
|
searchFields: string;
|
|
|
|
|
isArchiveRoute?: boolean;
|
|
|
|
|
children?: ReactNode;
|
2026-10-16 12:07:08 +02:00
|
|
|
}
|
2026-03-30 14:17:31 +02:00
|
|
|
|
|
|
|
|
export default function DataTableSearch({
|
|
|
|
|
modelRoute,
|
2026-10-16 12:07:08 +02:00
|
|
|
prefixRoute,
|
2026-03-30 14:17:31 +02:00
|
|
|
searchFields,
|
|
|
|
|
isArchiveRoute = false,
|
|
|
|
|
children,
|
|
|
|
|
}: DataTableSearchProps) {
|
2026-10-16 12:07:08 +02:00
|
|
|
const routeSearch = route().params.search;
|
|
|
|
|
const [search, setSearch] = useState<string | null>(routeSearch);
|
2026-03-30 14:17:31 +02:00
|
|
|
|
2026-10-16 12:07:08 +02:00
|
|
|
const submit = (e: SubmitEvent) => {
|
2026-03-30 14:17:31 +02:00
|
|
|
if (!search) return;
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const params = {
|
|
|
|
|
...route().params,
|
|
|
|
|
search,
|
|
|
|
|
};
|
2026-10-16 12:07:08 +02:00
|
|
|
AddFilterToUrl({
|
|
|
|
|
modelRoute: modelRoute,
|
|
|
|
|
prefixRoute: prefixRoute,
|
2026-03-30 14:17:31 +02:00
|
|
|
params,
|
|
|
|
|
isArchiveRoute,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const onDeleteSearch = () => {
|
|
|
|
|
setSearch(null);
|
|
|
|
|
const params = {
|
|
|
|
|
...route().params,
|
|
|
|
|
search: undefined,
|
|
|
|
|
};
|
2026-10-16 12:07:08 +02:00
|
|
|
AddFilterToUrl({
|
|
|
|
|
modelRoute: modelRoute,
|
|
|
|
|
prefixRoute: prefixRoute,
|
2026-03-30 14:17:31 +02:00
|
|
|
params,
|
|
|
|
|
isArchiveRoute,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={submit}
|
|
|
|
|
className="flex justify-between items-end gap-2 mb-4"
|
|
|
|
|
>
|
|
|
|
|
<SimpleString
|
|
|
|
|
name="search"
|
2026-10-19 17:43:33 +02:00
|
|
|
label={trans("uwil-backend::back.table_search", {
|
2026-03-30 14:17:31 +02:00
|
|
|
params: { searchFields },
|
|
|
|
|
})}
|
|
|
|
|
placeholder="ex: John Doe"
|
|
|
|
|
value={search ?? undefined}
|
|
|
|
|
wordCounter={false}
|
|
|
|
|
setData={(name, value) => {
|
|
|
|
|
setSearch(value);
|
|
|
|
|
}}
|
|
|
|
|
className="w-[calc(100%-40px-0.5rem)]"
|
|
|
|
|
/>
|
|
|
|
|
{children}
|
|
|
|
|
<BtnOptionnalLinkWithTooltip
|
|
|
|
|
content={<DeleteIcon />}
|
|
|
|
|
tooltipContent={
|
2026-10-19 17:43:33 +02:00
|
|
|
<p>{trans("uwil-backend::back.table_search_reset")}</p>
|
2026-03-30 14:17:31 +02:00
|
|
|
}
|
|
|
|
|
type="button"
|
|
|
|
|
variant="destructive"
|
|
|
|
|
onClick={onDeleteSearch}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
|
|
|
|
<InputError all />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|