Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generic type for data modal #2029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/components/bottomSheetModal/BottomSheetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import {
import type {
BottomSheetModalPrivateMethods,
BottomSheetModalProps,
BottomSheetModalState,
} from './types';

type BottomSheetModal = BottomSheetModalMethods;

const INITIAL_STATE: {
mount: boolean;
data?: never;
} = {
const INITIAL_STATE: BottomSheetModalState = {
mount: false,
data: undefined,
};

const BottomSheetModalComponent = forwardRef<
BottomSheetModal,
BottomSheetModalProps
>(function BottomSheetModal(props, ref) {
// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
type BottomSheetModal<T = any> = BottomSheetModalMethods<T>;

// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
function BottomSheetModalComponent<T = any>(
props: BottomSheetModalProps<T>,
ref: React.ForwardedRef<BottomSheetModal<T>>
) {
const {
// modal props
name,
Expand All @@ -62,7 +62,8 @@ const BottomSheetModalComponent = forwardRef<
} = props;

//#region state
const [{ mount, data }, setState] = useState(INITIAL_STATE);
const [{ mount, data }, setState] =
useState<BottomSheetModalState<T>>(INITIAL_STATE);
//#endregion

//#region hooks
Expand Down Expand Up @@ -190,7 +191,7 @@ const BottomSheetModalComponent = forwardRef<
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheetModal.name): used for debug only
// biome-ignore lint/correctness/useExhaustiveDependencies(ref): ref is a stable object
const handlePresent = useCallback(
function handlePresent(_data?: never) {
function handlePresent(_data?: T) {
requestAnimationFrame(() => {
setState({
mount: true,
Expand Down Expand Up @@ -461,9 +462,20 @@ const BottomSheetModalComponent = forwardRef<
</ContainerComponent>
</Portal>
) : null;
});

const BottomSheetModal = memo(BottomSheetModalComponent);
BottomSheetModal.displayName = 'BottomSheetModal';
}

const BottomSheetModal = memo(forwardRef(BottomSheetModalComponent)) as <
// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
T = any,
>(
props: BottomSheetModalProps<T> & {
ref?: React.ForwardedRef<BottomSheetModal<T>>;
}
) => ReturnType<typeof BottomSheetModalComponent>;
(
BottomSheetModal as React.MemoExoticComponent<
typeof BottomSheetModalComponent
>
).displayName = 'BottomSheetModal';

export default BottomSheetModal;
11 changes: 9 additions & 2 deletions src/components/bottomSheetModal/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export interface BottomSheetModalPrivateMethods {

export type BottomSheetModalStackBehavior = keyof typeof MODAL_STACK_BEHAVIOR;

export interface BottomSheetModalProps
// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
export interface BottomSheetModalProps<T = any>
extends Omit<BottomSheetProps, 'containerHeight' | 'onClose'> {
/**
* Modal name to help identify the modal for later on.
Expand Down Expand Up @@ -56,5 +57,11 @@ export interface BottomSheetModalProps
* A scrollable node or normal view.
* @type React.ReactNode[] | React.ReactNode | (({ data: any }?) => React.ReactElement)
*/
children: React.FC<{ data?: never }> | React.ReactNode[] | React.ReactNode;
children: React.FC<{ data?: T }> | React.ReactNode[] | React.ReactNode;
}

// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
export interface BottomSheetModalState<T = any> {
mount: boolean;
data: T | undefined;
}
6 changes: 4 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ export interface BottomSheetMethods {
*/
forceClose: (animationConfigs?: WithSpringConfig | WithTimingConfig) => void;
}
export interface BottomSheetModalMethods extends BottomSheetMethods {

// biome-ignore lint/suspicious/noExplicitAny: Using 'any' allows users to define their own strict types for 'data' property.
export interface BottomSheetModalMethods<T = any> extends BottomSheetMethods {
/**
* Mount and present the bottom sheet modal to the initial snap point.
* @param data to be passed to the modal.
*/
present: (data?: never) => void;
present: (data?: T) => void;
/**
* Close and unmount the bottom sheet modal.
* @param animationConfigs snap animation configs.
Expand Down