Shopify / restyle

A type-enforced system for building UI components in React Native with TypeScript.
https://shopify.github.io/restyle/
MIT License
2.96k stars 133 forks source link

Issues with `createText` typing #153

Open nelyousfi opened 2 years ago

nelyousfi commented 2 years ago

What:

const CustomText = ({ title }: { title: string }) => <Text>{title}</Text>;

const RestyledCustomText = createText<Theme>(CustomText);

<RestyledCustomText color="lightpink" accessibilityLabel="I am a label" />;
const CustomText = ({ title }: { title: string }) => <Text>{title}</Text>;
const RestyledCustomText = createText<Theme, {}>(CustomText);

I believe the same applied for createBox.

sbalay commented 2 years ago

I haven't checked your PR yet, but createText is aimed to be a shorthand of creating a restyle component based on react-native's Text, hence, they are coupled by design.

If you would like to create a restyle version of a custom component you should use createRestyleComponent instead. i.e.:

import {TextProps as RNTextProps} from 'react-native'
import {
  createRestyleComponent,
  textRestyleFunctions,
  TextProps as RestyleTextProps,
} from '@shopify/restyle'
import {Theme} from './theme'
import {CustomText} from './CustomText'

type CustomTextProps = RestyleTextProps<Theme> &
  Omit<
    RNTextProps,
    | 'fontWeight'
    | 'fontFamily'
    | 'fontSize'
    | 'lineHeight'
    | 'letterSpacing'
    | 'style'
  >

const CustomRestyleText = createRestyleComponent<CustomTextProps, Theme>(
  textRestyleFunctions,
  CustomText
)

export const Text = (props: CustomTextProps) => {
  return (
    <CustomRestyleText
      {...props}
    />
  )
}
nelyousfi commented 2 years ago

But then why we can pass an optional BaseComponent to createText + the props as generic? Cause for me, looking at that, I thought that createText and createView can also be used as a customised createRestyleComponent for Text and View.