简体   繁体   English

如何仅在一个组件上应用 Semantic UI 的 css?

[英]How can I apply Semantic UI's css only on one component?

I need to import the Semantic UI to make a Multiple Dropdown work properly.我需要导入语义 UI 以使 Multiple Dropdown 正常工作。 However, when I import their CSS, it overrides a lot of my css.但是,当我导入他们的 CSS 时,它会覆盖我的很多 css。 Obviously, I don't want them to override my style, but I want to use their component with their style that I will custom.显然,我不希望他们覆盖我的样式,但我想将他们的组件与我将自定义的样式一起使用。

Is there a way to apply a stylesheet only to one component instead of applying it to every component on my webpage?有没有办法将样式表仅应用于一个组件,而不是将其应用于我网页上的每个组件? I am really stuck on this and it sucks...我真的被困在这个问题上,这很糟糕......

Thanks a lot for your help!非常感谢你的帮助!

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Dropdown } from 'semantic-ui-react';
import { withTranslation } from 'react-i18next';
import { handleChange } from 'redux/actions';
import { connect } from 'react-redux';
import FormContext from 'context/FormContext';
import 'semantic-ui-css/semantic.min.css';

export class DropdownSearch extends Component {
  constructor (props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  getRequired () {
    if (this.props.required === true) {
      return <span className="tw-font-semibold tw-text-red-500 tw-text-sm tw-ml-2">{this.props.t('required')}</span>;
    }
  }

  handleChange (e, context) {
    var value = e.target.value;
    // this.props.handleChange(this.props.name, value, context.name);
  }

  render () {
    return (
      <FormContext.Consumer>
        {context =>
          <div className={`tw-flex tw-flex-col ${this.props.size} tw-px-2 tw-mb-3`}>
            <label htmlFor={this.props.name} className="tw-text-sm tw-font-bold">{this.props.title || this.props.t('common:' + this.props.name)}{this.getRequired()}</label>
            <Dropdown {...this.props} id={this.props.name} placeholder={this.props.title}/>
            {this.props.errors && this.props.errors[context.name] && this.props.errors[context.name][this.props.name] && (
              <div className="tw-bg-red-100 tw-mt-2 tw-border-l-4 tw-border-red-500 tw-text-red-700 tw-p-2 tw-text-sm">
                <p>{this.props.errors[context.name][this.props.name]}</p>
              </div>
            )}

          </div>
        }
      </FormContext.Consumer>

    );
  }
}

DropdownSearch.defaultProps = {
  size: 'w-full',
  required: true,
  type: 'text'
};

DropdownSearch.propTypes = {
  name: PropTypes.string.isRequired,
  title: PropTypes.string,
  size: PropTypes.string.isRequired,
  required: PropTypes.bool,
  type: PropTypes.string,
  t: PropTypes.func.isRequired
};

const mapStateToProps = ({ errors }, ownProps) => {
  return {
    errors: errors
  };
};

export default connect(mapStateToProps, { handleChange })(withTranslation(['input'])(DropdownSearch));

Applied only to <Dropdown> is the goal仅应用于<Dropdown>是目标

This is obviously a little late to the game, but there's an easier way than turning to CSS Modules for your specific use case.这显然有点晚了,但是对于您的特定用例,有一种比转向 CSS 模块更简单的方法。 If you only need to import the styles for one of Semantic UI's components such as the aforementioned <Dropdown> component, the easiest way to handle this is by importing the minified version of that specific component's css from the semantic-ui-css package which you've already installed. If you only need to import the styles for one of Semantic UI's components such as the aforementioned <Dropdown> component, the easiest way to handle this is by importing the minified version of that specific component's css from the semantic-ui-css package which you '已经安装了。

If you take a look at the semantic-ui-css files in your node_modules folder, you should notice that each component's css is separated out into individual files.如果您查看 node_modules 文件夹中的 semantic-ui-css 文件,您应该注意到每个组件的 css 被分离成单独的文件。 All you would need to do is import it like this:您需要做的就是像这样导入它:

import "semantic-ui-css/components/dropdown.min.css";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中仅将 css 应用于此组件? - How can I apply the css only to this component in React? 如何在 HTML/CSS 中使用语义 UI 创建模态? - How can I create a modal using Semantic UI in HTML/CSS? 如何在不使用semantic.css 的情况下在Semantic UI 中实现组件? - How Can I implement the components in Semantic UI without using semantic.css? 如何应用 css 全局 class 仅在一个组件不同? Angular - How to apply css global class different only in one component ? Angular 我可以在 Semantic UI React 的下拉组件的选项数组中指定分隔符或标题吗? - Can I specify a Divider or Header in Semantic UI React's options array for the dropdown component? 我如何将背景图像应用于仅 ONE Component React 的主体 - How i can apply a background image to the body of only ONE Component React 我如何使用react-semantic-ui和功能组件制作排序表? - How i can make a sorted table using react-semantic-ui and functional component? 如何将 css 应用于组件中的子标签 - How can I apply css to the child tag from component 我如何将jQuery事件仅应用于页面上的一个输入而没有类和ID - How can I apply jQuery event to only one input on page without classes and id's 如何将CSS仅应用于jQuery中的数字? - How can I apply CSS only to digits in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM