-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4222 from matuzalemsteles/issue-4210
feat(@clayui/core): adds functionality for expandable nodes and nested nodes
- Loading branch information
Showing
10 changed files
with
864 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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,70 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import {ItemContextProvider, useItem} from './useItem'; | ||
|
||
export type ChildrenFunction<T> = (item: T) => React.ReactElement; | ||
|
||
export interface ICollectionProps<T> { | ||
children: React.ReactNode | ChildrenFunction<T>; | ||
items?: Array<T>; | ||
} | ||
|
||
function getKey(index: number, key?: React.Key | null, parentKey?: React.Key) { | ||
if ( | ||
key != null && | ||
(!String(key).startsWith('.') || String(key).startsWith('.$')) | ||
) { | ||
return key; | ||
} | ||
|
||
return parentKey ? `${parentKey}.${index}` : `$.${index}`; | ||
} | ||
|
||
export function Collection<T extends Record<any, any>>({ | ||
children, | ||
items, | ||
}: ICollectionProps<T>) { | ||
const {key: parentKey} = useItem(); | ||
|
||
return ( | ||
<> | ||
{typeof children === 'function' && items | ||
? items.map((item, index) => { | ||
const child = children(item); | ||
|
||
const key = getKey( | ||
index, | ||
item.id ?? child.key, | ||
parentKey | ||
); | ||
|
||
return ( | ||
<ItemContextProvider | ||
key={key} | ||
value={{...item, key}} | ||
> | ||
{child} | ||
</ItemContextProvider> | ||
); | ||
}) | ||
: React.Children.toArray(children).map((child, index) => { | ||
if (!React.isValidElement(child)) { | ||
return null; | ||
} | ||
|
||
const key = getKey(index, child.key, parentKey); | ||
|
||
return ( | ||
<ItemContextProvider key={key} value={{key}}> | ||
{child} | ||
</ItemContextProvider> | ||
); | ||
})} | ||
</> | ||
); | ||
} |
This file contains 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 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
Oops, something went wrong.