当前位置: 首页 > 知识库问答 >
问题:

React Component添加了由于导入css模块而无法识别的类名称

史高阳
2023-03-14

目前正在进行一个使用React和CSS模块的大型项目。我想在一堆列表项上实现“反应-任何东西-排序”。

到目前为止,由于“react anything sortable”将以下类添加到“react anything component”中的任何子级,因此实现陷于停顿状态:。ui可排序。ui可排序项。用户界面拖拉和。ui可排序占位符。我假设这些类是为“react anythis sortable”传递的,以识别哪些DOM元素正在被拖动、放置等。

我通过参照导入列表组件的样式。像这样的scss文件:

import styles from './WidgetList.scss'

若要在零部件上使用样式,需要添加样式。类来使用类:

<div className={styles.container}>Something</div>

因此,这是可以理解的。由“react anything sortable”放置的ui sortable无法引用样式表,因为它不会添加。风格。

我该如何解决这个问题?

编辑:下面是我如何实现它的

WidgetList。js:

import React from 'react';
import ThinScrollbar from 'components/Scrollbars/ThinScrollbar';
import PureComponent from '../PureComponent';

import Sortable from 'react-anything-sortable';
import { sortable } from 'react-anything-sortable';
import styles from './WidgetList.scss';

@sortable
class WidgetListItem extends PureComponent {
    render() {
        return (
            <div {...this.props}>
                {this.props.children}
            </div>
        )
    }
}

export default class WidgetList extends PureComponent {
    constructor() {
        super();
        this.state = {};
    }

    handleSort(data) {
        this.setState({
            result: data.join(' ')
        });
        console.log(this.state.result)
    }

    toggleCheckbox(evt) {
        console.log(evt)
    }

    render() {
        let items = [1,2,3,4,5,6]
        // TODO: move widget creation to its own component <WidgetItem/>
        const widgetItems = items.map(i => {
            return (
                <WidgetListItem className="vertical" sortData={i} key={i}> //<--- this is where .ui-sortable-item is added on render
                    <div className={styles.item} i={i}>
                        <i className={styles.fa}></i>{`Widget ${i}`}
                        <div className={styles.checkbox} onClick={this.toggleCheckbox}></div>
                    </div>
                </WidgetListItem>
            )
        })
        return <div className={styles.container}>
            <ThinScrollbar>
                <Sortable onSort={::this.handleSort} className="vertical-container" direction="vertical"> //<--- this is where .ui-sortable is added on render
                    {widgetItems}
                </Sortable>
            </ThinScrollbar>
        </div>
    }
}

WidgetList。scss

@import "../../theme/variables";

.container {
    width: 100%;
    height: calc((100% - 335px) / 2);
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    padding: 0 4px 0 10px;
}

.item {
    border-left: 5px solid #46484C;
    background-color: #303236;
    padding: 8px;
    min-height: 36px;
    font-size: 12px;
    line-height: normal;
    cursor: pointer;
    margin-bottom: 5px;
    margin-right: 15px;
}

.item:hover {
    background-color: #34363b;
}

.item:last-of-type {
    margin-bottom: 0;
}

.fa {
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #b7b7b7;
    margin-right: 8px;
}

.fa:before {
    content: '\f07b';
}

.checkbox {
    width: 20px;
    height: 20px;
    float: right;
    background: url('img/checkboxes.gif') 0 0 no-repeat;
}

.activeCheckbox {
    composes: checkbox;
    background-position: 0 -20;
}

.ui-sortable {
    display: block;
    position: relative;
    overflow: visible;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
}

.ui-sortable:before,
.ui-sortable:after {
    content: " ";
    display: table;
}

.ui-sortable:after {
    clear: both;
}

.ui-sortable .ui-sortable-item {
    float: left;
    cursor: move;
}

.ui-sortable .ui-sortable-item.ui-sortable-dragging {
    position: absolute;
    z-index: 1688;
}

.ui-sortable .ui-sortable-placeholder {
    display: none;
}

