2024-03-20 15:03:53 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2024-10-29 12:45:03 +03:00
|
|
|
|
import clsx from 'clsx';
|
2025-02-05 12:43:16 +03:00
|
|
|
|
import { useState } from 'react';
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
|
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/DataTable';
|
2025-01-28 23:23:42 +03:00
|
|
|
|
import { CProps } from '@/components/props';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
import { SearchBar } from '@/components/shared/SearchBar';
|
|
|
|
|
import { NoData } from '@/components/View';
|
2024-12-16 23:52:11 +03:00
|
|
|
|
import { APP_COLORS } from '@/styling/color';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
|
2025-02-11 20:56:24 +03:00
|
|
|
|
import { describeConstituenta } from '../labels';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
import { IConstituenta } from '../models/rsform';
|
|
|
|
|
import { matchConstituenta } from '../models/rsformAPI';
|
|
|
|
|
import { CstMatchMode } from '../stores/cstSearch';
|
|
|
|
|
import BadgeConstituenta from './BadgeConstituenta';
|
|
|
|
|
|
2024-10-29 12:45:03 +03:00
|
|
|
|
interface PickConstituentaProps extends CProps.Styling {
|
2024-03-18 16:21:39 +03:00
|
|
|
|
id?: string;
|
2025-02-05 12:43:16 +03:00
|
|
|
|
items: IConstituenta[];
|
2025-02-04 20:35:55 +03:00
|
|
|
|
value?: IConstituenta;
|
|
|
|
|
onChange: (newValue: IConstituenta) => void;
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
rows?: number;
|
|
|
|
|
|
2024-01-29 14:44:51 +03:00
|
|
|
|
initialFilter?: string;
|
2023-12-28 14:04:44 +03:00
|
|
|
|
onBeginFilter?: (cst: IConstituenta) => boolean;
|
|
|
|
|
describeFunc?: (cst: IConstituenta) => string;
|
|
|
|
|
matchFunc?: (cst: IConstituenta, filter: string) => boolean;
|
2023-11-06 18:03:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
function PickConstituenta({
|
2024-03-18 16:21:39 +03:00
|
|
|
|
id,
|
2025-02-05 12:43:16 +03:00
|
|
|
|
items,
|
2023-12-28 14:04:44 +03:00
|
|
|
|
value,
|
2024-01-29 14:44:51 +03:00
|
|
|
|
initialFilter = '',
|
2023-11-06 18:03:23 +03:00
|
|
|
|
rows = 4,
|
|
|
|
|
describeFunc = describeConstituenta,
|
|
|
|
|
matchFunc = (cst, filter) => matchConstituenta(cst, filter, CstMatchMode.ALL),
|
2023-12-27 19:34:39 +03:00
|
|
|
|
onBeginFilter,
|
2025-02-04 20:35:55 +03:00
|
|
|
|
onChange,
|
2024-10-29 12:45:03 +03:00
|
|
|
|
className,
|
|
|
|
|
...restProps
|
2024-05-16 22:39:28 +03:00
|
|
|
|
}: PickConstituentaProps) {
|
2024-01-29 14:44:51 +03:00
|
|
|
|
const [filterText, setFilterText] = useState(initialFilter);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
2025-02-05 12:43:16 +03:00
|
|
|
|
const initialData = onBeginFilter ? items.filter(onBeginFilter) : items;
|
|
|
|
|
const filteredData = filterText === '' ? initialData : initialData.filter(cst => matchFunc(cst, filterText));
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
|
const columns = [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
size: 65,
|
|
|
|
|
minSize: 65,
|
|
|
|
|
maxSize: 65,
|
2025-02-05 12:43:16 +03:00
|
|
|
|
cell: props => <BadgeConstituenta value={props.row.original} />
|
2024-12-13 21:31:09 +03:00
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeFunc(cst), {
|
|
|
|
|
id: 'description',
|
|
|
|
|
size: 1000,
|
|
|
|
|
minSize: 1000
|
|
|
|
|
})
|
|
|
|
|
];
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
|
const conditionalRowStyles: IConditionalStyle<IConstituenta>[] = [
|
|
|
|
|
{
|
|
|
|
|
when: (cst: IConstituenta) => cst.id === value?.id,
|
2024-12-16 23:52:11 +03:00
|
|
|
|
style: { backgroundColor: APP_COLORS.bgSelected }
|
2024-12-13 21:31:09 +03:00
|
|
|
|
}
|
|
|
|
|
];
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
|
|
|
|
return (
|
2024-10-29 12:45:03 +03:00
|
|
|
|
<div className={clsx('border divide-y', className)} {...restProps}>
|
2024-03-18 16:21:39 +03:00
|
|
|
|
<SearchBar
|
|
|
|
|
id={id ? `${id}__search` : undefined}
|
2024-10-29 12:06:43 +03:00
|
|
|
|
className='clr-input rounded-t-md'
|
2024-03-18 16:21:39 +03:00
|
|
|
|
noBorder
|
2024-11-21 15:09:51 +03:00
|
|
|
|
query={filterText}
|
|
|
|
|
onChangeQuery={newValue => setFilterText(newValue)}
|
2024-03-18 16:21:39 +03:00
|
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<DataTable
|
2024-03-18 16:21:39 +03:00
|
|
|
|
id={id}
|
2024-03-20 15:03:53 +03:00
|
|
|
|
rows={rows}
|
|
|
|
|
contentHeight='1.3rem'
|
2023-12-28 14:04:44 +03:00
|
|
|
|
dense
|
|
|
|
|
noHeader
|
|
|
|
|
noFooter
|
2024-05-02 21:19:23 +03:00
|
|
|
|
className='text-sm select-none cc-scroll-y'
|
2023-12-28 14:04:44 +03:00
|
|
|
|
data={filteredData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={
|
2024-06-21 19:27:36 +03:00
|
|
|
|
<NoData className='min-h-[6rem]'>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<p>Список конституент пуст</p>
|
|
|
|
|
<p>Измените параметры фильтра</p>
|
2024-06-21 19:27:36 +03:00
|
|
|
|
</NoData>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
}
|
2025-02-04 20:35:55 +03:00
|
|
|
|
onRowClicked={onChange}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
export default PickConstituenta;
|