我想建立故事簿与组件之上的AntDesign组件在我的CRA项目内的类型记录。
v3故事书- v5.25 AntDesign - v3.23.2
我成功地设置了CRA + AntDesign,设置了故事簿,我的AntDesign组件呈现在带有AntD css类的Storybook中,但是样式丢失了
我试过了
没有一种方法允许我使用样式来呈现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的my组件
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>
);
});//运行故事书如下所示:运行故事书的结果
我希望在我的故事书中呈现基于AntD的组件,就像在我的CRA项目:基于CRA的项目运行结果中那样
发布于 2020-08-27 11:09:00
下面是我如何使它与故事集v6一起工作的方法
步骤1:为Ant设计安装一个预置。来源
npm i -D @storybook/preset-ant-design
步骤2:在您的./main.js中添加上述预设的引用。还要注意@storybook/预设-创建-反应-应用程序的选项是如何被忽略的。
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负责其余的事务,除了这两个之外,不需要进行任何更改,在我的例子中。
发布于 2019-11-01 16:35:01
在故事书的antd文件中导入import 'antd/dist/antd.css';样式:
// @ .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);请参阅CSS支持 at storybook docs。
https://stackoverflow.com/questions/58655667
复制相似问题