故事书和AntDesign组件-如何使用CRA和Typescript进行设置?

时间:2019-11-01 07:32:33

标签: typescript create-react-app antd storybook

我想用带有Typescript的CRA项目中的AntDesign组件之上构建的组件来设置Storybook。

CRA-v3 故事书-v5.25 AntDesign-v3.23.2

我成功设置了CRA + AntDesign,设置了Storybook,使用AntD css类在Storybook中渲染了我的AntDesign组件,但是样式丢失了

我尝试了

这些方法都不允许我使用样式渲染AntD组件

// .stories / webpack.config.js

const path = require("path");

module.exports = {
  module: {
    rules: [
      {
        loader: "babel-loader",
        exclude: /node_modules/,
        test: /\.js$/,
        options: {
          presets: ["@babel/react"],
          plugins: [["import", { libraryName: "antd", style: true }]]
        }
      },
      {
        test: /\.less$/,
        loaders: [
          "style-loader",
          "css-loader",
          {
            loader: "less-loader",

            options: {
              modifyVars: { "@primary-color": "#d8df19" },
              javascriptEnabled: true
            }
          }
        ],
        include: path.resolve(__dirname, "../")
      }
    ]
  }
};

// .stories / .babelrc

{
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "style": "css"
      }
    ]
  ]
}

//我基于AntDesign的组件

import { Menu } from "antd";
import React, { FC } from "react";
import { RouteComponentProps, withRouter } from "react-router";
import { styled } from "styletron-react";

export const Sidebar: FC<RouteComponentProps> = ({ history }) => {
  const SidebarLayout = styled(Menu, {
    height: "100vh",
    width: "100%"
  });

  return (
    <SidebarLayout
      onSelect={item => {
        history.push(item.key);
      }}
      defaultSelectedKeys={["/"]}
      selectedKeys={[history.location.pathname]}
      mode="inline"
      theme="dark"
    >
      <Menu.Item key="/">Table View</Menu.Item>
      <Menu.Item key="/dashboard">Dashboard</Menu.Item>
    </SidebarLayout>
  );
};

export default withRouter(Sidebar);

//我的故事

import { storiesOf } from "@storybook/react";
import { Button } from "antd";
import React from "react";
import { MemoryRouter } from "react-router";
import Sidebar from ".";

storiesOf("Sidebar", module).add("Renders a regular menu", () => {
  return (
    <MemoryRouter initialEntries={["/"]}>
      <Sidebar />
      <Button type="primary">Button</Button>
    </MemoryRouter>
  );
});

//运行故事书将显示以下内容: outcome of running storybook

我希望故事书中呈现的基于AntD的组件与CRA项目中的呈现方式相同: outcome of running CRA based project

2 个答案:

答案 0 :(得分:2)

这是我可以使其与 Storybook v6

一起使用的方法

步骤1:安装用于Ant设计的预设。 Source

npm i -D @storybook/preset-ant-design

步骤2:在您的.storybook / main.js中,添加上述预设的参考。还要注意,@ storybook / preset-create-react-app的选项是如何被覆盖的。

module.exports = {
  "stories": [
    "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/preset-ant-design",
    {
      "name": "@storybook/preset-create-react-app",
      "options": {
        "craOverrides": {
          "fileLoaderExcludes": ["less"]
        }
      }
    }
  ],
}

像Storybook 6.0一样,它可以处理其余的所有事情,在我看来,除了这两者之外,无需进行任何更改。

答案 1 :(得分:0)

在故事书的antd文件中导入import 'antd/dist/antd.css';样式config.js

// @ .storybook/config.js 
import { configure } from '@storybook/react';
import 'antd/dist/antd.css';

function loadStories() {
  const req = require.context('stories', true, /\.stories\.js$/);
  req.keys().forEach(filename => req(filename));
}

configure(loadStories, module);

请参阅storybook文档中的CSS Support