Skip to content

Commit

Permalink
feat: add scrollToIndex for FlatList #125
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Apr 10, 2020
1 parent cf0be59 commit 19a3b41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/PROPSMETHODS.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,13 @@ The method is used to programmatically scroll the modal content.
| Type | Required |
| ----------------------------------------------------- | -------- |
| function: (options: { y: number, animated: boolean }) | No |

### `scrollToIndex()`

The method is used to programmatically scroll to the index of the FlatList.

?> This method only works along with `flatListProps`.

| Type | Required |
| --------------------------------------------------------------------------------------------------- | -------- |
| function: (options: { index: number, viewOffset: number, viewPosition: number, animated: boolean }) | No |
22 changes: 19 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C

if (props.modalHeight && props.adjustToContentHeight) {
console.error(
`[react-native-modalize] You cannot use both 'modalHeight' and 'adjustToContentHeight' props at the same time. Only choose one of the two.`,
`[react-native-modalize] You can't use both 'modalHeight' and 'adjustToContentHeight' props at the same time. Only choose one of the two.`,
);
}

if ((props.scrollViewProps || props.children) && props.flatListProps) {
console.error(
`[react-native-modalize] 'flatListProps' You can\'t use the ScrollView and the FlatList at the 'same time. As soon as you use 'flatListProps' it will replaces the default ScrollView with 'a FlatList component. Remove the 'children' and/or 'scrollViewProps' to fix the error.`,
`[react-native-modalize] You have defined 'flatListProps' along with 'scrollViewProps' or 'children' props. Remove 'scrollViewProps' or 'children' or 'flatListProps' to fix the error.`,
);
}

if ((props.scrollViewProps || props.children) && props.sectionListProps) {
console.error(
`[react-native-modalize] 'sectionListProps' You can\'t use the ScrollView and the SectionList at the 'same time. As soon as you use 'sectionListProps' it will replaces the default ScrollView with 'a SectionList component. Remove the 'children' and/or 'scrollViewProps' to fix the error.`,
`[react-native-modalize] You have defined 'sectionListProps' along with 'scrollViewProps' or 'children' props. Remove 'scrollViewProps' or 'children' or 'sectionListProps' to fix the error.`,
);
}

Expand Down Expand Up @@ -206,6 +206,22 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
}
};

public scrollToIndex = (...args: Parameters<FlatList['scrollToIndex']>): void => {
const { flatListProps } = this.props;

if (!flatListProps) {
return console.error(
`[react-native-modalize] You can't use the 'scrollToIndex' method with something else than the FlatList component.`,
);
}

if (this.contentView.current) {
const ref = this.contentView.current as any;

ref.getNode().scrollToIndex(...args);
}
};

private get isHandleOutside(): boolean {
const { handlePosition } = this.props;

Expand Down

0 comments on commit 19a3b41

Please sign in to comment.