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

View File

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

View File

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