我希望能在使用kotlin-js创建的react网站中使用bootstrap Card组件。 应用程序使用kotlin-react包装器和react-bootstrap库。react-bootstrap文档说要使用来放置内容。
我需要的是CardBody组件。
类似这样:
<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 中使用该卡片。
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"
}
}
}
}
但是CardBody不是一个独立的react-bootstrap组件,不能像导入Card组件那样导入。它是Card组件内部的一个值。
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` */
但是这个无法编译。
- 如何为一个包含子组件而不是单独的组件文件的组件编写 Kotlin 声明文件?
- 如何在 RBuilder.render() 方法中使用此组件?
css{ fontFamily = "sans-serif" }
的事情? - ravindu1024