import { memo, useCallback, useMemo, useState } from 'react';
import { LegalEntityBrief } from '../../types/api';
import { Checkbox, Empty, Input, Spin } from 'antd';
type RowProps = {
entity: LegalEntityBrief;
checked: boolean;
onToggle: (id: string) => void;
};
const CompanyOptionRow = memo(({ entity, checked, onToggle }: RowProps) => (
onToggle(entity.id)}
onKeyDown={(e) => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
onToggle(entity.id);
}
}}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
width: '100%',
minWidth: 0,
gap: 12,
padding: '10px 8px',
borderRadius: 4,
borderBottom: '1px solid rgba(0, 0, 0, 0.05)',
cursor: 'pointer',
boxSizing: 'border-box',
}}>
{entity.name}
{entity.inn}
));
type CompanyMultiSelectProps = {
companies: LegalEntityBrief[];
loading?: boolean;
// injected by Form.Item:
value?: string[];
onChange?: (value: string[]) => void;
};
export const CompanyMultiSelect: React.FC = ({ companies, loading, value = [], onChange }) => {
const [search, setSearch] = useState('');
const toggle = useCallback(
(id: string) => {
const next = value.includes(id) ? value.filter((v) => v !== id) : [...value, id];
onChange?.(next);
},
[value, onChange],
);
const filtered = useMemo(() => {
if (!search) return companies;
const q = search.toLowerCase();
return companies.filter((s) => s.name.toLowerCase().includes(q));
}, [companies, search]);
return (
setSearch(e.target.value)}
style={{ marginBottom: 12 }}
/>
{loading ? (
) : null}
{!loading && filtered.length === 0 ? (
) : (
{filtered.map((s) => (
))}
)}
{!loading && companies.length > 0 && (
Выбрано: {value.length} / {companies.length}
)}
);
};