);styledPopov">
我仍然在学习反设计和样式组件之间的交互作用,我有一个问题:
我知道我能够传递诸如popover (它接受"overlayClassName“道具)这样的组件的样式:
const styledPopover = ({ className, ...props }) => (<ANTPopover {...props} overlayClassName={className} />);
styledPopover.propTypes = {
className: PropTypes.string,
};
export const Popover = styled(styledPopover)`...`
但是对于通知,我有className支柱,但是没有组件可以样式,只有一个函数。
https://ant.design/components/notification/
有什么方法可以将它包装成一个样式的组件吗?
非常感谢,
发布于 2020-08-21 02:52:39
Antd通知样式的一种简单方法是使用来自style
的API接口配置
样式:自定义内联样式-> CSSProperties
下面是一个简单的示例CodeSandBox。
若要与styledComponents一起使用antd,可以重写默认返回的getContainer
。这就是为什么您的createGlobalStyle
方法可以工作。
在这里,您可以将ref
返回到触发器,例如返回到Button
。
getContainer: (triggerNode) => buttonRef.current
注意:选择了正确的样式,但是整个通知似乎被破坏了(测试cancel按钮)。
来自CodeSandbox的完整代码示例。
import React, { useRef } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Button, notification } from "antd";
import styled from "styled-components";
const Styled = styled.div`
.ant-notification-notice {
background-color: green;
}
`;
const Notification = () => {
const openNotification = () => {
notification.open({
message: "Notification Title",
duration: 20,
description:
"This is the content of the notification. This is the content of the notification. This is the content of the notification.",
onClick: () => {
console.log("Notification Clicked!");
},
getContainer: (triggerNode) => {
console.log(triggerNode);
console.log(buttonRef);
// return document.body;
return buttonRef.current;
}
});
};
const buttonRef = useRef(null);
return (
<Styled>
<Button type="primary" onClick={openNotification} ref={buttonRef}>
Open the notification box
</Button>
</Styled>
);
};
ReactDOM.render(<Notification />, document.getElementById("container"));
https://stackoverflow.com/questions/63457011
复制