FactuGES_ERP/modules/customer-invoices/src/web/components/customer-invoice-taxes-multi-select.tsx

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-10-08 15:44:08 +00:00
import { SpainTaxCatalogProvider } from '@erp/core';
2025-07-16 15:47:45 +00:00
import { MultiSelect } from "@repo/rdx-ui/components";
import { cn } from "@repo/shadcn-ui/lib/utils";
2025-10-08 15:44:08 +00:00
import { useCallback, useMemo } from 'react';
2025-07-16 15:47:45 +00:00
import { useTranslation } from "../i18n";
2025-10-08 15:44:08 +00:00
2025-07-16 15:47:45 +00:00
interface CustomerInvoiceTaxesMultiSelect {
2025-10-16 11:18:55 +00:00
value?: string[];
2025-07-16 15:47:45 +00:00
onChange: (selectedValues: string[]) => void;
2025-10-11 16:44:38 +00:00
className?: string;
2025-10-16 17:59:13 +00:00
inputId?: string;
2025-07-16 15:47:45 +00:00
[key: string]: any; // Allow other props to be passed
}
export const CustomerInvoiceTaxesMultiSelect = (props: CustomerInvoiceTaxesMultiSelect) => {
2025-10-16 17:59:13 +00:00
const { value, onChange, className, inputId, ...otherProps } = props;
2025-07-16 15:47:45 +00:00
const { t } = useTranslation();
2025-10-08 15:44:08 +00:00
const taxCatalog = useMemo(() => SpainTaxCatalogProvider(), []);
const catalogLookup = useMemo(() => SpainTaxCatalogProvider().toOptionLookup(), []);
/**
* Filtra para mantener solo un elemento por grupo.
* Si hay duplicados dentro del mismo grupo, se queda con el último.
*/
const filterSelectedByGroup = useCallback((selectedValues: string[]) => {
const groupMap = new Map<string | undefined, string>();
selectedValues.forEach((code) => {
const item = taxCatalog.findByCode(code).getOrUndefined();
const group = item?.group ?? "ungrouped";
groupMap.set(group, code); // Sobrescribe el anterior del mismo grupo
});
2025-07-16 15:47:45 +00:00
2025-10-08 15:44:08 +00:00
return Array.from(groupMap.values());
}, [taxCatalog]);
2025-07-16 15:47:45 +00:00
return (
<div className={cn("w-full", "max-w-md")}>
<MultiSelect
2025-10-16 17:59:13 +00:00
id={inputId}
2025-10-08 15:44:08 +00:00
options={catalogLookup}
onValueChange={onChange}
2025-07-16 15:47:45 +00:00
defaultValue={value}
placeholder={t("components.customer_invoice_taxes_multi_select.placeholder")}
2025-10-08 16:50:48 +00:00
variant='secondary'
2025-07-16 15:47:45 +00:00
animation={0}
maxCount={3}
2025-10-08 15:44:08 +00:00
autoFilter={true}
filterSelected={filterSelectedByGroup}
2025-10-11 16:44:38 +00:00
className={cn(
"flex w-full -mt-0.5 px-1 py-0.5 rounded-md border min-h-8 h-auto items-center justify-between bg-background hover:bg-inherit [&_svg]:pointer-events-auto",
"hover:border-ring hover:ring-ring/50 hover:ring-[2px]",
className
)}
2025-07-16 15:47:45 +00:00
{...otherProps}
/>
</div>
);
};