-
Notifications
You must be signed in to change notification settings - Fork 499
feat(@clayui/core): Add initial sketch of components to TreeView #4218
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,71 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
|
||
import {TreeViewGroup} from './TreeViewGroup'; | ||
import {TreeViewItem, TreeViewItemStack} from './TreeViewItem'; | ||
import {TreeViewContext} from './context'; | ||
import type {IExpandable, IMultipleSelection} from './useTree'; | ||
|
||
type ChildrenFunction<T> = (item: T) => React.ReactElement; | ||
|
||
interface ITreeViewProps<T> | ||
extends React.HTMLAttributes<HTMLUListElement>, | ||
IMultipleSelection, | ||
IExpandable { | ||
children: React.ReactNode | ChildrenFunction<T>; | ||
displayType?: 'light' | 'dark'; | ||
items?: Array<T>; | ||
showExpanderOnHover?: boolean; | ||
} | ||
|
||
export function TreeView<T>( | ||
props: ITreeViewProps<T> | ||
): JSX.Element & { | ||
Item: typeof TreeViewItem; | ||
Group: typeof TreeViewGroup; | ||
ItemStack: typeof TreeViewItemStack; | ||
}; | ||
|
||
export function TreeView<T>({ | ||
children, | ||
className, | ||
displayType = 'light', | ||
items, | ||
showExpanderOnHover = true, | ||
...otherProps | ||
}: ITreeViewProps<T>) { | ||
return ( | ||
<ul | ||
{...otherProps} | ||
className={classNames('treeview', className, { | ||
[`treeview-${displayType}`]: displayType, | ||
'show-component-expander-on-hover': showExpanderOnHover, | ||
})} | ||
role="tree" | ||
> | ||
<TreeViewContext.Provider value={{showExpanderOnHover}}> | ||
{items | ||
? items.map((item, index) => { | ||
if (typeof children === 'function') { | ||
return React.cloneElement( | ||
children(item) as React.ReactElement, | ||
{key: index} | ||
); | ||
} | ||
|
||
return null; | ||
}) | ||
: children} | ||
</TreeViewContext.Provider> | ||
</ul> | ||
); | ||
} | ||
|
||
TreeView.Item = TreeViewItem; | ||
TreeView.Group = TreeViewGroup; | ||
TreeView.ItemStack = TreeViewItemStack; |
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,42 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
type ChildrenFunction<T> = (item: T) => React.ReactElement; | ||
|
||
type TreeViewItemProps<T> = { | ||
children: React.ReactNode | ChildrenFunction<T>; | ||
items?: Array<T>; | ||
}; | ||
|
||
export function TreeViewGroup<T>( | ||
props: TreeViewItemProps<T> | ||
): JSX.Element & { | ||
displayName: string; | ||
}; | ||
|
||
export function TreeViewGroup<T>({children, items}: TreeViewItemProps<T>) { | ||
return ( | ||
<div className="collapse show"> | ||
<ul className="treeview-group" role="group"> | ||
{items | ||
? items.map((item, index) => { | ||
if (typeof children === 'function') { | ||
return React.cloneElement( | ||
children(item) as React.ReactElement, | ||
{key: index} | ||
); | ||
} | ||
|
||
return null; | ||
}) | ||
: children} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
TreeViewGroup.displayName = 'ClayTreeViewGroup'; |
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,117 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import Button from '@clayui/button'; | ||
import Icon from '@clayui/icon'; | ||
import Layout from '@clayui/layout'; | ||
import React, {useContext} from 'react'; | ||
|
||
type TreeViewItemProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const SpacingContext = React.createContext(0); | ||
|
||
export function TreeViewItem({children}: TreeViewItemProps) { | ||
const spacing = useContext(SpacingContext); | ||
|
||
const [left, right] = React.Children.toArray(children); | ||
|
||
const group = | ||
// @ts-ignore | ||
right?.type?.displayName === 'ClayTreeViewGroup' ? right : null; | ||
|
||
return ( | ||
<SpacingContext.Provider value={spacing + 24}> | ||
<li className="treeview-item" role="none"> | ||
<div | ||
aria-expanded="true" | ||
className="treeview-link" | ||
data-toggle="collapse" | ||
role="treeitem" | ||
style={{paddingLeft: `${spacing}px`}} | ||
tabIndex={0} | ||
> | ||
<span | ||
className="c-inner" | ||
style={{marginLeft: `-${spacing}px`}} | ||
tabIndex={-2} | ||
> | ||
{typeof left === 'string' ? ( | ||
<Layout.ContentRow> | ||
<Layout.ContentCol expand> | ||
<div className="component-text">{left}</div> | ||
</Layout.ContentCol> | ||
</Layout.ContentRow> | ||
) : group ? ( | ||
left | ||
) : ( | ||
<TreeViewItemStack expandable={false}> | ||
{children} | ||
</TreeViewItemStack> | ||
)} | ||
</span> | ||
</div> | ||
{group} | ||
</li> | ||
</SpacingContext.Provider> | ||
); | ||
} | ||
|
||
type TreeViewItemStackProps = { | ||
children: React.ReactNode; | ||
expandable?: boolean; | ||
}; | ||
|
||
export function TreeViewItemStack({ | ||
children, | ||
expandable = true, | ||
}: TreeViewItemStackProps) { | ||
const childrenArray = React.Children.toArray(children); | ||
|
||
return ( | ||
<Layout.ContentRow> | ||
{expandable && ( | ||
<Layout.ContentCol> | ||
<Button | ||
aria-expanded="true" | ||
className="component-expander" | ||
data-toggle="collapse" | ||
displayType={null} | ||
monospaced | ||
> | ||
<span className="c-inner" tabIndex={-2}> | ||
<Icon symbol="angle-down" /> | ||
<Icon | ||
className="component-expanded-d-none" | ||
symbol="angle-right" | ||
/> | ||
</span> | ||
</Button> | ||
</Layout.ContentCol> | ||
)} | ||
|
||
{React.Children.map(children, (child, index) => { | ||
let content = child; | ||
|
||
if (typeof child === 'string') { | ||
content = <div className="component-text">{child}</div>; | ||
|
||
// @ts-ignore | ||
} else if (child?.type.displayName === 'ClayIcon') { | ||
content = <div className="component-icon">{child}</div>; | ||
} | ||
|
||
return ( | ||
<Layout.ContentCol | ||
expand={index === childrenArray.length - 1} | ||
> | ||
{content} | ||
</Layout.ContentCol> | ||
); | ||
})} | ||
</Layout.ContentRow> | ||
); | ||
} |
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,16 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import React, {useContext} from 'react'; | ||
|
||
export interface ITreeViewContext { | ||
showExpanderOnHover?: boolean; | ||
} | ||
|
||
export const TreeViewContext = React.createContext<ITreeViewContext>({}); | ||
|
||
export function useTreeViewContext(): ITreeViewContext { | ||
return useContext(TreeViewContext); | ||
} |
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,6 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
export * from './TreeView'; |
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,28 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
export interface IExpandable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file will be used to handle the tree data structure, to help control multiple selection and expansion of |
||
/** | ||
* The currently expanded keys in the collection. | ||
*/ | ||
expandedKeys?: Set<string>; | ||
|
||
/** | ||
* Handler that is called when items are expanded or collapsed. | ||
*/ | ||
onExpandedChange?: (keys: Set<string>) => void; | ||
} | ||
|
||
export interface IMultipleSelection { | ||
/** | ||
* The currently selected keys in the collection. | ||
*/ | ||
selectedKeys?: Set<string>; | ||
|
||
/** | ||
* Handler that is called when the selection changes. | ||
*/ | ||
onSelectionChange?: (keys: Set<string>) => void; | ||
} |
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.
Unlike type on the other components in this one we are stopping using
React.FunctionComponent
as I had commenting in #4054. For more information on why, read also facebook/create-react-app#8177