fix: single and multi data source

This commit is contained in:
dream2023 2024-01-18 16:30:38 +08:00
parent a15a72a113
commit 48e4b2118d
3 changed files with 22 additions and 26 deletions

View File

@ -109,19 +109,27 @@ const LoadingItem = ({ loadMore, maxHeight }) => {
);
};
export function useMenuSearch(data: any, openKey: string, showType?: boolean) {
export function useMenuSearch(data: any[], openKeys: string[], showType?: boolean) {
const [searchValue, setSearchValue] = useState('');
const [count, setCount] = useState(STEP);
const isMuliSource = useMemo(() => data.length > 1, [data]);
const openKey = useMemo(() => {
return isMuliSource ? openKeys?.[1] : openKeys?.length > 0;
}, [openKeys]);
useEffect(() => {
if (openKey) {
if (!openKey) {
setSearchValue('');
}
}, [openKey]);
const currentItems = useMemo(() => {
if (isMuliSource) {
if (!openKey) return [];
return data.find((item) => (item.key || item.name) === openKey)?.children || [];
}
return data[0].children || [];
}, [data, openKey]);
// 根据搜索的值进行处理
@ -215,6 +223,7 @@ export function useMenuSearch(data: any, openKey: string, showType?: boolean) {
}, [limitedSearchedItems, searchValue, shouldLoadMore, showType]);
const res = useMemo(() => {
if (!isMuliSource) return resultItems;
return data.map((item) => {
if (openKey && item.key === openKey) {
return {
@ -288,11 +297,10 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => {
[createBlockSchema, getTemplateSchemaByMode, insert, isCusomeizeCreate, onCreateBlockSchema, templateWrap],
);
const items = useCollectionDataSourceItems(componentType, filter);
console.log('items', items);
const getMenuItems = useGetSchemaInitializerMenuItems(onClick);
const childItems = useMemo(() => getMenuItems(items, name), [items]);
const [openMenuKey, setOpenMenuKey] = useState('');
const searchedChildren = useMenuSearch(childItems, openMenuKey);
const [openMenuKeys, setOpenMenuKeys] = useState([]);
const searchedChildren = useMenuSearch(childItems, openMenuKeys);
const compiledMenuItems = useMemo(
() => [
{
@ -312,11 +320,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => {
return (
<SchemaInitializerMenu
onOpenChange={(keys) => {
if (keys.length === 2) {
setOpenMenuKey(keys[1]);
} else if (keys.length === 0) {
setOpenMenuKey('');
}
setOpenMenuKeys(keys);
}}
items={compiledMenuItems}
/>

View File

@ -64,8 +64,8 @@ export default {
})),
[allCollections],
);
const [openMenuKey, setOpenMenuKey] = useState('');
const searchedChildren = useMenuSearch(childItems, openMenuKey);
const [openMenuKeys, setOpenMenuKeys] = useState([]);
const searchedChildren = useMenuSearch(childItems, openMenuKeys);
return {
name: 'createRecordForm',
@ -74,11 +74,7 @@ export default {
title: `{{t("Create record form", { ns: "${NAMESPACE}" })}}`,
componentProps: {
onOpenChange(keys) {
if (keys.length === 2) {
setOpenMenuKey(keys[1]);
} else if (keys.length === 0) {
setOpenMenuKey('');
}
setOpenMenuKeys(keys);
},
},
children: searchedChildren,

View File

@ -101,8 +101,8 @@ export default {
})),
[allCollections],
);
const [openMenuKey, setOpenMenuKey] = useState('');
const searchedChildren = useMenuSearch(childItems, openMenuKey);
const [openMenuKeys, setOpenMenuKeys] = useState([]);
const searchedChildren = useMenuSearch(childItems, openMenuKeys);
return {
name: 'updateRecordForm',
key: 'updateRecordForm',
@ -110,11 +110,7 @@ export default {
title: `{{t("Update record form", { ns: "${NAMESPACE}" })}}`,
componentProps: {
onOpenChange(keys) {
if (keys.length === 2) {
setOpenMenuKey(keys[1]);
} else if (keys.length === 0) {
setOpenMenuKey('');
}
setOpenMenuKeys(keys);
},
},
children: searchedChildren,