11/* eslint-disable no-continue, no-loop-func, no-cond-assign */
22import type { ElementType } from 'react'
33import type { ReactNativeThemedStyledFunction } from 'styled-components/native'
4+ import type {
5+ StyleGenerator ,
6+ StyleGeneratorProps ,
7+ Theme ,
8+ } from '@xstyled/system'
9+ import type { StyledConfig , ThemedStyledFunction } from 'styled-components'
10+ import type {
11+ ReactNativeBoxElements ,
12+ ReactNativeElement ,
13+ ReactNativeElements ,
14+ } from './types'
15+ import * as ReactNative from 'react-native'
416import { string } from '@xstyled/util'
5- import { StyleGenerator , Theme } from '@xstyled/system'
6- import { StyledConfig } from 'styled-components'
7- import * as RN from 'react-native'
817import { scStyled } from './scStyled'
918import { createCssFunction , XCSSFunction } from './createCssFunction'
10- import type { ReactNativeThemedBaseStyledInterface } from './types'
1119
1220const getCreateStyle = (
1321 baseCreateStyle : ReactNativeThemedStyledFunction < any , any > ,
@@ -25,7 +33,17 @@ const getCreateStyle = (
2533 return createStyle
2634}
2735
28- export type XStyled = ReactNativeThemedBaseStyledInterface < Theme >
36+ type BoxStyledTags < TProps extends object > = {
37+ [ Key in keyof ReactNativeBoxElements ] : ThemedStyledFunction <
38+ ReactNativeElement < ReactNativeBoxElements [ Key ] , Theme > ,
39+ Theme ,
40+ TProps
41+ >
42+ }
43+
44+ export interface XStyled < TGen extends StyleGenerator >
45+ extends ReactNativeElements ,
46+ BoxStyledTags < StyleGeneratorProps < TGen > > { }
2947
3048const createShouldForwardProp = (
3149 generator : StyleGenerator ,
@@ -36,31 +54,19 @@ const createShouldForwardProp = (
3654) => boolean ) => {
3755 const propSet = new Set < string > ( generator . meta . props )
3856
39- return (
40- prop : string | number | symbol ,
41- defaultValidatorFn : ( prop : string | number | symbol ) => boolean ,
42- elementToBeCreated ?: ElementType ,
43- ) => {
57+ return ( prop : string | number | symbol ) => {
4458 if ( string ( prop ) && propSet . has ( prop ) ) {
4559 return false
4660 }
47- if ( typeof elementToBeCreated === 'string' ) {
48- // We must test elementToBeCreated so we can pass through props for <x.div
49- // as={Component} />. However elementToBeCreated isn't available until
50- // styled-components 5.2.4 or 6, and in the meantime will be undefined.
51- // This means that HTML elements could get unwanted props, but ultimately
52- // this is a bug in the caller, because why are they passing unwanted
53- // props?
54- return defaultValidatorFn ( prop )
55- }
61+
5662 return true
5763 }
5864}
5965
6066export const createBaseStyled = < TGen extends StyleGenerator > (
6167 css : XCSSFunction ,
6268 generator ?: TGen ,
63- ) : XStyled => {
69+ ) : XStyled < TGen > => {
6470 const config = generator
6571 ? {
6672 shouldForwardProp : createShouldForwardProp ( generator ) ,
@@ -74,26 +80,35 @@ export const createBaseStyled = <TGen extends StyleGenerator>(
7480 css ,
7581 generator ,
7682 )
77- } ) as XStyled
83+ } ) as XStyled < TGen >
7884}
7985
8086export const createStyled = < TGen extends StyleGenerator > (
8187 generator : TGen ,
82- ) : XStyled => {
88+ ) : XStyled < TGen > => {
8389 const css = createCssFunction ( generator )
84- // const styled = createBaseStyled(css)
90+ const styled = createBaseStyled ( css )
8591 const xstyled = createBaseStyled ( css , generator )
8692
8793 Object . keys ( scStyled ) . forEach ( ( tag ) => {
88- Object . defineProperty ( xstyled , tag , {
94+ Object . defineProperty ( styled , tag , {
95+ enumerable : true ,
96+ configurable : false ,
97+ get ( ) {
98+ // @ts -ignore
99+ return styled ( ReactNative [ tag ] )
100+ } ,
101+ } )
102+
103+ Object . defineProperty ( styled , `${ tag } Box` , {
89104 enumerable : true ,
90105 configurable : false ,
91106 get ( ) {
92107 // @ts -ignore
93- return xstyled ( RN [ tag ] )
108+ return xstyled ( ReactNative [ tag ] )
94109 } ,
95110 } )
96111 } )
97112
98- return xstyled
113+ return styled
99114}
0 commit comments