Skip to content

Commit 6727624

Browse files
authored
feat: enable log statements using the prop debug (#237)
Resolve #235
1 parent 3619926 commit 6727624

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ All props are spread onto underlying [FlatList](https://facebook.github.io/react
5050
| `onScrollOffsetChange` | `(offset: number) => void` | Called with scroll offset. Stand-in for `onScroll`. |
5151
| `onPlaceholderIndexChange` | `(index: number) => void` | Called when the index of the placeholder changes |
5252
| `dragItemOverflow` | `boolean` | If true, dragged item follows finger beyond list boundary. |
53+
| `debug` | `boolean` | A useful prop used to debug this component. It will log warnings and errors and also a component to debug its animation. |
5354

5455
## Example
5556

src/index.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ const defaultProps = {
7474

7575
type DefaultProps = Readonly<typeof defaultProps>;
7676

77-
const debugGestureState = (state: GestureState, context: string) => {
78-
const stateStr = Object.entries(GestureState).find(([k, v]) => v === state);
79-
console.log(`## ${context} debug gesture state: ${state} - ${stateStr}`);
80-
};
81-
8277
type AnimatedFlatListType<T> = { getNode: () => RNFlatList<T> };
8378

8479
export type DragEndParams<T> = {
@@ -338,7 +333,7 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
338333
if (this.state.hoverComponent) {
339334
// We can't drag more than one row at a time
340335
// TODO: Put action on queue?
341-
console.log("## Can't set multiple active items");
336+
if (this.props.debug) console.log("## Can't set multiple active items");
342337
} else {
343338
this.isPressedIn.js = true;
344339

@@ -523,7 +518,12 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
523518

524519
const size = horizontal ? w : h;
525520
const offset = baseOffset + extraOffset;
526-
// console.log(`measure key ${key}: wdith ${w} height ${h} x ${x} y ${y} size ${size} offset ${offset}`)
521+
522+
if (this.props.debug)
523+
console.log(
524+
`measure key ${key}: wdith ${w} height ${h} x ${x} y ${y} size ${size} offset ${offset}`
525+
);
526+
527527
if (cellData) {
528528
cellData.size.setValue(size);
529529
cellData.offset.setValue(offset);
@@ -537,7 +537,7 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
537537
};
538538

539539
const onFail = () => {
540-
console.log("## measureLayout fail!", key);
540+
if (this.props.debug) console.log("## measureLayout fail!", key);
541541
};
542542

543543
const ref = this.cellRefs.get(key);
@@ -554,7 +554,8 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
554554
: !flatListNode
555555
? "no flatlist node"
556556
: "invalid ref";
557-
console.log(`## can't measure ${key} reason: ${reason}`);
557+
if (this.props.debug)
558+
console.log(`## can't measure ${key} reason: ${reason}`);
558559
this.queue.push(() => this.measureCell(key));
559560
return resolve();
560561
}
@@ -856,7 +857,9 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
856857
const { renderItem } = this.props;
857858
if (!this.cellData.get(key)) this.setCellData(key, index);
858859
const { onUnmount } = this.cellData.get(key) || {
859-
onUnmount: () => console.log("## error, no cellData")
860+
onUnmount: () => {
861+
if (this.props.debug) console.log("## error, no cellData");
862+
}
860863
};
861864
return (
862865
<RowItem
@@ -1061,16 +1064,20 @@ type RowItemProps<T> = {
10611064
renderItem: (params: RenderItemParams<T>) => React.ReactNode;
10621065
itemKey: string;
10631066
onUnmount: () => void;
1067+
debug?: boolean;
10641068
};
10651069

10661070
class RowItem<T> extends React.PureComponent<RowItemProps<T>> {
10671071
drag = () => {
1068-
const { drag, renderItem, item, keyToIndex, itemKey } = this.props;
1072+
const { drag, renderItem, item, keyToIndex, itemKey, debug } = this.props;
10691073
const hoverComponent = renderItem({
10701074
isActive: true,
10711075
item,
10721076
index: keyToIndex.get(itemKey),
1073-
drag: () => console.log("## attempt to call drag() on hovering component")
1077+
drag: () => {
1078+
if (debug)
1079+
console.log("## attempt to call drag() on hovering component");
1080+
}
10741081
});
10751082
drag(hoverComponent, itemKey);
10761083
};

0 commit comments

Comments
 (0)