diff --git a/src/components/TreeSelect/__stories__/components/WithDndListExample.tsx b/src/components/TreeSelect/__stories__/components/WithDndListExample.tsx index 51da701515..c9c6253581 100644 --- a/src/components/TreeSelect/__stories__/components/WithDndListExample.tsx +++ b/src/components/TreeSelect/__stories__/components/WithDndListExample.tsx @@ -18,7 +18,7 @@ import {reorderArray} from '../../../useList/__stories__/utils/reorderArray'; import {TreeSelect} from '../../TreeSelect'; import {TreeSelectItem} from '../../TreeSelectItem'; import type {TreeSelectItemProps} from '../../TreeSelectItem'; -import type {TreeSelectProps} from '../../types'; +import type {TreeSelectProps, TreeSelectRenderContainer, TreeSelectRenderItem} from '../../types'; const DraggableListItem = ({ provided, @@ -52,11 +52,87 @@ export const WithDndListExample = (storyProps: WithDndListExampleProps) => { const [items, setItems] = React.useState(randomItems); const [value, setValue] = React.useState([]); - const handleDrugEnd: OnDragEndResponder = ({destination, source}) => { - if (typeof destination?.index === 'number' && destination.index !== source.index) { - setItems((items) => reorderArray(items, source.index, destination.index)); - } - }; + const renderContainer: TreeSelectRenderContainer = React.useCallback( + ({renderItem, visibleFlattenIds, containerRef, id}) => { + const handleDrugEnd: OnDragEndResponder = ({destination, source}) => { + if (typeof destination?.index === 'number' && destination.index !== source.index) { + setItems((currentItems) => + reorderArray(currentItems, source.index, destination.index), + ); + } + }; + + return ( + + { + return renderItem( + visibleFlattenIds[rubric.source.index], + rubric.source.index, + { + provided, + active: snapshot.isDragging, + }, + ); + }} + > + {(droppableProvided: DroppableProvided) => ( + +
+ {visibleFlattenIds.map((listItemId, index) => + renderItem(listItemId, index), + )} + {droppableProvided.placeholder} +
+
+ )} +
+
+ ); + }, + [setItems], + ); + + const renderItem: TreeSelectRenderItem = React.useCallback( + ({data, props, index, renderContext: renderContextProps}) => { + const commonProps = { + ...props, + title: data.someRandomKey, + endSlot: , + }; + + // here passed props from `renderContainer` method. + if (renderContextProps) { + return ( + + ); + } + return ( + + {(provided: DraggableProvided, snapshot: DraggableStateSnapshot) => ( + + )} + + ); + }, + [], + ); return ( @@ -74,76 +150,8 @@ export const WithDndListExample = (storyProps: WithDndListExampleProps) => { setValue([id]); } }} - renderContainer={({renderItem, visibleFlattenIds, containerRef, id}) => { - return ( - - { - return renderItem( - visibleFlattenIds[rubric.source.index], - rubric.source.index, - { - provided, - active: snapshot.isDragging, - }, - ); - }} - > - {(droppableProvided: DroppableProvided) => ( - -
- {visibleFlattenIds.map((id, index) => - renderItem(id, index), - )} - {droppableProvided.placeholder} -
-
- )} -
-
- ); - }} - renderItem={({data, props, index, renderContext: renderContextProps}) => { - const commonProps = { - ...props, - title: data.someRandomKey, - endSlot: , - }; - - // here passed props from `renderContainer` method. - if (renderContextProps) { - return ( - - ); - } - return ( - - {(provided: DraggableProvided, snapshot: DraggableStateSnapshot) => ( - - )} - - ); - }} + renderContainer={renderContainer} + renderItem={renderItem} />
);