在样式组件 React 中忽略父样式中的 CSS

发布于 2025-01-12 03:52:46 字数 975 浏览 0 评论 0原文

我正在使用使用样式组件的现有组件库创建一个 React 组件。我有时需要覆盖父库中的样式并使用我自己的样式。这是库中具有样式的 TextInput 组件,

border: 2px solid black;

在使用此组件时,我不希望输入框周围有边框。相反,我只想要一条下划线。

我用两种方法做到了 使用样式道具

<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{borderTop: 'none',borderRight: 'none',
borderLeft:'none', borderBottom: '2px solid black'}}
/>

通过这个我得到一个黑色边框底部,但它需要我对所有其他三个重复。有没有更好的方法让我可以写一行来解决我的问题?

我根据现有组件创建了一个样式组件,

const TextInputStyled = styled(TextInput)`
border-top: none;
border-right: none;
border-left: none;
border-bottom: 1px solid black`;

这是我具有上述样式的组件。

<TextInputStyled
                name="test-input"
                value={testInput}
                onChange={onInputChange}
            />

在这种情况下,我得到的边框底部为 2px 黑色,但另一个边框也存在,这也是重复。

有更好的方法吗?我经常需要调整父组件的样式。

非常感谢任何帮助。

I am creating a React component using an existing component library that uses styled component. I time to time need to override the style from the parent library and use my own. Here is a TextInput component from the library that has style,

border: 2px solid black;

while using this component I don't want to have the border around the input box. Instead, I just want one underline.

I did it two ways,
Using style props,

<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{borderTop: 'none',borderRight: 'none',
borderLeft:'none', borderBottom: '2px solid black'}}
/>

By this I get a black border-bottom but it requires me to repeat for all other three. Is there a better way so that I can write one line and that solves my issue ?

I created a styled component based on the existing component,

const TextInputStyled = styled(TextInput)`
border-top: none;
border-right: none;
border-left: none;
border-bottom: 1px solid black`;

And here my component with the above style.

<TextInputStyled
                name="test-input"
                value={testInput}
                onChange={onInputChange}
            />

In this case I get the border-bottom with 2px black but the other border also exist and this is also a repetition.

Is there a better to do this ? I often need to twitch the style of the parent component.

Any help is very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2025-01-19 03:52:46

你为什么不尝试这个代码:

<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{border:'unset', borderBottom: '2px solid black'}}
/>

更新:

如果你坚持使用样式组件,ThemeProvider是解决方案:


const TextInput=styled.input `
border: 2px solid black;
border-bottom:  ${props => props.theme.borderBottom};
border-top:${props => props.theme.borderTop};
border-left:${props => props.theme.borderLeft};
border-right:${props => props.theme.borderRight};
`;

       \\Define what props.theme will look like
 const theme= {
    borderTop: 'none;',
    borderRight: 'none;',
    borderLeft: 'none;',
    borderBottom: '1px solid black;'
  }
return(<>
  \\normal TextInput with border
 <TextInput
name="test-input"
/>


     \\customized TextInput without border except underline
 <ThemeProvider theme={theme}>
  <TextInput
name="test-input"



/>
</ThemeProvider>
</>
)


如果你不了解ThemeProvider,请阅读这篇文章:

https://styled-components.com/docs/advanced

why don't you try this code:

<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{border:'unset', borderBottom: '2px solid black'}}
/>

Update:

If you insist on using styled component, ThemeProvider is the solution:


const TextInput=styled.input `
border: 2px solid black;
border-bottom:  ${props => props.theme.borderBottom};
border-top:${props => props.theme.borderTop};
border-left:${props => props.theme.borderLeft};
border-right:${props => props.theme.borderRight};
`;

       \\Define what props.theme will look like
 const theme= {
    borderTop: 'none;',
    borderRight: 'none;',
    borderLeft: 'none;',
    borderBottom: '1px solid black;'
  }
return(<>
  \\normal TextInput with border
 <TextInput
name="test-input"
/>


     \\customized TextInput without border except underline
 <ThemeProvider theme={theme}>
  <TextInput
name="test-input"



/>
</ThemeProvider>
</>
)


If you don't know about ThemeProvider read this article:

https://styled-components.com/docs/advanced

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文