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

前端 未结 5 1544
独厮守ぢ
独厮守ぢ 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:18

    I have found this ancient question and try to solve in an easy way:

    import React from 'react';
    import styled from 'styled-components';
    import { Card } from 'antd';
    import { CardProps } from 'antd/lib/card';
    
    export const NewCard: React.FunctionComponent = styled(Card)`
      margin-bottom: 24px;
    `;
    

    without render props :D

    if you just need wrap a component as function component, that's all right. But you will lose the properties of class component such as Card.Meta.

    There is a workaround:

    import React from 'react';
    import styled from 'styled-components';
    import { Card } from 'antd';
    import { CardProps } from 'antd/lib/card';
    
    export const NewCard: typeof Card = styled(Card)`
      margin-bottom: 24px;
    ` as any;
    

    Everything (maybe... XD) works as original Antd Component ;)

提交回复
热议问题