我似乎无法使用样式组件设置PrimeReact组件的样式。
给出下面的代码来渲染InputText,我的目的是改变它的宽度。但它不起作用。
import styled from "styled-components";
import {InputText} from 'primereact/components/inputtext/InputText';
class Component extends React.Component {
render() {
return (
<InputText/>
)
}
const ComponentView = styled(Component)`
.ui-inputtext {
width: 1000px;
}
`;
styled-components
生成一个应该传递给组件的className
。
import styled from "styled-components";
import {InputText} from 'primereact/components/inputtext/InputText';
class Component extends React.Component {
render() {
return (
<InputText className={this.props.className} /> <---- here
)
}
const ComponentView = styled(Component)`
.ui-inputtext {
width: 1000px;
}
`;
如果InputText
不接受className
,你可以简单地用另一个组件包装它:
import styled from "styled-components";
import {InputText} from 'primereact/components/inputtext/InputText';
class Component extends React.Component {
render() {
return (
<div className={this.props.className}> <---- here
<InputText />
</div>
)
}
const ComponentView = styled(Component)`
.ui-inputtext {
width: 1000px;
}
`;
PrimeReact有很多样式应用了单独的sass样式表,通常结合多个类名和html标签。
为了让你的风格获胜,你需要更多的CSS特性。
解决方案是使用嵌套选择器,如:
const ComponentView = styled(Component)`
&&& {
width: 1000px;
}`
这将生成3个相同的类名,并由Styled Components docs推荐。需要更多classname特异性?使用更多&
s。
或者你可以放入!important
。我见过这个。
或者编辑their sass file。