Skip to content

Commit

Permalink
feat: added data to present modal api (#942)
Browse files Browse the repository at this point in the history
* feat: added data api for modal present

* fix: updated rendering children approach
  • Loading branch information
gorhom committed May 14, 2022
1 parent ce15894 commit 8a3d138
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
backdropComponent,
backgroundComponent,
footerComponent,
children,
children: Content,
} = props;
//#endregion

Expand Down Expand Up @@ -1621,9 +1621,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
key="BottomSheetRootDraggableView"
style={contentContainerStyle}
>
{typeof children === 'function'
? (children as Function)()
: children}
{typeof Content === 'function' ? <Content /> : Content}

{footerComponent && (
<BottomSheetFooterContainer
Expand Down
27 changes: 20 additions & 7 deletions src/components/bottomSheetModal/BottomSheetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import type { BottomSheetModalProps } from './types';

type BottomSheetModal = BottomSheetModalMethods;

const INITIAL_STATE: {
mount: boolean;
data: any;
} = {
mount: false,
data: undefined,
};

const BottomSheetModalComponent = forwardRef<
BottomSheetModal,
BottomSheetModalProps
Expand All @@ -41,12 +49,12 @@ const BottomSheetModalComponent = forwardRef<
onChange: _providedOnChange,

// components
children,
children: Content,
...bottomSheetProps
} = props;

//#region state
const [mount, setMount] = useState(false);
const [{ mount, data }, setState] = useState(INITIAL_STATE);
//#endregion

//#region hooks
Expand Down Expand Up @@ -103,7 +111,7 @@ const BottomSheetModalComponent = forwardRef<

// unmount the node, if sheet is still mounted
if (_mounted) {
setMount(false);
setState(INITIAL_STATE);
}

// fire `onDismiss` callback
Expand Down Expand Up @@ -161,9 +169,12 @@ const BottomSheetModalComponent = forwardRef<

//#region bottom sheet modal methods
const handlePresent = useCallback(
function handlePresent() {
function handlePresent(_data?: any) {
requestAnimationFrame(() => {
setMount(true);
setState({
mount: true,
data: _data,
});
mountSheet(key, ref, stackBehavior);

print({
Expand Down Expand Up @@ -348,7 +359,7 @@ const BottomSheetModalComponent = forwardRef<
//#endregion

// render
// console.log('BottomSheetModal', index, snapPoints)
// console.log('BottomSheetModal', index, mount, data);
return mount ? (
<Portal
key={key}
Expand All @@ -368,7 +379,9 @@ const BottomSheetModalComponent = forwardRef<
containerOffset={containerOffset}
onChange={handleBottomSheetOnChange}
onClose={handleBottomSheetOnClose}
children={children}
children={
typeof Content === 'function' ? <Content data={data} /> : Content
}
$modal={true}
/>
</Portal>
Expand Down
10 changes: 10 additions & 0 deletions src/components/bottomSheetModal/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type React from 'react';
import type { BottomSheetProps } from '../bottomSheet';
import type { MODAL_STACK_BEHAVIOR } from '../../constants';

Expand Down Expand Up @@ -43,4 +44,13 @@ export interface BottomSheetModalProps
* @type () => void;
*/
onDismiss?: () => void;

/**
* A scrollable node or normal view.
* @type React.ReactNode[] | React.ReactNode
*/
children:
| (({ data: any }?) => React.ReactNode)
| React.ReactNode[]
| React.ReactNode;
}
3 changes: 2 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ export interface BottomSheetMethods {
export interface BottomSheetModalMethods extends BottomSheetMethods {
/**
* Mount and present the bottom sheet modal to the initial snap point.
* @param data to be passed to the modal.
*/
present: () => void;
present: (data?: any) => void;
/**
* Close and unmount the bottom sheet modal.
* @param animationConfigs snap animation configs.
Expand Down

0 comments on commit 8a3d138

Please sign in to comment.