Skip to content
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clevertask/react-sortable-tree",
"version": "0.0.5",
"version": "0.0.6",
"type": "module",
"license": "MIT",
"author": "CleverTask",
Expand Down
13 changes: 12 additions & 1 deletion src/SortableTree/SortableTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function PrivateSortableTree({
onAddItem,
onDragEnd,
onItemClick,
renderItem,
}: SortableTreeProps) {
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
const [overId, setOverId] = useState<UniqueIdentifier | null>(null);
Expand Down Expand Up @@ -339,7 +340,16 @@ function PrivateSortableTree({
>
<SortableContext items={sortedIds} strategy={verticalListSortingStrategy}>
{flattenedItems.map(
({ id, label, children, collapsed, depth, canFetchChildren, disableDragging }) => (
({
id,
label,
children,
collapsed,
depth,
canFetchChildren,
disableDragging,
...rest
}) => (
<SortableTreeItem
key={id}
id={id}
Expand All @@ -359,6 +369,7 @@ function PrivateSortableTree({
}
onAdd={allowNestedItemAddition ? () => onAddItem?.(id) : undefined}
onLabelClick={onItemClick ? () => onItemClick(id) : undefined}
renderedItem={renderItem?.({ id, label, children, ...rest })}
/>
),
)}
Expand Down
3 changes: 3 additions & 0 deletions src/SortableTree/components/TreeItem/TreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Props extends Omit<HTMLAttributes<HTMLLIElement>, 'id'> {
onAdd?(): void;
onLabelClick?(): void;
wrapperRef?(node: HTMLLIElement): void;
renderedItem?: React.ReactNode;
}

export const _TreeItem = forwardRef<HTMLDivElement, Props>(
Expand All @@ -47,6 +48,7 @@ export const _TreeItem = forwardRef<HTMLDivElement, Props>(
style,
value,
wrapperRef,
renderedItem,
...props
},
ref,
Expand Down Expand Up @@ -82,6 +84,7 @@ export const _TreeItem = forwardRef<HTMLDivElement, Props>(
<span onClick={onLabelClick} className={styles.Text}>
{value}
</span>
{!clone && renderedItem && renderedItem}
{!clone && onRemove && <Remove onClick={onRemove} />}
{!clone && onAdd && <Add onClick={onAdd} />}
{clone && childCount && childCount > 1 ?
Expand Down
8 changes: 8 additions & 0 deletions src/SortableTree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export interface SortableTreeProps {
* @param id - The id of the clicked item.
*/
onItemClick?: (id: UniqueIdentifier) => void;

/**
* You can place a react component next to the item's label. This is temporal while we
* figure out a way of rendering a whole custom item
* @param item
* @returns
*/
renderItem?: (item: TreeItem) => React.ReactNode;
}

/**
Expand Down