Skip to content

Commit 7695dcb

Browse files
committed
feat: new way to use typescript via generics
1 parent 1a4eac1 commit 7695dcb

File tree

14 files changed

+69
-58
lines changed

14 files changed

+69
-58
lines changed

example/global.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

example/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, { useCallback, useState } from 'react';
2-
import type { AppTheme } from 'react-native-unicorn-modals';
32
import { createModalProvider, Alert, Menu, defaultThemeDark, defaultThemeLight } from 'react-native-unicorn-modals';
43

54
import CustomModal from './components/CustomModal';
65
import Example from './components/Example';
76

7+
import type { AppTheme } from './types';
8+
89
const lightTheme: AppTheme = {
910
...defaultThemeLight,
1011
buttonColor: '#284387',

example/src/components/CustomModal.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import { Button, Text, View } from 'react-native';
3-
import type { CustomModalData, RenderableComponent } from 'react-native-unicorn-modals';
4-
import { useTheme } from 'react-native-unicorn-modals';
3+
import type { RenderableComponent } from 'react-native-unicorn-modals';
54

65
import styles from './CustomModal.styles';
6+
import type { CustomModalData } from '../types';
7+
import useAppTheme from '../hooks/useAppTheme';
78

89
const CustomModal: RenderableComponent<CustomModalData> = ({ WrapperComponent }) => {
9-
const theme = useTheme();
10+
const theme = useAppTheme();
1011
return (
1112
<WrapperComponent style={styles.wrapper}>
1213
{({ title }, actionCallback) => (

example/src/components/Example.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from 'react';
22
import { SafeAreaView, ScrollView, StatusBar, Text, TouchableOpacity, View } from 'react-native';
3-
import { useModals } from 'react-native-unicorn-modals';
43

54
import Tile from './Tile';
65
import styles from './Example.styles';
6+
import useAppModals from '../hooks/useAppModals';
77

88
interface ExampleProps {
99
darkMode: boolean;
1010
switchTheme: () => void;
1111
}
1212
const Example: React.FC<ExampleProps> = ({ darkMode, switchTheme }) => {
13-
const modal = useModals();
13+
const modal = useAppModals();
1414

1515
function createModalHandler(cancelable: boolean = true) {
1616
return () => {

example/src/hooks/useAppModals.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useModals } from 'react-native-unicorn-modals';
2+
3+
import type { RegisteredComponents } from '../types';
4+
5+
/**
6+
* Typed wrapper around `useModals`.
7+
*/
8+
export default function useAppModals() {
9+
return useModals<RegisteredComponents>();
10+
}

example/src/hooks/useAppTheme.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useTheme } from 'react-native-unicorn-modals';
2+
3+
import type { AppTheme } from '../types';
4+
5+
/**
6+
* Typed wrapper around `useTheme`.
7+
*/
8+
export default function useAppTheme() {
9+
return useTheme<AppTheme>();
10+
}

example/src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { AlertData, MenuData, Theme } from 'react-native-unicorn-modals';
2+
3+
// Components
4+
export interface CustomModalData {
5+
title: string;
6+
}
7+
8+
export interface RegisteredComponents {
9+
alert: AlertData;
10+
custom: CustomModalData;
11+
menu: MenuData;
12+
}
13+
14+
// Theme
15+
interface AdditionalThemeProps {
16+
buttonColor: string;
17+
}
18+
export type AppTheme = Theme<AdditionalThemeProps>;

src/components/Renderer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import WrapperComponent from './WrapperComponent';
55

66
import { hideAnimationStart } from '../state/action-creators';
77

8-
import type { Dispatch, ReducerState } from '../types';
8+
import type { ComponentName, Dispatch, ReducerState } from '../types';
99

1010
interface RendererProps {
11-
// TODO: Remove any
11+
// TODO: Fix any
1212
components: {
13-
[k: string]: any;
13+
[k: ComponentName]: React.FC<{ WrapperComponent: React.FC<any> }>;
1414
};
1515
dispatch: Dispatch;
1616
state: ReducerState;

src/components/WrapperComponent.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import { useAlertAnimatedStyle } from '../hooks/useAlertAnimatedStyle';
88
import { useModalsContext } from '../hooks/useModalsContext';
99
import { hideAnimationFinished, hideAnimationStart } from '../state/action-creators';
1010
import AvoidDismiss from './AvoidDismiss';
11-
import type { ComponentName, CreateActionCallback, RegisteredComponents } from '../types';
11+
import type { CreateActionCallback } from '../types';
1212

1313
export interface WrapperComponentProps<E extends unknown = unknown> {
14-
children: (
15-
data: RegisteredComponents[ComponentName] | any,
16-
createActionCallback: CreateActionCallback<E>,
17-
) => ReactNode;
14+
children: (data: unknown, createActionCallback: CreateActionCallback<E>) => ReactNode;
1815
style?: ViewStyle;
1916
}
2017

src/hooks/useModals.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import { useCallback } from 'react';
22

33
import { showModal } from '../state/action-creators';
44
import { useModalsContext } from './useModalsContext';
5-
import type { ComponentName, RegisteredComponents, ShowRenderItemOptions, UseModalsReturn } from '../types';
5+
import type { UseModalsReturn, UseModalsShow } from '../types';
66

7-
export function useModals(): UseModalsReturn {
7+
export function useModals<RC extends Object>(): UseModalsReturn<RC> {
88
const { dispatch } = useModalsContext();
99

10-
const show = useCallback(
11-
<K extends ComponentName>(type: K, data: RegisteredComponents[K], options?: ShowRenderItemOptions) => {
12-
dispatch(showModal(type, data, options));
13-
},
10+
const show = useCallback<UseModalsShow<RC>>(
11+
(type, data, options) => dispatch(showModal(type, data, options)),
1412
[dispatch],
1513
);
1614

0 commit comments

Comments
 (0)