.ui-sortable .ui-sortable-placeholder.visible {
    display: block;
    z-index: -1;
}

.vertical-container {
    width: 200px;
    height:500px;
    padding: 10px;
    border: 1px #ccc solid;
    background-color: red;
}

.vertical.ui-sortable-item {
    float: none;
    display: block;
    width: 100%;
    padding: 10px 5px;
    margin-bottom: 10px;
    border: 1px #eee solid;
    background-color: red;
}

共有1个答案

岳华灿
2023-03-14

如果您无法控制ui可排序的className,即您不能使用css模块,您可以将这些类名导出为“全局”类:

/* Will be hashed */
.activeCheckbox {
    background-position: 0 -20px;
}

/* Will be left as 'ui-sortable' */
:global .ui-sortable {
    display: block;
}

如果您在同一个选择器中有全局和本地类名称的组合,您还可以将单个类名称指定为“全局”:

/* 'ui-sortable' will be left as 'ui-sortable', 'bar' will be hashed */
:global(.ui-sortable) .bar {
    display: block;
}
 类似资料:
  • 这是我正在使用的图书馆。但我需要更改java代码的样式。但是由于它是一个外部依赖项,所以文件是只读的。 这是我的Android文件结构: Android文件结构截图 这是我的build.gradle(模块:App)的依赖项块代码: code bloack的屏幕截图 在我的share.java文件中,我尝试导入CarouselPicker,这是我为alt+enter得到的结果: Alt+Enter给

  • 我想使用JavaFX作为我的游戏的开始屏幕。 然而,由于某些原因,导入的“javafx”似乎无法识别。 起初,我(出于某种奇怪的原因)认为我需要e(fx)clipse,但是我成功安装了它,它并没有解决问题。我在互联网上没有找到其他可以解决这个问题的东西。 我需要导入javafx作为库还是什么?如果是,在哪里? 谢谢

  • 问题内容: 尝试使用别名时-这很常见,我很惊讶地看到以下结果: 创建一个别名 使用别名导入其中包含的模块 python中是否还有其他语法/等效项可用于导入模块? 问题答案: 使用并 不会 创建一个别名。您误解了导入系统。 导入有两件事: 将模块加载到内存中,并将结果存储在中。此操作仅执行 一次 ;后续导入将重用已经加载的模块对象。 在当前名称空间中绑定一个或多个名称。 该语法使您可以在最后一步中控

  • 问题内容: jaxb插件有很大的问题。我有一个项目A,其中有src / main / resources / xsd / common.xsd文件。在这个项目中,我使用cxf-xjc-plugin生成Java类。我在src / main / resources / META- INF下也有我的Episod文件,名为sun-jaxb.episode 接下来,我有项目B,该项目对项目A具有Maven依

  • 问题是: 我的问题是:我做错了什么?我如何修复事情以成功导入BuildingType接口?也许问题出在类路径上,但是我不知道哪个值是合适的。 代码组织: buildingType.java

  • 我有一个简单的Flutter应用程序。 如果我将类移动到另一个文件中,例如custom_card.dart,然后执行,我会得到错误消息:。如果类留在main.dart中,那么一切都可以工作。我错过了什么?

  • 尝试导入tensorflow时出现以下情况: 回溯(最近一次调用):文件 “”,第 1 行,在文件 “/Applications/PyCharm.app/Content/helpers/pydev/_pydev_bundle/pydev_import_hook.py” 中,第 20 行,在 do_import 模块 = self._system_import(name, *args, **kwar

  • 问题内容: 我正在尝试安装,但正在运行return 在使用各种软件包之前,这种情况会持续一段时间,然后再返回其他任何内容。给我: 如何安装?在github页面的自述文件中使用了(而不是)。我的Go版本是。 问题答案: 这些问题与无效有关。 我认为您已安装Go in 。 因此,将您的路径更改为的值。 看来您的工作空间(GOPATH)位于。 这可能会解决您的问题。 将此添加到bash配置文件的底部,位