javascript - 如何将带有子项的 react-bootstrap 组件导入 kotlin-js react app

标签 javascript react-bootstrap kotlin-js kotlin-js-interop typescript-definitions

我想在使用 kotlin-js 创建的 React 网站中使用引导卡组件。该应用程序使用 kotlin-react 包装器和 react-bootstrap 库。

react-bootstrap 文档说使用 来放置内容。

<Card>
  <Card.Body>This is some text within a card body.</Card.Body>
</Card>

到目前为止,我已经成功地将 Card JavaScript 模块导入到 kotlin-js 中。

@file:JsModule("react-bootstrap/Card")

import react.RClass
import react.RProps


@JsName("default")
external val Card: RClass<RProps>

这样我就可以在 RComponent 中使用 Card。

class Content : RComponent<RProps, RState>() {
    override fun RBuilder.render() {
        Card {
           +"Hello World"
        }
    }
}

什么渲染到:

<div class="card">
  Hello World
</div>

我还需要的是 CardBody 组件。

像这样:

class Content : RComponent<RProps, RState>() {
    override fun RBuilder.render() {
        Card {
            CardBody {
                + "Hello World"
            }
        }
    }
}

But the CardBody is not a separate react-bootstrap component which could be imported like the Card component. It is a value inside the Card component.


typescript 定义文件 Card.d.ts 如下所示:

import CardImg from './CardImg';
import { BsPrefixPropsWithChildren, BsPrefixRefForwardingComponent } from './helpers';
import { Color, Variant } from './types';
declare const CardBody: BsPrefixRefForwardingComponent<any, {}>;
declare const CardTitle: BsPrefixRefForwardingComponent<any, {}>;
declare const CardSubtitle: BsPrefixRefForwardingComponent<any, {}>;
declare const CardLink: BsPrefixRefForwardingComponent<any, {}>;
declare const CardText: BsPrefixRefForwardingComponent<any, {}>;
declare const CardHeader: BsPrefixRefForwardingComponent<any, {}>;
declare const CardFooter: BsPrefixRefForwardingComponent<any, {}>;
declare const CardImgOverlay: BsPrefixRefForwardingComponent<any, {}>;
export interface CardProps extends BsPrefixPropsWithChildren {
    bg?: Variant;
    text?: Color;
    border?: Variant;
    body?: boolean;
}
declare type Card = BsPrefixRefForwardingComponent<'div', CardProps> & {
    Img: typeof CardImg;
    Title: typeof CardTitle;
    Subtitle: typeof CardSubtitle;
    Body: typeof CardBody;
    Link: typeof CardLink;
    Text: typeof CardText;
    Header: typeof CardHeader;
    Footer: typeof CardFooter;
    ImgOverlay: typeof CardImgOverlay;
};
declare const Card: Card;
export default Card;

如果我使用 dukat 将 TypeScript 定义文件转换为 Kotlin 声明。它会生成一个 Card.module_react-bootstrap.kt 文件

@file:JsModule("react-bootstrap")
@file:JsNonModule
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS")

import kotlin.js.*
import kotlin.js.Json
import org.khronos.webgl.*
import org.w3c.dom.*
import org.w3c.dom.events.*
import org.w3c.dom.parsing.*
import org.w3c.dom.svg.*
import org.w3c.dom.url.*
import org.w3c.fetch.*
import org.w3c.files.*
import org.w3c.notifications.*
import org.w3c.performance.*
import org.w3c.workers.*
import org.w3c.xhr.*

external var CardBody: BsPrefixRefForwardingComponent<Any, Any>

external var CardTitle: BsPrefixRefForwardingComponent<Any, Any>

external var CardSubtitle: BsPrefixRefForwardingComponent<Any, Any>

external var CardLink: BsPrefixRefForwardingComponent<Any, Any>

external var CardText: BsPrefixRefForwardingComponent<Any, Any>

external var CardHeader: BsPrefixRefForwardingComponent<Any, Any>

external var CardFooter: BsPrefixRefForwardingComponent<Any, Any>

external var CardImgOverlay: BsPrefixRefForwardingComponent<Any, Any>

external interface CardProps : BsPrefixPropsWithChildren {
    var bg: dynamic /* String | String | String | String | String | String | String | String | String? */
        get() = definedExternally
        set(value) = definedExternally
    var text: String? /* 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | 'white' | 'muted' */
        get() = definedExternally
        set(value) = definedExternally
    var border: dynamic /* String | String | String | String | String | String | String | String | String? */
        get() = definedExternally
        set(value) = definedExternally
    var body: Boolean?
        get() = definedExternally
        set(value) = definedExternally
}

external interface `T$0` {
    var Img: Any
    var Title: Any
    var Subtitle: Any
    var Body: Any
    var Link: Any
    var Text: Any
    var Header: Any
    var Footer: Any
    var ImgOverlay: Any
}

@JsName("default")
external var Card: BsPrefixRefForwardingComponent<String /* 'div' */, CardProps> /* BsPrefixRefForwardingComponent<String /* 'div' */, CardProps> & `T$0` */

但这不能编译。


  1. 如何为内部有子组件而不是单独的组件文件的组件编写 kotlin 声明文件?
  2. 如何在 RBuilder.render() 方法中使用该组件?

最佳答案

阅读 Typescript declerations,似乎

文件:Card.kt

@file:JsModule("react-bootstrap/Card")

package bootstrap

import react.RClass
import react.RProps

@JsName("default")
external val Card: RClass<RProps>

文件:Card.ktx.kt(因为你不能在 JsModule 中有非外部声明)

package bootstrap

import react.RClass
import react.RProps

val CardBody : RClass<RProps> = Card.asDynamic().Body

val CardLink : RClass<RProps> = Card.asDynamic().Link

用法

fun RBuilder.MyComp(){
  Card {
    CardBody {+"Card body goes here" }
  }
}

应该可以。

关于javascript - 如何将带有子项的 react-bootstrap 组件导入 kotlin-js react app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62920114/

相关文章:

javascript - 当我单击带有远程 : true, 的链接时,并未初始化插件

javascript - 如何通过 jQuery 更改文本?

reactjs - Reactstrap 和 React-Bootstrap 有什么区别?

javascript - 如何在 FIRESTORE 中的文档中输入多个数据

javascript - react Bootstrap 组件开关不工作

javascript - 如何在ReactJS中使用CSS两两排列图形

kotlin-js - dceTask keep 不保留定义的类

kotlin - Koltin 多平台多模块 JVM/JS 项目设置

intellij-idea - 在 Intellij 的 KotlinJS 中使用 pow

javascript - 根据另一个值过滤元素