How to wrap up Ant Design with Styled Components and TypeScript?

前端 未结 5 1543
独厮守ぢ
独厮守ぢ 2021-02-13 15:47

I want to wrap up my ant-design components with styled-components, I know that this is possible (https://gist.github.com/samuelcastro/0ff7db4fd54ce2b80cd1c34a85b40c08) however I

5条回答
  •  终归单人心
    2021-02-13 16:28

    index.tsx (Button Component)

    import { Button as AntButton } from 'antd'
    import { NativeButtonProps } from 'antd/lib/button/button'
    import 'antd/lib/button/style/css'
    import * as React from 'react'
    import styledComponents from 'styled-components'
    import * as colours from '../colours'
    
    const getColour = (props: any) =>
      props.status === 'green'
        ? colours.STATUS_GREEN
        : props.status === 'red'
          ? colours.STATUS_RED
          : props.type === 'primary'
            ? colours.PRIMARY
            : colours.WHITE
    
    export interface ButtonProps extends NativeButtonProps {
      status?: string
    }
    
    export default styledComponents((props: ButtonProps) => )`
      &:focus,
      &:hover
      & {
        background-color: ${getColour};
        border-color: ${getColour};
      }
    `
    
    

    import React from 'react'
    import Button, { ButtonProps } from './index'
    
    interface ButtonAsyncSingleSuccessProps extends ButtonProps {
      clickFunc: any, // (...args: any[]) => Promise
      labelLoading: string,
      labelReady: string,
      labelSuccess: string,
    }
    
    interface ButtonAsyncSingleSuccessState {
      label: string,
      loading: boolean,
      status: string
    }
    
    export default class ButtonAsyncSingleSuccess extends React.Component<
      ButtonAsyncSingleSuccessProps,
      ButtonAsyncSingleSuccessState
    > {
      constructor (props: any) {
        super(props)
        this.state = {
          label: props.labelReady,
          loading: false,
          status: ''
        }
      }
      public clickHandler (event: any) {
        const { labelLoading, labelReady, labelSuccess, clickFunc } = this.props
        this.setState({
          label: labelLoading,
          loading: true,
          status: ''
        })
        clickFunc(event)
          .then(() => {
            this.setState({
              label: labelSuccess,
              loading: false,
              status: 'green'
            })
          })
          .catch(() => {
            this.setState({
              label: labelReady,
              loading: false,
              status: 'red'
            })
          })
      }
      public render () {
        const {
          labelLoading,
          labelReady,
          labelSuccess,
          clickFunc,
          ...props
        } = this.props
        const { label, loading, status } = this.state
        if (status === 'red') {
          setTimeout(() => this.setState({ status: '' }), 1000) // flash red
        }
        return (
          
        )
      }
    }

提交回复
热议问题