feat: автоматический выбор подсправочника по привязке к строке родителя

This commit is contained in:
2026-07-21 14:24:35 +03:00
parent 2e1652e7ee
commit dee9ae7ad6
2 changed files with 17 additions and 60 deletions

View File

@@ -47,11 +47,6 @@ export default function SubdirectoryFieldConfig({
const subdirectories = subdirectoriesData?.subdirectories || []; const subdirectories = subdirectoriesData?.subdirectories || [];
const showSubdirectory = !!field.options?.showSubdirectory; const showSubdirectory = !!field.options?.showSubdirectory;
const selectedSubdirectoryId = field.options?.subdirectoryId as number | undefined;
const selectedSubdirectory = useMemo(() => {
return subdirectories.find((s) => s.id === selectedSubdirectoryId || s.childTableId === selectedSubdirectoryId);
}, [subdirectories, selectedSubdirectoryId]);
const selectedTable = useMemo(() => { const selectedTable = useMemo(() => {
return tablesData?.tables?.find((t) => t.id === tableId); return tablesData?.tables?.find((t) => t.id === tableId);
@@ -65,7 +60,6 @@ export default function SubdirectoryFieldConfig({
tableId: undefined, tableId: undefined,
displayColumn: undefined, displayColumn: undefined,
showSubdirectory: undefined, showSubdirectory: undefined,
subdirectoryId: undefined,
}, },
}); });
return; return;
@@ -78,7 +72,6 @@ export default function SubdirectoryFieldConfig({
tableId: newTableId, tableId: newTableId,
displayColumn: newSelectedTable?.columns?.[0]?.name || undefined, displayColumn: newSelectedTable?.columns?.[0]?.name || undefined,
showSubdirectory: undefined, showSubdirectory: undefined,
subdirectoryId: undefined,
}, },
}); });
}; };
@@ -88,18 +81,6 @@ export default function SubdirectoryFieldConfig({
options: { options: {
...field.options, ...field.options,
showSubdirectory: checked, showSubdirectory: checked,
subdirectoryId: checked ? field.options?.subdirectoryId : undefined,
},
});
};
const handleSubdirectoryChange = (value: string) => {
const newSubdirectory = value === 'none' ? undefined : subdirectories.find((s) => s.id?.toString() === value || s.childTableId?.toString() === value);
updateField(index, {
options: {
...field.options,
subdirectoryId: newSubdirectory?.id,
childTableId: newSubdirectory?.childTableId,
}, },
}); });
}; };
@@ -178,35 +159,15 @@ export default function SubdirectoryFieldConfig({
</div> </div>
{showSubdirectory && ( {showSubdirectory && (
<div> <p className="text-xs text-muted-foreground">
<Label className="text-xs">Подсправочник</Label> При выборе строки родительского справочника будет показан подсправочник, привязанный к этой строке.
<Select value={selectedSubdirectoryId?.toString() || 'none'} onValueChange={handleSubdirectoryChange}> {subdirectories.length > 0
<SelectTrigger> ? ` Найдено подсправочников: ${subdirectories.length}.`
<SelectValue placeholder="Выберите подсправочник" /> : ' Нет привязанных подсправочников.'}
</SelectTrigger>
<SelectContent>
<SelectItem value="none">Не выбрано</SelectItem>
{subdirectories.length === 0 && (
<SelectItem value="no-subdirectories" disabled>
Нет привязанных подсправочников
</SelectItem>
)}
{subdirectories.map((sub) => (
<SelectItem key={sub.id} value={sub.id?.toString() || sub.childTableId?.toString() || ''}>
{sub.childTable?.name || `Справочник ${sub.childTableId}`}
</SelectItem>
))}
</SelectContent>
</Select>
{selectedSubdirectory && (
<p className="text-xs text-muted-foreground mt-1">
Подсправочник показывается при выборе привязанных строк родителя.
</p> </p>
)} )}
</div> </div>
)} )}
</div> </div>
)}
</div>
); );
} }

View File

@@ -65,7 +65,6 @@ export default function TableFieldWithSubdirectory({
const opts = options && !Array.isArray(options) ? (options as Record<string, any>) : {}; const opts = options && !Array.isArray(options) ? (options as Record<string, any>) : {};
const tableId = opts.tableId as number | undefined; const tableId = opts.tableId as number | undefined;
const showSubdirectory = !!opts.showSubdirectory; const showSubdirectory = !!opts.showSubdirectory;
const subdirectoryId = opts.subdirectoryId as number | undefined;
const { data: subdirectoriesData } = useQuery<{ subdirectories: DataTableSubdirectory[] }>({ const { data: subdirectoriesData } = useQuery<{ subdirectories: DataTableSubdirectory[] }>({
queryKey: ['/api/directories', tableId, 'subdirectories'], queryKey: ['/api/directories', tableId, 'subdirectories'],
@@ -77,16 +76,14 @@ export default function TableFieldWithSubdirectory({
staleTime: 60 * 1000, staleTime: 60 * 1000,
}); });
const subdirectory = useMemo(() => {
return subdirectoriesData?.subdirectories?.find((s) => s.id === subdirectoryId);
}, [subdirectoriesData, subdirectoryId]);
const { parentId, childId } = parseTableValue(value); const { parentId, childId } = parseTableValue(value);
const isParentLinked = useMemo(() => { const subdirectory = useMemo(() => {
if (!subdirectory || !parentId) return false; if (!parentId) return undefined;
return subdirectory.parentRowIds?.includes(Number(parentId)) ?? false; return subdirectoriesData?.subdirectories?.find((s) =>
}, [subdirectory, parentId]); s.parentRowIds?.includes(Number(parentId))
);
}, [subdirectoriesData, parentId]);
const handleParentChange = (newParentId: string) => { const handleParentChange = (newParentId: string) => {
onChange(serializeTableValue(newParentId || null, null)); onChange(serializeTableValue(newParentId || null, null));
@@ -96,7 +93,7 @@ export default function TableFieldWithSubdirectory({
onChange(serializeTableValue(parentId, newChildId || null)); onChange(serializeTableValue(parentId, newChildId || null));
}; };
if (!showSubdirectory || !subdirectory || allowMultiple) { if (!showSubdirectory || allowMultiple) {
const SelectComponent = allowMultiple ? TableFieldMultiSelect : TableFieldSelect; const SelectComponent = allowMultiple ? TableFieldMultiSelect : TableFieldSelect;
return ( return (
<SelectComponent <SelectComponent
@@ -111,10 +108,9 @@ export default function TableFieldWithSubdirectory({
} }
const parentOptions = opts; const parentOptions = opts;
const childOptions = { const childOptions = subdirectory
...opts, ? { ...opts, tableId: subdirectory.childTableId }
tableId: subdirectory.childTableId, : opts;
};
return ( return (
<div className="space-y-2"> <div className="space-y-2">
@@ -126,7 +122,7 @@ export default function TableFieldWithSubdirectory({
value={parentId || ''} value={parentId || ''}
onChange={handleParentChange} onChange={handleParentChange}
/> />
{isParentLinked && ( {subdirectory && (
<TableFieldSelect <TableFieldSelect
fieldId={fieldId} fieldId={fieldId}
fieldCode={`${fieldCode}_sub`} fieldCode={`${fieldCode}_sub`}