React-native:样式化组件和打字稿中的多个样式道具

时间:2020-07-13 06:33:50

标签: typescript react-native styled-components

我正在为我的应用程序使用React-native打字稿。我真的是React-native的新手。许多语法对我来说都是新的。对于样式,我使用styled-components。我已经创建了全局按钮组件。因此,我可以在不同的组件中使用它。我想创建条件大小和按钮的颜色。因此,我可以从父组件操纵尺寸和颜色。我可以做两个条件样式道具。但是我不知道如何在React-native中执行多个条件。

这是我的Button组件

import React from 'react';
import { Text, TouchableHighlight } from 'react-native';
import styled, { css } from 'styled-components/native';


export interface IButton {
  appearance?: "primary" | "secondary" | "dark" | "light";
  children?: string | JSX.Element;
  size?: "small" | "medium" | "big";
  className?: string;
  disabled?: boolean;
  loading?: boolean;
  style?: React.CSSProperties;
  onPress?: () => void;
}

const Button = ({ appearance, children, size, disabled, loading, style, onPress, className }: IButton) => {
  return (
    <ButtonContainer
      className={className}
      onPress={onPress}
      disabled={disabled}
      style={
        disabled ?
          { ...style, "opacity": 0.5, "pointerEvents": `none` } :
          loading ?
            { ...style, "pointerEvents": `none` } :
            style
      }
    >
      {loading ? <Label>loading...</Label> : <Label>{children} </Label>}
    </ButtonContainer>
  );
};


const Label = styled.Text`
  color: white;
  font-weight: 700;
  align-self: center;
  padding: 10px;
`

const ButtonContainer = styled.TouchableHighlight<
  {
    appearance: IButton["appearance"]; // this is my typescript props I don't know how to connect them with my Button styling.
  }
  >`
  width: 50%;
  margin-top: 5px;
  border-color: grey;
  border-width: 2px;
  border-radius: 5px;
  border-radius: 4px;
 background-color:${props => props.primary ? "gray" : "blue"} //
`

export default Button;

这是我使用按钮组件的父组件。

<Button
    size="medium" //this is does not work because I did not style yet
    onPress={() => console.log('hello')}

  >
    click me 
  </Button>

1 个答案:

答案 0 :(得分:0)

Pageviews

P.S。在深入研究诸如Typescript和样式化组件之类的高级主题之前,您应该坚持基本的原则。