Skip to content
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

Added property noSort to tree component, also updated documentation #810

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/tree/tree-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export interface TreeConfig {
onFileClick?: (path: string) => void
initialExpand: boolean
isImperative: boolean
noSort: boolean
}

const defaultContext = {
initialExpand: false,
isImperative: false,
noSort: false,
}

export const TreeContext = React.createContext<TreeConfig>(defaultContext)
Expand Down
4 changes: 2 additions & 2 deletions components/tree/tree-folder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const TreeFolder: React.FC<React.PropsWithChildren<TreeFolderProps>> = ({
...props
}: React.PropsWithChildren<TreeFolderProps> & typeof defaultProps) => {
const theme = useTheme()
const { initialExpand, isImperative } = useTreeContext()
const { initialExpand, isImperative, noSort } = useTreeContext()
const [expanded, setExpanded] = useState<boolean>(initialExpand)
useEffect(() => setExpanded(initialExpand), [])

Expand All @@ -55,7 +55,7 @@ const TreeFolder: React.FC<React.PropsWithChildren<TreeFolderProps>> = ({

const sortedChildren = isImperative
? nextChildren
: sortChildren(nextChildren, TreeFolder)
: sortChildren(nextChildren, TreeFolder, noSort)

return (
<div className={useClasses('folder', className)} onClick={clickHandler} {...props}>
Expand Down
16 changes: 11 additions & 5 deletions components/tree/tree-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import React, { ReactNode } from 'react'
export const sortChildren = (
children: ReactNode | undefined,
folderComponentType: React.ElementType,
noSort: boolean
) => {
return React.Children.toArray(children).sort((a, b) => {
if (!React.isValidElement(a) || !React.isValidElement(b)) return 0
if (a.type !== b.type) return a.type !== folderComponentType ? 1 : -1
return `${a.props.name}`.charCodeAt(0) - `${b.props.name}`.charCodeAt(0)
})
if (!noSort) {
// Sort
return React.Children.toArray(children).sort((a, b) => {
if (!React.isValidElement(a) || !React.isValidElement(b)) return 0
if (a.type !== b.type) return a.type !== folderComponentType ? 1 : -1
return `${a.props.name}`.charCodeAt(0) - `${b.props.name}`.charCodeAt(0)
})
} else {
return React.Children.toArray(children);
}
}

export const makeChildPath = (name: string, parentPath?: string) => {
Expand Down
20 changes: 13 additions & 7 deletions components/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,36 @@ interface Props {
initialExpand?: boolean
onClick?: (path: string) => void
className?: string
noSort?: boolean
}

const defaultProps = {
initialExpand: false,
className: '',
noSort: false,
}

type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type TreeProps = Props & NativeAttrs

const makeChildren = (value: Array<TreeFile> = []) => {
const makeChildren = (value: Array<TreeFile> = [], noSort: boolean) => {
if (!value || !value.length) return null
return value
.sort((a, b) => {
if (!noSort) {
// Sort
value = value.sort((a, b) => {
if (a.type !== b.type) return a.type !== directoryType ? 1 : -1

return `${a.name}`.charCodeAt(0) - `${b.name}`.charCodeAt(0)
})
.map((item, index) => {
}
return value.map((item, index) => {
if (item.type === directoryType)
return (
<TreeFolder
name={item.name}
extra={item.extra}
key={`folder-${item.name}-${index}`}>
{makeChildren(item.files)}
{makeChildren(item.files, noSort)}
</TreeFolder>
)
return (
Expand All @@ -66,6 +70,7 @@ const Tree: React.FC<React.PropsWithChildren<TreeProps>> = ({
initialExpand,
value,
className,
noSort,
...props
}: React.PropsWithChildren<TreeProps> & typeof defaultProps) => {
const isImperative = Boolean(value && value.length > 0)
Expand All @@ -78,13 +83,14 @@ const Tree: React.FC<React.PropsWithChildren<TreeProps>> = ({
onFileClick,
initialExpand,
isImperative,
noSort,
}),
[initialExpand],
)

const customChildren = isImperative
? makeChildren(value)
: sortChildren(children, TreeFolder)
? makeChildren(value, noSort)
: sortChildren(children, TreeFolder, noSort)

return (
<TreeContext.Provider value={initialValue}>
Expand Down
14 changes: 12 additions & 2 deletions pages/en-us/components/file-tree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Display a list of files and folders in a hierarchical tree structure.

<Playground
title="Extra Message"
desc="Use props `value` to show more complex file tree."
desc="Use props `value` to show more complex file tree. Also ask for NO sorting"
scope={{ Tree }}
code={`
() => {
Expand Down Expand Up @@ -102,8 +102,17 @@ Display a list of files and folders in a hierarchical tree structure.
type: 'file',
name: 'views.md',
extra: '8.1kb',
}, {
type: 'file',
name: 'abc.md',
extra: '9kb',
}, {
type: 'directory',
name: 'empty',
extra: '0 files',
files: [],
}]
return <Tree value={files} />
return <Tree value={files} noSort="true" />
}
`}
/>
Expand Down Expand Up @@ -142,6 +151,7 @@ Display a list of files and folders in a hierarchical tree structure.
| ----------------- | ----------------- | ------------------------ | --------------------------------- | ------- |
| **value** | value of files | `Array<FileTreeValue>` | - | - |
| **initialExpand** | expand by default | `boolean` | - | `false` |
| **noSort** | no sort elements | `boolean` | - | `false` |
| **onClick** | click file event | `(path: string) => void` | - | - |
| ... | native props | `HTMLAttributes` | `'id', 'title', 'className', ...` | - |

Expand Down