-
Notifications
You must be signed in to change notification settings - Fork 40
feat(catalogue): add catalogue #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LuckyFBB
wants to merge
9
commits into
DTStack:master
Choose a base branch
from
LuckyFBB:feat/catalogue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3247f75
feat(catalogue): add catalogue
LuckyFBB 7584b49
fix: add useTreeData return type
LuckyFBB 99e2000
feat(catalogue): change catalogue and add examples
LuckyFBB 97b4259
feat(catalogue): add custom overlay, form.error msg and drag style
LuckyFBB 9545f76
feat(catalogue): support title and icon, change CatalogueTree
LuckyFBB 2e730f5
feat(catalogue): support tabs in cataligue
LuckyFBB 98ac9d1
feat(catalogue): change catalogue and add some utils to CRUD treedata
LuckyFBB 1cb0c29
feat(catalogue): remove some unuse icon
LuckyFBB d09bcee
fix(catalogue): remove some function to utils
LuckyFBB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
import React, { useState } from 'react'; | ||
import { Dropdown, DropdownProps, Form, Input, Tabs } from 'antd'; | ||
import { BlockHeader, EllipsisText } from 'dt-react-component'; | ||
import { IBlockHeaderProps } from 'dt-react-component/blockHeader'; | ||
|
||
import { ITreeNode } from '../useTreeData'; | ||
import { CatalogIcon, CloseIcon, DragIcon, EllipsisIcon, SearchIcon } from './icon'; | ||
import CatalogueTree, { ICatalogueTree } from './tree'; | ||
|
||
interface Tab { | ||
readonly key: string; | ||
readonly title: React.ReactNode; | ||
} | ||
|
||
type readOnlyTab = readonly Tab[]; | ||
|
||
type TabKey<T extends readOnlyTab> = T[number]['key']; | ||
|
||
interface NormalCatalogueProps<U extends Record<string, any> = {}> | ||
extends Partial<Pick<IBlockHeaderProps, 'tooltip' | 'addonAfter' | 'addonBefore' | 'title'>>, | ||
ICatalogueTree<U> { | ||
showSearch?: boolean; | ||
edit?: boolean; | ||
placeholder?: string; | ||
loading?: boolean; | ||
onCancelSave?: (item: ITreeNode<U>) => void; | ||
overlay?: (item: ITreeNode<U>) => DropdownProps['overlay']; | ||
onSearch?: (value: string) => void; | ||
onSave?: (data: ITreeNode<U>, value: string) => Promise<string | void>; | ||
} | ||
interface TabsCatalogueProps<U extends Record<string, any>, T extends readOnlyTab> | ||
extends NormalCatalogueProps<U> { | ||
tabList?: T; | ||
activeTabKey?: TabKey<T>; | ||
defaultTabKey?: TabKey<T>; | ||
onTabChange?: (key: TabKey<T>) => void; | ||
} | ||
|
||
export type CatalogueProps<U extends Record<string, any> = {}, T extends readOnlyTab = any> = | ||
| TabsCatalogueProps<U, T> | ||
| NormalCatalogueProps<U>; | ||
|
||
function isTabMode<U extends Record<string, any> = {}, T extends readOnlyTab = any>( | ||
props: CatalogueProps<U, T> | ||
): props is TabsCatalogueProps<U, T> { | ||
return 'tabList' in props; | ||
} | ||
|
||
const Catalogue = <U extends Record<string, any> = {}, T extends readOnlyTab = any>( | ||
props: CatalogueProps<U, T> | ||
) => { | ||
const { | ||
title, | ||
addonBefore = <CatalogIcon style={{ fontSize: 20 }} />, | ||
tooltip = false, | ||
showSearch = false, | ||
placeholder = '搜索目录名称', | ||
addonAfter, | ||
edit = true, | ||
treeData, | ||
draggable, | ||
titleRender, | ||
overlay, | ||
onSearch, | ||
onSave, | ||
onCancelSave, | ||
...rest | ||
} = props; | ||
|
||
const [tabSearch, setTabSearch] = useState(false); | ||
|
||
const [form] = Form.useForm(); | ||
|
||
const defaultTitleRender = (item: ITreeNode<U>) => { | ||
if (item.edit) { | ||
return ( | ||
<Form form={form} preserve={false} className="tree__title--input"> | ||
<Form.Item name="catalog_input" initialValue={item?.title as string}> | ||
<Input | ||
size="small" | ||
placeholder={`请输入${title}名称`} | ||
maxLength={100} | ||
autoFocus | ||
onFocus={() => form.setFields([{ name: 'catalog_input', errors: [] }])} | ||
onClick={(e) => e.stopPropagation()} | ||
onBlur={({ target }) => handleInputSubmit(item, target.value)} | ||
onPressEnter={({ target }) => | ||
handleInputSubmit(item, (target as any).value) | ||
} | ||
/> | ||
</Form.Item> | ||
</Form> | ||
); | ||
} | ||
return ( | ||
<div className="tree__title"> | ||
<div className="tree__title--text"> | ||
<EllipsisText value={item.title} watchParentSizeChange maxWidth="100%" /> | ||
</div> | ||
{edit && renderNodeHover(item)} | ||
</div> | ||
); | ||
}; | ||
|
||
const renderHeader = () => { | ||
if (!title) return null; | ||
return ( | ||
<div className="dt-catalogue__header"> | ||
<BlockHeader | ||
title={title} | ||
tooltip={tooltip} | ||
background={false} | ||
addonBefore={addonBefore} | ||
addonAfter={addonAfter} | ||
spaceBottom={12} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const renderSearch = () => { | ||
if (!showSearch || (isTabMode(props) && !tabSearch)) return null; | ||
return ( | ||
<div className="dt-catalogue__search"> | ||
<Input.Search placeholder={placeholder} onSearch={onSearch} /> | ||
{isTabMode(props) && ( | ||
<CloseIcon className="close" style={{}} onClick={() => setTabSearch(false)} /> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
const renderTab = () => { | ||
if (!isTabMode(props) || tabSearch) return null; | ||
const { activeTabKey, tabList, onTabChange } = props; | ||
return ( | ||
<Tabs | ||
className="dt-catalogue__tabs" | ||
size="small" | ||
tabBarExtraContent={ | ||
<SearchIcon className="search" onClick={() => setTabSearch(true)} /> | ||
} | ||
activeKey={activeTabKey} | ||
onChange={onTabChange} | ||
> | ||
{tabList?.map((tab: { key: string; title: React.ReactNode }) => ( | ||
<Tabs.TabPane tab={tab.title} key={tab.key} /> | ||
))} | ||
</Tabs> | ||
); | ||
}; | ||
|
||
const handleInputSubmit = (item: ITreeNode<U>, value: string) => { | ||
if (!value) { | ||
return onCancelSave?.(item); | ||
} | ||
onSave?.(item, value).then((msg) => { | ||
form.setFields([{ name: 'catalog_input', errors: msg ? [msg] : [] }]); | ||
}); | ||
}; | ||
|
||
const renderNodeHover = (item: ITreeNode<U>) => { | ||
return ( | ||
<div | ||
className="tree__title--operation" | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
}} | ||
> | ||
{overlay && ( | ||
<Dropdown | ||
overlay={overlay(item)} | ||
placement="bottomRight" | ||
arrow | ||
destroyPopupOnHide | ||
getPopupContainer={(triggerNode) => | ||
triggerNode.parentElement as HTMLElement | ||
} | ||
> | ||
<EllipsisIcon onClick={(e) => e.stopPropagation()} /> | ||
</Dropdown> | ||
)} | ||
{draggable && <DragIcon />} | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="dt-catalogue"> | ||
{renderHeader()} | ||
{renderSearch()} | ||
{renderTab()} | ||
<CatalogueTree | ||
treeData={treeData} | ||
draggable={draggable ? { icon: false } : false} | ||
titleRender={titleRender || defaultTitleRender} | ||
{...rest} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Catalogue; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑不要作这个操作,交给用户去做