import React, { ComponentType, forwardRef, Ref, useContext, ReactElement, PropsWithoutRef, RefAttributes, ForwardRefExoticComponent, PropsWithChildren } from 'react'; import { StyleSheet, StyleProp } from 'react-native'; import hoistNonReactStatics from 'hoist-non-react-statics'; import ThemedValue from './ThemedValue'; import ThemeContext from './ThemeContext'; export type ThemeStyle = { [K in keyof S]: S[K] extends ReadonlyArray ? ReadonlyArray> : S[K] extends object ? ThemeStyle : | S[K] | ThemedValue< { [name: string]: S[K]; }, S[K] >; }; export type ThemeProps

= { [K in keyof P]: P[K] | ThemedValue<{ [name: string]: P[K] }, P[K]>; }; export function transformValue( value: T | ThemedValue<{ [name: string]: T }, T>, mode: string ): T { if (value instanceof ThemedValue) { return value.selectValue(mode); } else { return value; } } export function transformStyle( style: StyleProp>, mode: string ): StyleProp { if (!style) { return style as StyleProp; } const flattenStyle = StyleSheet.flatten>>(style); const themedStyle = Object.create(null); let hasThemedStyle: boolean = false; for (const key of Object.keys(flattenStyle as object) as Array) { // @ts-ignore const styleValue = flattenStyle[key]; themedStyle[key] = transformValue(styleValue, mode); if (!hasThemedStyle && themedStyle[key] !== styleValue) { hasThemedStyle = true; } } if (!hasThemedStyle) { return style as StyleProp; } else { return themedStyle as StyleProp; } } function createThemedComponent< P extends object, // original props S extends Partial

// themed style props >( Component: ComponentType>, styles: Array ): ForwardRefExoticComponent< PropsWithChildren< PropsWithoutRef< Omit & { [K in keyof S]?: StyleProp>; } > & RefAttributes< ComponentType< Omit & { [K in keyof S]?: StyleProp>; } > > > >; function createThemedComponent< P extends object, // original props S extends Partial

, // themed style props T extends Partial

// themed common props >( Component: ComponentType>, styles: Array, themed: Array ): ForwardRefExoticComponent< PropsWithChildren< PropsWithoutRef< Omit & ThemeProps & { [K in keyof S]?: StyleProp>; } > & RefAttributes< ComponentType< Omit & ThemeProps & { [K in keyof S]?: StyleProp>; } > > > >; function createThemedComponent< P extends object, // original props S extends Partial

, // themed style props T extends Partial

// themed common props >( Component: ComponentType>, styles: Array, themed?: Array ): ForwardRefExoticComponent< PropsWithChildren< PropsWithoutRef< Omit & ThemeProps & { [K in keyof S]?: StyleProp>; } > & RefAttributes< ComponentType< Omit & ThemeProps & { [K in keyof S]?: StyleProp>; } > > > > { function ThemedComponent( props: Omit & ThemeProps & { [K in keyof S]?: StyleProp>; }, ref: Ref< ComponentType< Omit & ThemeProps & { [K in keyof S]?: StyleProp>; } > > ): ReactElement

{ const theme = useContext(ThemeContext); const themedProps: any = { ...props }; styles.forEach((key: keyof S) => { themedProps[key] = transformStyle(props[key] as any, theme); }); if (themed) { themed.forEach((key: keyof T) => { themedProps[key] = transformValue(props[key] as any, theme); }); } return ; } return forwardRef(hoistNonReactStatics(ThemedComponent, Component)); } export default createThemedComponent;