Skip to content

Commit 879bcfd

Browse files
authored
add renderPlaceholder prop (#208)
* add renderPlaceholder * fix placeholder pos logic * readme typo * cleanup * cleanup * cleanup
1 parent ecdeaf1 commit 879bcfd

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ All props are spread onto underlying [FlatList](https://facebook.github.io/react
3636
| `data` | `T[]` | Items to be rendered. |
3737
| `horizontal` | `boolean` | Orientation of list. |
3838
| `renderItem` | `(params: { item: T, index: number, drag: () => void, isActive: boolean}) => JSX.Element` | Call `drag` when the row should become active (i.e. in an `onLongPress` or `onPressIn`). |
39+
| `renderPlaceholder` | `(params: { item: T, index: number }) => React.ReactNode` | Component to be rendered underneath the hovering component |
3940
| `keyExtractor` | `(item: T, index: number) => string` | Unique key for each item |
4041
| `onDragBegin` | `(index: number) => void` | Called when row becomes active. |
4142
| `onRelease` | `(index: number) => void` | Called when active row touch ends. |

src/index.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type Props<T> = Modify<
104104
onRelease?: (index: number) => void;
105105
onDragEnd?: (params: DragEndParams<T>) => void;
106106
renderItem: (params: RenderItemParams<T>) => React.ReactNode;
107+
renderPlaceholder?: (params: { item: T; index: number }) => React.ReactNode;
107108
animationConfig: Partial<Animated.SpringConfig>;
108109
activationDistance?: number;
109110
debug?: boolean;
@@ -180,6 +181,9 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
180181
hoverMid = add(this.hoverAnim, divide(this.activeCellSize, 2));
181182
hoverOffset = add(this.hoverAnim, this.scrollOffset);
182183

184+
placeholderOffset = new Value(0);
185+
placeholderPos = sub(this.placeholderOffset, this.scrollOffset);
186+
183187
hoverTo = new Value(0);
184188
hoverClock = new Clock();
185189
hoverAnimState = {
@@ -411,7 +415,6 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
411415
);
412416
const onHasMoved = startClock(clock);
413417
const onChangeSpacerIndex = cond(clockRunning(clock), stopClock(clock));
414-
415418
const onFinished = stopClock(clock);
416419

417420
const prevTrans = new Value(0);
@@ -442,7 +445,8 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
442445
onHasMoved,
443446
onChangeSpacerIndex,
444447
onFinished,
445-
this.isPressedIn.native
448+
this.isPressedIn.native,
449+
this.placeholderOffset
446450
);
447451

448452
const transform = this.props.horizontal
@@ -854,6 +858,30 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
854858
);
855859
};
856860

861+
renderPlaceholder = () => {
862+
const { renderPlaceholder, horizontal } = this.props;
863+
const { activeKey } = this.state;
864+
if (!activeKey || !renderPlaceholder) return null;
865+
const activeIndex = this.keyToIndex.get(activeKey);
866+
if (activeIndex === undefined) return null;
867+
const activeItem = this.props.data[activeIndex];
868+
const translateKey = horizontal ? "translateX" : "translateY";
869+
const sizeKey = horizontal ? "width" : "height";
870+
const style = {
871+
...StyleSheet.absoluteFillObject,
872+
[sizeKey]: this.activeCellSize,
873+
transform: [
874+
{ [translateKey]: this.placeholderPos }
875+
] as Animated.AnimatedTransform
876+
};
877+
878+
return (
879+
<Animated.View style={style}>
880+
{renderPlaceholder({ item: activeItem, index: activeIndex })}
881+
</Animated.View>
882+
);
883+
};
884+
857885
CellRendererComponent = (cellProps: any) => {
858886
const { item, index, children, onLayout } = cellProps;
859887
const { horizontal } = this.props;
@@ -911,8 +939,10 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
911939
debug,
912940
horizontal,
913941
activationDistance,
914-
onScrollOffsetChange
942+
onScrollOffsetChange,
943+
renderPlaceholder
915944
} = this.props;
945+
916946
const { hoverComponent } = this.state;
917947
let dynamicProps = {};
918948
if (activationDistance) {
@@ -934,6 +964,7 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
934964
onLayout={this.onContainerLayout}
935965
onTouchEnd={this.onContainerTouchEnd}
936966
>
967+
{!!renderPlaceholder && this.renderPlaceholder()}
937968
<AnimatedFlatList
938969
{...this.props}
939970
CellRendererComponent={this.CellRendererComponent}

src/procs.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export const setupCell = (proc as RetypedProc)(
100100
onHasMoved: Animated.Node<number>,
101101
onChangeSpacerIndex: Animated.Node<number>,
102102
onFinished: Animated.Node<number>,
103-
isPressedIn: Animated.Node<number>
103+
isPressedIn: Animated.Node<number>,
104+
placeholderOffset: Animated.Value<number>
104105
) =>
105106
block([
106107
set(isAfterActive, getIsAfterActive(currentIndex, activeIndex)),
@@ -209,6 +210,13 @@ export const setupCell = (proc as RetypedProc)(
209210
]),
210211
runSpring,
211212
cond(finished, [onFinished, set(time, 0), set(finished, 0)]),
213+
cond(
214+
eq(spacerIndex, currentIndex),
215+
set(
216+
placeholderOffset,
217+
cond(isAfterActive, add(sub(offset, activeCellSize), size), offset)
218+
)
219+
),
212220
position
213221
])
214222
);

0 commit comments

Comments
 (0)