Skip to content

Commit 65f3945

Browse files
chore: add render item prop (#12)
1 parent a7ca1dc commit 65f3945

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@clevertask/react-sortable-tree",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"type": "module",
55
"license": "MIT",
66
"author": "CleverTask",

src/SortableTree/SortableTree.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function PrivateSortableTree({
8484
onAddItem,
8585
onDragEnd,
8686
onItemClick,
87+
renderItem,
8788
}: SortableTreeProps) {
8889
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
8990
const [overId, setOverId] = useState<UniqueIdentifier | null>(null);
@@ -339,7 +340,16 @@ function PrivateSortableTree({
339340
>
340341
<SortableContext items={sortedIds} strategy={verticalListSortingStrategy}>
341342
{flattenedItems.map(
342-
({ id, label, children, collapsed, depth, canFetchChildren, disableDragging }) => (
343+
({
344+
id,
345+
label,
346+
children,
347+
collapsed,
348+
depth,
349+
canFetchChildren,
350+
disableDragging,
351+
...rest
352+
}) => (
343353
<SortableTreeItem
344354
key={id}
345355
id={id}
@@ -359,6 +369,7 @@ function PrivateSortableTree({
359369
}
360370
onAdd={allowNestedItemAddition ? () => onAddItem?.(id) : undefined}
361371
onLabelClick={onItemClick ? () => onItemClick(id) : undefined}
372+
renderedItem={renderItem?.({ id, label, children, ...rest })}
362373
/>
363374
),
364375
)}

src/SortableTree/components/TreeItem/TreeItem.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Props extends Omit<HTMLAttributes<HTMLLIElement>, 'id'> {
2424
onAdd?(): void;
2525
onLabelClick?(): void;
2626
wrapperRef?(node: HTMLLIElement): void;
27+
renderedItem?: React.ReactNode;
2728
}
2829

2930
export const _TreeItem = forwardRef<HTMLDivElement, Props>(
@@ -47,6 +48,7 @@ export const _TreeItem = forwardRef<HTMLDivElement, Props>(
4748
style,
4849
value,
4950
wrapperRef,
51+
renderedItem,
5052
...props
5153
},
5254
ref,
@@ -82,6 +84,7 @@ export const _TreeItem = forwardRef<HTMLDivElement, Props>(
8284
<span onClick={onLabelClick} className={styles.Text}>
8385
{value}
8486
</span>
87+
{!clone && renderedItem && renderedItem}
8588
{!clone && onRemove && <Remove onClick={onRemove} />}
8689
{!clone && onAdd && <Add onClick={onAdd} />}
8790
{clone && childCount && childCount > 1 ?

src/SortableTree/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ export interface SortableTreeProps {
135135
* @param id - The id of the clicked item.
136136
*/
137137
onItemClick?: (id: UniqueIdentifier) => void;
138+
139+
/**
140+
* You can place a react component next to the item's label. This is temporal while we
141+
* figure out a way of rendering a whole custom item
142+
* @param item
143+
* @returns
144+
*/
145+
renderItem?: (item: TreeItem) => React.ReactNode;
138146
}
139147

140148
/**

0 commit comments

Comments
 (0)