Skip to content

Commit

Permalink
fix: added error message when dynamic sizing enabled with a wrong chi…
Browse files Browse the repository at this point in the history
…ldren type
  • Loading branch information
gorhom committed Feb 25, 2024
1 parent 6efd8ae commit 8b62dca
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
//#endregion

//#region validate props
usePropsValidator({
index: _providedIndex,
snapPoints: _providedSnapPoints,
enableDynamicSizing,
topInset,
bottomInset,
});
if (__DEV__) {
// eslint-disable-next-line react-hooks/rules-of-hooks
usePropsValidator({
index: _providedIndex,
snapPoints: _providedSnapPoints,
enableDynamicSizing,
topInset,
bottomInset,
children,
});
}
//#endregion

//#region layout variables
Expand Down
2 changes: 2 additions & 0 deletions src/components/bottomSheetScrollable/BottomSheetFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const BottomSheetFlatListComponent = createBottomSheetScrollableComponent<

const BottomSheetFlatList = memo(BottomSheetFlatListComponent);
BottomSheetFlatList.displayName = 'BottomSheetFlatList';
//@ts-ignore
BottomSheetFlatList.$bottomSheetIntegrated = true;

export default BottomSheetFlatList as <T>(
props: BottomSheetFlatListProps<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const BottomSheetScrollViewComponent = createBottomSheetScrollableComponent<

const BottomSheetScrollView = memo(BottomSheetScrollViewComponent);
BottomSheetScrollView.displayName = 'BottomSheetScrollView';
//@ts-ignore
BottomSheetScrollView.$bottomSheetIntegrated = true;

export default BottomSheetScrollView as (
props: BottomSheetScrollViewProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const BottomSheetSectionListComponent = createBottomSheetScrollableComponent<

const BottomSheetSectionList = memo(BottomSheetSectionListComponent);
BottomSheetSectionList.displayName = 'BottomSheetSectionList';
//@ts-ignore
BottomSheetSectionList.$bottomSheetIntegrated = true;

export default BottomSheetSectionList as <ItemT, SectionT = DefaultSectionT>(
props: BottomSheetSectionListProps<ItemT, SectionT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const BottomSheetVirtualizedListComponent =

const BottomSheetVirtualizedList = memo(BottomSheetVirtualizedListComponent);
BottomSheetVirtualizedList.displayName = 'BottomSheetVirtualizedList';
//@ts-ignore
BottomSheetVirtualizedList.$bottomSheetIntegrated = true;

export default BottomSheetVirtualizedList as <T>(
props: BottomSheetVirtualizedListProps<T>
Expand Down
2 changes: 2 additions & 0 deletions src/components/bottomSheetView/BottomSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ function BottomSheetViewComponent({

const BottomSheetView = memo(BottomSheetViewComponent);
BottomSheetView.displayName = 'BottomSheetView';
//@ts-ignore
BottomSheetView.$bottomSheetIntegrated = true;

export default BottomSheetView;
7 changes: 5 additions & 2 deletions src/hooks/useNormalizedSnapPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ export const useNormalizedSnapPoints = (
: containerHeight.value
);

// push dynamic snap point into the normalized snap points.
_normalizedSnapPoints.push(dynamicSnapPoint);
// push dynamic snap point into the normalized snap points,
// only if it does not exists in the provided list already.
if (!_normalizedSnapPoints.includes(dynamicSnapPoint)) {
_normalizedSnapPoints.push(dynamicSnapPoint);
}

// sort all snap points.
_normalizedSnapPoints = _normalizedSnapPoints.sort((a, b) => b - a);
Expand Down
22 changes: 21 additions & 1 deletion src/hooks/usePropsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ export const usePropsValidator = ({
enableDynamicSizing,
topInset,
bottomInset,
children,
}: Pick<
BottomSheetProps,
'index' | 'snapPoints' | 'enableDynamicSizing' | 'topInset' | 'bottomInset'
| 'index'
| 'snapPoints'
| 'enableDynamicSizing'
| 'topInset'
| 'bottomInset'
| 'children'
>) => {
useMemo(() => {
//#region snap points
Expand Down Expand Up @@ -78,4 +84,18 @@ export const usePropsValidator = ({

// animations
}, [index, snapPoints, topInset, bottomInset, enableDynamicSizing]);

useMemo(() => {
invariant(
(enableDynamicSizing &&
children &&
// @ts-ignore
children.type &&
// @ts-ignore
children.type.$bottomSheetIntegrated) ||
!enableDynamicSizing,
`'enableDynamicSizing' is enabled but children type is not integrated with the library !` +
` expected children types are\n- BottomSheetView\n- BottomSheetFlatList\n- BottomSheetScrollView\n- BottomSheetSectionList\n- BottomSheetVirtualizedList`
);
}, [enableDynamicSizing, children]);
};

0 comments on commit 8b62dca

Please sign in to comment.