forked from Shopify/restyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateRestyleComponent.tsx
30 lines (26 loc) · 1021 Bytes
/
createRestyleComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import React from 'react';
import {View} from 'react-native';
import composeRestyleFunctions from './composeRestyleFunctions';
import {BaseTheme, RestyleFunctionContainer} from './types';
import useRestyle from './hooks/useRestyle';
const createRestyleComponent = <
Props extends {[key: string]: any},
Theme extends BaseTheme,
>(
restyleFunctions: (
| RestyleFunctionContainer<Props, Theme>
| RestyleFunctionContainer<Props, Theme>[]
)[],
BaseComponent: React.ComponentType<any> = View,
) => {
const composedRestyleFunction = composeRestyleFunctions(restyleFunctions);
const RestyleComponent = React.forwardRef((props: Props, ref) => {
const passedProps = useRestyle(composedRestyleFunction, props);
return <BaseComponent ref={ref} {...passedProps} />;
});
type RestyleComponentType = typeof RestyleComponent;
return RestyleComponent as RestyleComponentType & {
defaultProps?: Partial<React.ComponentProps<RestyleComponentType>>;
};
};
export default createRestyleComponent;