Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'm having troubles to do the same with TypeScript.

This is what I have so far:

import { Button as AntButton } from 'antd';
import { ButtonProps } from 'antd/lib/button/button';
import styledComponents from 'styled-components';

interface IButtonProps extends ButtonProps {
   customProp: string;
}

export const Button = styledComponents<IButtonProps>(AntButton)`
  // any custom style here
`;

As you can see I'm defining my ant-design button with as any in order to make it work, otherwise I get some incompatible types like:

Argument of type 'typeof Button' is not assignable to parameter of
type 'ComponentType<IButtonProps>'.

Type 'typeof Button' is not assignable to type
'StatelessComponent<IButtonProps>'.

Types of property 'propTypes' are incompatible.

 Property 'customProp' is missing in type '{ 
    type: Requireable<string>; 
    shape: Requireable<string>; 
    size: Requireable<string>; 
    htmlType: Requireable<string>; 
    onClick: ...
    etc
 }

Thank you.

Solution:

import { Button as AntButton } from 'antd';
import { NativeButtonProps } from 'antd/lib/button/button';
import * as React from 'react';
import styledComponents from 'styled-components';

export const Button = styledComponents<NativeButtonProps>(props => <AntButton {...props} />)`
    // custom-props
`;
like image 459
Samuel Castro Avatar asked Oct 08 '18 14:10

Samuel Castro


People also ask

How do I use styled-components with AntD?

Note that AntD uses its own css classes to style its components. You might have noticed this if you open up AntD css with Inspect Element. styled from styled-components will apply CSS styling to that component directly! It has the formatting of standard css, but with the importance level to override AntD's styles!

How do you use ant design in React typescript?

Install and Initialization Ensure your system has installed latest version of yarn or npm. Create a new cra-template-typescript project named antd-demo-ts using yarn. Then we go inside antd-demo-ts and start it. Open browser at http://localhost:3000/, it renders a header saying "Welcome to React" on the page.

Can I use SCSS in styled-components?

In SCSS, you can use variables and formulas for expressing colors, font sizes, widths and spacing variants. However, when you switch from SCSS to styled components, the syntax isn't valid anymore. Styled component files are CSS-in-JS, so they are actually JavaScript files.


1 Answers

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<CardProps> = 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)<CardProps>`
  margin-bottom: 24px;
` as any;

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

like image 61
XuToTo Avatar answered Sep 28 '22 03:09

XuToTo