Share
Embed
フロントエンドもKotlinで書きたい! -WebページをKotlin/JSで作った軌跡-
Kotlin Fest 2019 - Hall C 16:45〜
「フロントエンドもKotlinで書きたい! -WebページをKotlin/JSで作った軌跡-」のセッション資料です
subroh_0508
August 24, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
108 slides
-
"CPVU.F w ʹ͜͠Γ͞ͿΖʙ ຊ໊ࡔ্৴ w ౦ژɾҏ౾େౡग़ w גࣜձࣾ#FBS5BJM w
"OESPJEΤϯδχΞ ,PUMJO+BWB ˑ w 8FCΤϯδχΞ 3BJMT3FBDU w झຯͰ,PUMJO+4 -
"HFOEB ,PUMJO+4ͷ֓ཁ w ྺ࢙ɺࣄྫհCZTVCSPI@ ,PUMJO+4 3FBDUKTͰʮ)FMMP 8PSMEʯ w OQN ,PUMJO+4
w (SBEMF ,PUMJO+4 ,PUMJO+4ΞϓϦͷ+4ϥΠϒϥϦͷಋೖ -
,PUMJO+4ͷ֓ཁ +4ͰͷϑϩϯτΤϯυ։ൃʹඞཁͳϞϊ w +4ίʔυɾελΠϧγʔτ $44 ɾ)5.-ϑΝΠϧ w ύοέʔδཧπʔϧ OQN
w Ϟδϡʔϧόϯυϥʔ 8FCQBDL w ෳͷ+4ϞδϡʔϧΛͭʹ·ͱΊɺ)5.-Ͱѻ͍͍͢ܗʹ͢Δ w ϞδϡʔϧؒͷґଘؔղܾɺNJOJGZɺίʔυɾϦιʔεͷม ྫ54ˠ+4ɺ4BTTˠ$44 -
,PUMJO+4ͷ֓ཁ w +4ίʔυʹมՄೳͳ,PUMJOඪ४ϥΠϒϥϦ w ,PUMJOˠ+4ͷมΛ࣮ߦ͢Δ(SBEMFϓϥάΠϯ w OQN8FCQBDLͷίϚϯυΛ࣮ߦ͢Δ(SBEMFϓϥάΠϯ ʴIUNMDTTͷ%4-ϥΠϒϥϦɺ֤छ+4 ϥούʔ ϥΠϒϥϦ
,PUMJO+4͕ఏڙ͢ΔϞϊ ,PUMJO͔Β+4ͷมɺOQNʹΑΔόʔδϣϯཧɺ8FCQBDLʹΑΔόϯυϧ શͯΛ,PUMJOͷੈքͰ݁ͤ͞ɺϑϩϯτΤϯυ։ൃΛ࣮ݱ͢Δʂ -
,PUMJO+4ͷྺ࢙ ,PUMJOϦϦʔε w ,PUMJO+4ਖ਼ࣜϦϦʔεɻʮLPUMJOKTʯͰ,PUMJOˠ+4ม͕Ͱ͖ΔΑ͏ʹ LPUMJOGSPOUFOEQMVHJOϦϦʔε w (SBEMF͔ΒͷOQNɺ8FCQBDLͷίϚϯυ࣮ߦΛαϙʔτ͢ΔϓϥάΠϯ ,PUMJOϦϦʔε w ,PUMJO.11ొɻ+7./BUJWFͷϞδϡʔϧͱϩδοΫΛڞ௨Խ
,PUMJOϦϦʔε w LPUMJOKTˠDPNKFUCSBJOTLPUMJOKTʹվ໊ɺLPUMJOGSPOUFOEQMVHJOͷҰ෦ػೳ͕౷߹ w ʮLPUMJOGSPOUFOEQMVHJOࠓޙഇࢭ༧ఆʯͱࠂ ,PUMJOϦϦʔε w 8FCQBDLͷઃఆ߲Ճɺόάमਖ਼ -
,PUMJOϦϦʔε w ,PUMJO+4ਖ਼ࣜϦϦʔεɻʮLPUMJOKTʯͰ,PUMJOˠ+4ม͕Ͱ͖ΔΑ͏ʹ LPUMJOGSPOUFOEQMVHJOϦϦʔε w (SBEMF͔ΒͷOQNɺ8FCQBDLͷίϚϯυ࣮ߦΛαϙʔτ͢ΔϓϥάΠϯ ,PUMJOϦϦʔε w ,PUMJO.11ొɻ+7./BUJWFͷϞδϡʔϧͱϩδοΫΛڞ௨Խ ,PUMJOϦϦʔε
w LPUMJOKTˠDPNKFUCSBJOTLPUMJOKTʹվ໊ɺLPUMJOGSPOUFOEQMVHJOͷҰ෦ػೳ͕౷߹ w ʮLPUMJOGSPOUFOEQMVHJOࠓޙഇࢭ༧ఆʯͱࠂ ,PUMJOϦϦʔε w 8FCQBDLͷઃఆ߲Ճɺόάमਖ਼ ,PUMJO+4ͷྺ࢙ ,PUMJO.11ͷҰ෦ͱͯ͠ ֤छϓϥάΠϯ͕·ͱ·ΔྲྀΕ -
,PUMJOϦϦʔε w ,PUMJO+4ਖ਼ࣜϦϦʔεɻʮLPUMJOKTʯͰ,PUMJOˠ+4ม͕Ͱ͖ΔΑ͏ʹ LPUMJOGSPOUFOEQMVHJOϦϦʔε w (SBEMF͔ΒͷOQNɺ8FCQBDLͷίϚϯυ࣮ߦΛαϙʔτ͢ΔϓϥάΠϯ ,PUMJOϦϦʔε w ,PUMJO.11ొɻ+7./BUJWFͷϞδϡʔϧͱϩδοΫΛڞ௨Խ ,PUMJOϦϦʔε
w LPUMJOKTˠDPNKFUCSBJOTLPUMJOKTʹվ໊ɺLPUMJOGSPOUFOEQMVHJOͷҰ෦ػೳ͕౷߹ w ʮLPUMJOGSPOUFOEQMVHJOࠓޙഇࢭ༧ఆʯͱࠂ ,PUMJOϦϦʔε w 8FCQBDLͷઃఆ߲Ճɺόάमਖ਼ ,PUMJO+4ͷྺ࢙ ͜ͷ࣌ظͷใ͕݁ߏଟ͍ ࠓޙݹ͘ͳΔͷͰҙʂ -
݁ہͲΜͳϞϊ͕࡞ΕΔͷ w ࠷ऴతͳՌˠ+4ϑΝΠϧɺ8FCQBDLͷόϯυϧϑΝΠϧ w +4Ͱ࡞ΕΔϞϊશͯ,PUMJO+4Ͱ࡞Δ͜ͱ͕Մೳʂ ཧ্ w ࣄྫհ w
ࣗͷϙʔτϑΥϦΦαΠτ w খௗ͞Μͷࣗशࣨ ϒϥβ্Ͱ,PUMJOΛֶΔֶशαΠτ -
ࣄྫ ϙʔτϑΥϦΦαΠτ (JUIVCTVCSPIQPSUGPMJP ༻ϥΠϒϥϦɾ"1* w ,PUMJO+4 W w
3FBDUKT w .BUFSJBM6* ˠ'JSFCBTF)PTUJOHͰϗεςΟϯά ɹ-JOLTVCSPINF w (PPHMF.BQ"1* w SFBDUTXJQFBCMFWJFXT -
ࣄྫ খௗ͞Μͷࣗशࣨ (JUIVCTVCSPIPUPOBTIJLPUMJOEFW ػೳ w ϒϥβ্Ͱ,PUMJO,PBOTͷ՝͕Ͱ͖Δ w ՝ͷΫϦΞɺίϯύΠϧΤϥʔʹ ɹ߹ΘͤͯϨεϙϯεΛฦ͢
ˠখௗ͞ΜͱϖΞϓϩ͠ͳ͕Β,PUMJOΛֶश w ,PUMJOͷ3&1-LPUMJOQMBZHSPVOE w ϖʔδભҠ3FBDI3PVUFS খௗ͞Μ -
ࣄྫ খௗ͞Μͷࣗशࣨ (JUIVCTVCSPIPUPOBTIJLPUMJOEFW ༷ϥΠϒϥϦ w ,PUMJO+4 W w
3FBDUKT w .BUFSJBM6* ˠϧʔςΟϯάϥΠϒϥϦΛͬͨ41" ɹ খௗ͞Μ w 3FBDI3PVUFS w LPUMJOQMBZHSPVOE -
,PUMJO+4Ͱ3FBDUΞϓϦΛ࡞Δ ,PUMJOͰॻ͍ͨίʔυΛOQN 8FCQBDLͰϏϧυ w OQNɺ8FCQBDLͷίϚϯυΛ࣮ߦ +4ͷੈքʹ,PUMJOΛಋೖ͢Δ w ϝϦοτ+4Ͱॻ͔ΕͨطଘͷϓϩδΣΫτʹಋೖ͍͢͠
w σϝϦοτOQNɺ8FCQBDL͕ͱ͖ͬͭʹ͍͘ ಛʹ,PUMJOΤϯδχΞ -
,PUMJO+4 OQN w +FU#SBJOTDSFBUFSFBDULPUMJOBQQ w ʮDSFBUFSFBDUBQQʯͷ,PUMJO൛ w ίϚϯυҰൃͰ3FBDUKTͷαϯϓϧΞϓϦΛੜ ཁ/PEFKT
ˠ,PUMJO+4ʹΑΔ3FBDUΞϓϦͷ࣮ΠϝʔδΛ௫Έ͍͢ -
,PUMJO+4 OQN w ϞδϡʔϧͷΠϯετʔϧɾϓϩδΣΫτੜɾ࣮ߦ # インストール $ npm install
-g create-react-kotlin-app # プロジェクト生成 $ create-react-kotlin-app my-app # 実行 $ cd my-app/ $ npm start -
,PUMJO+4ͷϥούʔϞδϡʔϧ͕ ґଘؔʹՃࡁΈʂ "devDependencies": { "@jetbrains/gen-idea-libs": "^1.0.11", "@jetbrains/kotlin-extensions": "^1.0.1-pre.58", "@jetbrains/kotlin-react": "^16.5.2-pre.58",
"@jetbrains/kotlin-react-dom": "^16.5.2-pre.58", "@jetbrains/kotlin-webpack-plugin": "^2.0.3", "@jetbrains/ts2kt-automator": "^1.0.12", "kotlin": "^1.3.0", "kotlinx-html": “^0.6.11", … } -
package index import … fun main(args: Array<String>) { requireAll(require.context("src", true,
js("/\\.css$/"))) render(document.getElementById("root")) { child(App::class) { } } } -
package index import … fun main(args: Array<String>) { requireAll(require.context("src", true,
js("/\\.css$/"))) render(document.getElementById("root")) { child(App::class) { } } } TSDҎԼͷDTTΛҰׅಡΈࠐΈ -
package index import … fun main(args: Array<String>) { requireAll(require.context("src", true,
js("/\\.css$/"))) render(document.getElementById("root")) { child(App::class) { } } } "QQίϯϙʔωϯτΛSFOEFS -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } 3$PNQPOFOUΛܧঝ$PNQPOFOUఆٛ -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } SFOEFSϝιουʹIUNMΛ%4-Ͱهड़ -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } EJWDMBTT/BNFl"QQIFBEFSzʜEJW -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } I8FMDPNFUP3FBDUXJUI,PUMJOI -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } ճస͢Δϩΰͷදࣔίϯϙʔωϯτ -
package app import … class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } ܦա࣌ؒදࣔͷίϯϙʔωϯτ -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } 31SPQTΛܧঝͨ͠5JDLFS1SPQT ˠίϯϙʔωϯτ͔Β ɹड͚औΔͷఆٛ -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } 34UBUFΛܧঝͨ͠5JDLFS4UBUF ˠ5JDLFSίϯϙʔωϯτͷঢ়ଶ -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } JOJUؔͰܦա࣌ؒͷॳظԽ -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } ඵ͝ͱʹTUBUFͷΛߋ৽ ˞TFU4UBUFϝιουͰߋ৽ SFOEFSϝιουͷݺͼग़͠ -
interface TickerProps : RProps { var startFrom: Int } interface
TickerState : RState { var secondsElapsed: Int } class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } ܦա࣌ؒͷදࣔ -
ίʔυൺֱ class App extends React.Component { render() { return (
<div className='App-header'> <Logo/> <h2>Welcome to React with Kotlin</h2> <p className='App-intro'> To get started, edit <code>app/App.kt</code> and save to reload. </p> <p className='App-ticker'> <Ticker startFrom={ 0 }/> </p> </div> ); } } class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div("App-header") { child(Logo::class) { } h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { child(Ticker:class) { attrs.startFrom = 0 } } } } +4 ,PUMJO -
w +4ϥΠϒϥϦΛՃ͍ͨ͠ʂ w npm install hoge --save w Ճͨ͠+4ϥΠϒϥϦΛ,PUMJOͰѻ͍͍ͨʂ w
ޙड़ w 8FCQBDLͷઃఆΛΧελϚΠζ͍ͨ͠ʂ w npm run eject ͰઃఆϑΝΠϧΛग़ྗՄ ,PUMJO+4 OQN -
,PUMJO+4 (SBEMF w (SPVQ*E w "SUJGBDU*E w 1SPKFDUOBNF w
1SPKFDUMPDBUJPO ͓ΈͰ -
,PUMJO+4 (SBEMF ϓϩδΣΫτͷܗ͕ੜʂ Δ͜ͱ w (SBEMFʹґଘؔɺϏϧυઃఆՃ w ,PUMJO+4ͷίʔυՃ w
JOEFYIUNMͷ࡞ 4BNQMF3FQPTVCSPILPUMJOSFBDUTBNQMF -
(SBEMF1MVHJOͷࢦఆ SFQPTJUPSJFTͷࢦఆ ,PUMJO+4 (SBEMF plugins { id 'org.jetbrains.kotlin.js' version
'1.3.50' } group 'subroh0508.net' version '1.0-SNAPSHOT' repositories { jcenter() mavenCentral() maven { url "http://dl.bintray.com/kotlin/kotlin-js-wrappers" } maven { url 'https://plugins.gradle.org/m2/' } } -
8FCQBDLͷઃఆ ,PUMJO+4 (SBEMF kotlin { target { browser {
runTask { devServer = new KotlinWebpackConfig.DevServer( true, false, true, true, false, 8080, null, ["$projectDir/src/main/resources".toString()] ) archiveFileName = "kotlin-react-sample.js" } } } -
8FCQBDLͷઃఆ ,PUMJO+4 (SBEMF kotlin { target { browser {
runTask { devServer = new KotlinWebpackConfig.DevServer( true, false, true, true, false, 8080, null, ["$projectDir/src/main/resources".toString()] ) archiveFileName = "kotlin-react-sample.js" } } } CSPXTFSϒϩοΫͰ 8FCQBDLͷઃఆΛهड़ -
kotlin { target { browser { runTask { devServer =
new KotlinWebpackConfig.DevServer( true, false, true, true, false, 8080, null, ["$projectDir/src/main/resources".toString()] ) archiveFileName = "kotlin-react-sample.js" } } } 8FCQBDLͷઃఆ ,PUMJO+4 (SBEMF QPSUɾQSPYZɾDPOUFOU#BTFΛࢦఆ ˠϦιʔεσΟϨΫτϦͷύεΛ DPOUFOU#BTFʹࢦఆ -
kotlin { target { browser { runTask { devServer =
new KotlinWebpackConfig.DevServer( true, false, true, true, false, 8080, null, ["$projectDir/src/main/resources".toString()] ) archiveFileName = "kotlin-react-sample.js" } } } 8FCQBDLͷઃఆ ,PUMJO+4 (SBEMF BSDIJWF'JMF/BNF ˠόϯυϧޙͷϑΝΠϧ໊ -
ϥΠϒϥϦͷґଘؔ ,PUMJO+4 (SBEMF sourceSets { main { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js" implementation "org.jetbrains.kotlinx:kotlinx-html-js:0.6.12" implementation "org.jetbrains:kotlin-react:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-react-dom:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-styled:1.0.0-pre.83-kotlin-1.3.41" implementation npm("core-js", "~3.1.4") implementation npm("react", "16.9.0") implementation npm("react-dom", "16.9.0") implementation npm("styled-components", "3.4.10") implementation npm("inline-style-prefixer", "5.0.4") } } } -
sourceSets { main { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-js" implementation "org.jetbrains.kotlinx:kotlinx-html-js:0.6.12"
implementation "org.jetbrains:kotlin-react:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-react-dom:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-styled:1.0.0-pre.83-kotlin-1.3.41" implementation npm("core-js", "~3.1.4") implementation npm("react", "16.9.0") implementation npm("react-dom", "16.9.0") implementation npm("styled-components", "3.4.10") implementation npm("inline-style-prefixer", "5.0.4") } } } ϥΠϒϥϦͷґଘؔ ,PUMJO+4 (SBEMF ,PUMJO+4ͷඪ४ϥΠϒϥϦ -
ϥΠϒϥϦͷґଘؔ sourceSets { main { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-js" implementation
"org.jetbrains.kotlinx:kotlinx-html-js:0.6.12" implementation "org.jetbrains:kotlin-react:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-react-dom:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-styled:1.0.0-pre.83-kotlin-1.3.41" implementation npm("core-js", "~3.1.4") implementation npm("react", "16.9.0") implementation npm("react-dom", "16.9.0") implementation npm("styled-components", "3.4.10") implementation npm("inline-style-prefixer", "5.0.4") } } } ,PUMJO+4 (SBEMF LPUMJOYIUNMIUNMͷ%4-ϥΠϒϥϦ LPUMJOSFBDU3FBDUͷϥούʔϥΠϒϥϦ -
sourceSets { main { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-js" implementation "org.jetbrains.kotlinx:kotlinx-html-js:0.6.12"
implementation "org.jetbrains:kotlin-react:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-react-dom:16.9.0-pre.83-kotlin-1.3.41" implementation "org.jetbrains:kotlin-styled:1.0.0-pre.83-kotlin-1.3.41" implementation npm("core-js", "~3.1.4") implementation npm("react", "16.9.0") implementation npm("react-dom", "16.9.0") implementation npm("styled-components", "3.4.10") implementation npm("inline-style-prefixer", "5.0.4") } } } ϥΠϒϥϦͷґଘؔ ,PUMJO+4 (SBEMF +4ͷϥΠϒϥϦOQN Ͱࢦఆ -
,PUMJO+4ͷίʔσΟϯά ,PUMJO+4 (SBEMF // main.kt fun main() { render(document.getElementById("root"))
{ child(App::class) { } } } // App.kt class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div { +"Hello, World!" } } } -
JOEFYIUNMͷ࡞ ,PUMJO+4 (SBEMF <!DOCTYPE html> <html lang="en"> <head> <meta
charset="UTF-8"> <title>Title</title> </head> <body> <div id="root"></div> <script src="kotlin-react-sample.js"></script> </body> </html> ˡIUNMϑΝΠϧΛ SFTPVSDFTҎԼʹஔ -
ಈ͖Λ͚͍ͭͨ ྫΧϯλʔ interface AppState : RState { var count: Int
} class App : RComponent<RProps, AppState>() { override fun AppState.init() { count = 0 } private fun onClickedButton() { setState { count += 1 } } override fun RBuilder.render() { div { +"count: ${state.count}" } button { attrs.onClickFunction = { onClickedButton() } +"click!" } } } -
interface AppState : RState { var count: Int } class
App : RComponent<RProps, AppState>() { override fun AppState.init() { count = 0 } private fun onClickedButton() { setState { count += 1 } } override fun RBuilder.render() { div { +"count: ${state.count}" } button { attrs.onClickFunction = { onClickedButton() } +"click!" } } } TUBUFͷܕఆٛ ಈ͖Λ͚͍ͭͨ ྫΧϯλʔ -
interface AppState : RState { var count: Int } class
App : RComponent<RProps, AppState>() { override fun AppState.init() { count = 0 } private fun onClickedButton() { setState { count += 1 } } override fun RBuilder.render() { div { +"count: ${state.count}" } button { attrs.onClickFunction = { onClickedButton() } +"click!" } } } TUBUFͷॳظԽ ಈ͖Λ͚͍ͭͨ ྫΧϯλʔ -
interface AppState : RState { var count: Int } class
App : RComponent<RProps, AppState>() { override fun AppState.init() { count = 0 } private fun onClickedButton() { setState { count += 1 } } override fun RBuilder.render() { div { +"count: ${state.count}" } button { attrs.onClickFunction = { onClickedButton() } +"click!" } } } ϘλϯΫϦοΫ࣌ͷίʔϧόοΫؔ ˠTFU4UBUFϝιουͰΧϯτΛ૿͢ ಈ͖Λ͚͍ͭͨ ྫΧϯλʔ -
interface AppState : RState { var count: Int } class
App : RComponent<RProps, AppState>() { override fun AppState.init() { count = 0 } private fun onClickedButton() { setState { count += 1 } } override fun RBuilder.render() { div { +"count: ${state.count}" } button { attrs.onClickFunction = { onClickedButton() } +"click!" } } } ΧϯτͷදࣔͱίʔϧόοΫͷࢦఆ ˠBUUSTPO$MJDL'VODUJPO\^ ಈ͖Λ͚͍ͭͨ ྫΧϯλʔ -
ελΠϧΛ͚͍ͭͨ DTTJO,PUMJO override fun RBuilder.render() { div { +"count: ${state.count}"
} styledButton { css { width = 200.px height = 100.px backgroundColor = Color.aqua } attrs.onClickFunction = { onClickedButton() } +"click!" } } -
ελΠϧΛ͚͍ͭͨ DTTJO,PUMJO override fun RBuilder.render() { div { +"count: ${state.count}"
} styledButton { css { width = 200.px height = 100.px backgroundColor = Color.aqua } attrs.onClickFunction = { onClickedButton() } +"click!" } } TUZMFE#VUUPOʹ͢Δͱ DTT IBOEMFS3VMF4FU ϝιου͕ੜ͑Δ -
ελΠϧΛ͚͍ͭͨ DTTJO,PUMJO override fun RBuilder.render() { div { +"count: ${state.count}"
} styledButton { css { width = 200.px height = 100.px backgroundColor = Color.aqua } attrs.onClickFunction = { onClickedButton() } +"click!" } } DTTϒϩοΫͷதͰελΠϧΛهड़ ˞TUZMFEDPNQPOFOUͷػೳ -
+4ϥΠϒϥϦͷಋೖ ґଘؔՃ ྫSFBDUTXJQFBCMFWJFXT w OQNͷ߹ npm install react-swipeable-views --save
w (SBEMFͷ߹CVJMEHSBEMFʹه sourceSets { main { dependencies { ... implementation npm("react-swipeable-views") ... } } } -
+4ϥΠϒϥϦͷಋೖ !+T.PEVMFΞϊςʔγϣϯ w +4ϥΠϒϥϦΛ,PUMJOͷੈքʹΠϯϙʔτ͢ΔΞϊςʔγϣϯ @JsModule("react-swipeable-views") private external val reactSwipeableViewModule: dynamic
fun test() { reactSwipeableViewModule.default.onChangeIndex = { ... } } -
+4ϥΠϒϥϦͷಋೖ !+T.PEVMFΞϊςʔγϣϯ w +4ϥΠϒϥϦΛ,PUMJOͷੈքʹΠϯϙʔτ͢ΔΞϊςʔγϣϯ @JsModule("react-swipeable-views") private external val reactSwipeableViewModule: dynamic
fun test() { reactSwipeableViewModule.default.onChangeIndex = { ... } } +4ϥΠϒϥϦΛΠϯϙʔτ PO$IBOHF*OEFYʹ ϥϜμࣜΛೖ Ξϊςʔγϣϯ͚ͭͩʂ +4ϥΠϒϥϦΛ,PUMJOͷੈքͰ ҰԠ ѻ͑Δʂ -
+4ϥΠϒϥϦͷಋೖ !+T.PEVMFΞϊςʔγϣϯ w +4ϥΠϒϥϦΛ,PUMJOͷੈքʹΠϯϙʔτ͢ΔΞϊςʔγϣϯ @JsModule("react-swipeable-views") private external val reactSwipeableViewModule: dynamic
fun test() { reactSwipeableViewModule.default.onChangeIndex = { ... } } EZOBNJDܕʜʁ -
+4ϥΠϒϥϦͷಋೖ EZOBNJDܕ w ,PUMJOͱ+4ͷڮ͠Λ୲͏ܕ w ,PUMJOͷੈքͰ+4ͷΦϒδΣΫτΛͦͷ··දݱ͢Δ͜ͱ͕Ͱ͖Δ // { foo: 'foo',
bar: { hoge: 111 } }をKotlinで扱う場合 val jsObject: dynamic = js("{ foo: 'foo', bar: { hoge: 111 } }") print(jsObject["foo"]) // => 'foo' print(jsObject["bar"]["hoge"]) // => 111 jsObject["aaa"]["bbb"] = "add from kotlin" print(jsObject["aaa"]["bbb"]) // => add from kotlin -
+4ϥΠϒϥϦͷಋೖ EZOBNJDܕ w ,PUMJOͱ+4ͷڮ͠Λ୲͏ܕ w ,PUMJOͷੈքͰ+4ͷΦϒδΣΫτΛͦͷ··දݱ͢Δ͜ͱ͕Ͱ͖Δ // { foo: 'foo',
bar: { hoge: 111 } }をKotlinで扱う場合 val jsObject: dynamic = js("{ foo: 'foo', bar: { hoge: 111 } }") print(jsObject["foo"]) // => 'foo' print(jsObject["bar"]["hoge"]) // => 111 jsObject["aaa"]["bbb"] = "add from kotlin" print(jsObject["aaa"]["bbb"]) // => add from kotlin +4ͷΦϒδΣΫτΛੜ -
+4ϥΠϒϥϦͷಋೖ EZOBNJDܕ w ,PUMJOͱ+4ͷڮ͠Λ୲͏ܕ w ,PUMJOͷੈքͰ+4ͷΦϒδΣΫτΛͦͷ··දݱ͢Δ͜ͱ͕Ͱ͖Δ // { foo: 'foo',
bar: { hoge: 111 } }をKotlinで扱う場合 val jsObject: dynamic = js("{ foo: 'foo', bar: { hoge: 111 } }") print(jsObject["foo"]) // => 'foo' print(jsObject["bar"]["hoge"]) // => 111 jsObject["aaa"]["bbb"] = "add from kotlin" print(jsObject["aaa"]["bbb"]) // => add from kotlin ଘࡏ͢ΔΩʔͷΛऔͬͯ͘Δ -
+4ϥΠϒϥϦͷಋೖ EZOBNJDܕ w ,PUMJOͱ+4ͷڮ͠Λ୲͏ܕ w ,PUMJOͷੈքͰ+4ͷΦϒδΣΫτΛͦͷ··දݱ͢Δ͜ͱ͕Ͱ͖Δ // { foo: 'foo',
bar: { hoge: 111 } }をKotlinで扱う場合 val jsObject: dynamic = js("{ foo: 'foo', bar: { hoge: 111 } }") print(jsObject["foo"]) // => 'foo' print(jsObject["bar"]["hoge"]) // => 111 jsObject["aaa"]["bbb"] = "add from kotlin" print(jsObject["aaa"]["bbb"]) // => add from kotlin ଘࡏ͠ͳ͍ΩʔͷΛ +4ʹ͍ۙจ๏ͰೖͰ͖ͯ͠·͏ -
+4ϥΠϒϥϦͷಋೖ EZOBNJDܕ w ,PUMJOͱ+4ͷڮ͠Λ୲͏ܕ w ,PUMJOͷੈքͰ+4ͷΦϒδΣΫτΛͦͷ··දݱ͢Δ͜ͱ͕Ͱ͖Δ // { foo: 'foo',
bar: { hoge: 111 } }をKotlinで扱う場合 val jsObject: dynamic = js("{ foo: 'foo', bar: { hoge: 111 } }") print(jsObject.foo) // => 'foo' print(jsObject.bar.hoge) // => 111 jsObject.aaa.bbb = "add from kotlin" // => Error! ͜͏ͳͬͯཉ͍͠ -
FYUFSOBMम০ࢠ w ,PUMJOͷܕఆٛͱ+4ͷΦϒδΣΫτΛϚοϐϯά͢Δम০ࢠ external interface TestObject { var foo: String
var bar: TestChildObject } external interface TestChildObject { var hoge: String } val jsObject: TestObject = js("{ foo: 'foo', bar: { hoge: '111' } }") print(jsObject.foo) // => 'foo' print(jsObject.bar.hoge) // => 111 5ZQF4DSJQUͷܕఆٛϑΝΠϧͱ ׂಉ͡ +4ϥΠϒϥϦͷಋೖ -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ @JsModule("react-swipeable-views") private external val
reactSwipeableViewModule: dynamic external interface ReactSwipeableProps : RProps { var onChangeIndex: (Int, Int, ChangeReason) -> Unit } val RBuilder.swipeableViews: RClass<ReactSwipeableProps> get() = reactSwipeableViewModule.default as RClass<ReactSwipeableProps> -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ @JsModule("react-swipeable-views") private external val
reactSwipeableViewModule: dynamic external interface ReactSwipeableProps : RProps { var onChangeIndex: (Int, Int, ChangeReason) -> Unit } val RBuilder.swipeableViews: RClass<ReactSwipeableProps> get() = reactSwipeableViewModule.default as RClass<ReactSwipeableProps> FYUFSOBMJOUFSGBDFΛఆٛ ˠϓϩύςΟʮPO$IBOHF*OEFYʯ͕ ɹଘࡏ͢Δ͜ͱΛڭ͑ࠐ·ͤΔ -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ @JsModule("react-swipeable-views") private external val
reactSwipeableViewModule: dynamic external interface ReactSwipeableProps : RProps { var onChangeIndex: (Int, Int, ChangeReason) -> Unit } val RBuilder.swipeableViews: RClass<ReactSwipeableProps> get() = reactSwipeableViewModule.default as RClass<ReactSwipeableProps> Πϯϙʔτͨ͠ϞδϡʔϧΛ3$MBTT31SPQTʹΩϟετ -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ fun ReactSwipeableProps.onChangeIndex(block: (Int) ->
Unit) { onChangeIndex = { index: Int, _, _ -> block(index) } } // RComponentクラス内 override fun RBuilder.render() { swipeableViews { attrs.onChangeIndex { i -> setState { tabNumber = i } } } } -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ fun ReactSwipeableProps.onChangeIndex(block: (Int) ->
Unit) { onChangeIndex = { index: Int, _, _ -> block(index) } } // RComponentクラス内 override fun RBuilder.render() { swipeableViews { attrs.onChangeIndex { i -> setState { tabNumber = i } } } } 1SPQTͷ֦ுؔͱͯ͠ ηολʔΛఆٛ -
+4ϥΠϒϥϦΛʮ,PUMJOΒ͘͠ʯѻ͏ʹ w FYUFSOBMम০ࢠΛ͚ͭͨΠϯλʔϑΣʔεͰܕఆٛ w +4ͷؔΛ,PUMJOΒ͘͠ݺΔΑ͏ʹؔఆٛ +4ϥΠϒϥϦͷಋೖ fun ReactSwipeableProps.onChangeIndex(block: (Int) ->
Unit) { onChangeIndex = { index: Int, _, _ -> block(index) } } // RComponentクラス内 override fun RBuilder.render() { swipeableViews { attrs.onChangeIndex { i -> setState { tabNumber = i } } } } BUUSTϓϩύςΟ͔Β1SPQT͕ੜ͑Δ ˠ֦ுؔݺͼग़͠ -
ܕఆٛɾؔఆٛʹ͔͔Δ࿑ྗ w ϥΠϒϥϦʹ͖ͭɺଟͯ͘ߦఔ w ඞཁͳྔ͚ͩఆٛ͢Ε0, ͨͩ͠ w ڊେͳϥΠϒϥϦͷܕఆٛɾؔఆٛਖ਼͔ͳΓ͠ΜͲ͍ w 6*ϑϨʔϜϫʔΫಛʹ
τϥΠΞϯυΤϥʔͷαΠΫϧ͕ճͮ͠Β͍ w ྫ3FBDU#PPUTUSBQɺ.BUFSJBM6*FUD +4ϥΠϒϥϦͷಋೖ -
Ͳ͏ͯ͠ॻ͖ํ͕͔Βͳ͍ʂ w KT ϝιουҾʹੜͷ+4ίʔυΛจࣈྻͰ͢͜ͱ͕Ͱ͖Δ w +4ΛͱΓ͋͑ͣίϐϖˠগͣͭ͠,PUMJOʹม +4ϥΠϒϥϦͷಋೖ inline fun
Window.initMap() = js("""new window.google.maps.Map( document.getElementById('googleMap'), { center: { lat: 34.740864, lng: 139.391152 }, zoom: 11.3, mapTypeId: 'roadmap' } )""") ྫ(PPHMF.BQ"1*ͷॳظԽίʔυ -
·ͱΊ ,PUMJO+4ͷ֓ཁɾΞοϓσʔτͷྺ࢙ʹ͍ͭͯղઆ w ॳಠཱͨ͠ϓϥάΠϯ w ݱࡏ,PUMJO.11ͷҰཌྷΛ୲͏ཱͪҐஔ ,PUMJO+4Λͬͨ8FCΞϓϦ࡞ͷϋϯζΦϯ w OQNͱ(SBEMFɺͲͪΒͰՄ w
EZOBNJDͱFYUFSOBM͕͍͜ͳͤΕɺ+4ϥΠϒϥϦΛࣗ༝ʹѻ͑Δ w ࠷ऴखஈͱͯ͠KTϝιου -
·ͱΊ ,PUMJO+4ͷ͜Ε͔Β w CBDLFOEͱGSPOUFOE͕ͭͷ,PUMJOϓϩδΣΫτʹ·ͱ·Δʂ w EBUBDMBTTͷڞ༗ɺ(SBEMFͰͷϏϧυɺ%4-ͰͷIUNMDTTهड़ ຊ൪ಋೖͷน w ѹతͳࣄྫɾใෆʂɹϥΠϒϥϦ·ͩ·ͩগͳ͍ʂ w
؆୯ͳϞϊͰ࡞ͬͯެ։ˠݟͷੵɺ,PUMJO+4ͷલਐͷҰॿ ,PUMJOͰϑϩϯτΤϯυͷ։ൃ͕Ͱ͖Δͷָ͍͠ʂ ࢁͷਓ͕৮ͬͯ͘ΕΔͱخ͍͠☺ -
ίʔυϦʔσΟϯά LPUMJOSFBDU ʮ)FMMP 8PSMEʯΛදࣔ͢ΔαϯϓϧΞϓϦ class App : RComponent<RProps, RState>() {
override fun RBuilder.render() { div { +"Hello, World!" } } } 3$PNQPOFOUʁ 3#VJMEFSʁ SFOEFSϝιου ͳ֦ͥுؔʁ -
ίʔυϦʔσΟϯά LPUMJOSFBDU LPUMJOSFBDUͷසग़Ϋϥε w 3$MBTT ˠ3FBDU$MBTTͱରԠɺίϯϙʔωϯτͷܕఆٛ w 3$PNQPOFOU3FBDU&MFNFOU ˠ$PNQPOFOU3FBDU&MFNFOUͱରԠɺίϯϙʔωϯτͷΠϯελϯε w
3#VJMEFS ˠ%0.ߏஙɾ$PNQPOFOUͷੜʹΛ࣋ͭLPUMJOSFBDUಠࣗͷΫϥε -
ίʔυϦʔσΟϯά LPUMJOSFBDU @ReactDsl open class RBuilder { val childList =
mutableListOf<Any>() fun child(element: ReactElement): ReactElement { childList.add(element) return element } operator fun String.unaryPlus() { childList.add(this) } fun <P : RProps> child(type: Any, props: P, children: List<Any>) = child(createElement(type, props, *children.toTypedArray())) fun <P : RProps> child(type: Any, props: P, handler: RHandler<P>): ReactElement { val children = with(RElementBuilder(props)) { handler() childList } return child(type, props, children) } operator fun <P : RProps> RClass<P>.invoke(handler: RHandler<P>) = child(this, jsObject {}, handler) operator fun <T> RProvider<T>.invoke(value: T, handler: RHandler<RProviderProps<T>>) = child(this, jsObject { this.value = value }, handler) operator fun <T> RConsumer<T>.invoke(handler: RBuilder.(T) -> Unit) = child(this, jsObject<RConsumerProps<T>> { this.children = { value -> buildElements { handler(value) } } }) {} fun <P : RProps> RClass<P>.node( props: P, children: List<Any> = emptyList() ) = child(this, clone(props), children) fun <P : RProps, C : Component<P, *>> child(klazz: KClass<C>, handler: RHandler<P>): ReactElement { val rClass = klazz.js as RClass<P> return rClass(handler) } inline fun <P : RProps, reified C : Component<P, *>> child(noinline handler: RHandler<P>) = child(C::class, handler) fun <P : RProps, C : Component<P, *>> node( klazz: KClass<C>, props: P, children: List<Any> = emptyList() ): ReactElement { val rClass = klazz.js as RClass<P> return rClass.node(props, children) } inline fun <P : RProps, reified C : Component<P, *>> node(props: P, children: List<Any> = emptyList()) = node(C::class, props, children) fun RProps.children() { childList.addAll(Children.toArray(children)) } } 3#VJMEFSΫϥε -
ίʔυϦʔσΟϯά LPUMJOSFBDU 3#VJMEFSΫϥε w %0.ཁૉΛ֨ೲ͢ΔDIJME-JTUϓϩύςΟΛ࣋ͭ @ReactDsl open class RBuilder {
val childList = mutableListOf<Any>() fun child(element: ReactElement): ReactElement { childList.add(element) return element } operator fun String.unaryPlus() { childList.add(this) } fun <P : RProps> child(type: Any, props: P, children: List<Any>) = child(createElement(type, props, *children.toTypedArray())) fun <P : RProps> child(type: Any, props: P, handler: RHandler<P>): ReactElement { val children = with(RElementBuilder(props)) { handler() childList } return child(type, props, children) } operator fun <P : RProps> RClass<P>.invoke(handler: RHandler<P>) = child(this, jsObject {}, handler) operator fun <T> RProvider<T>.invoke(value: T, handler: RHandler<RProviderProps<T>>) = child(this, jsObject { this.value = value }, handler) operator fun <T> RConsumer<T>.invoke(handler: RBuilder.(T) -> Unit) = child(this, jsObject<RConsumerProps<T>> { this.children = { value -> buildElements { handler(value) } } }) {} fun <P : RProps> RClass<P>.node( props: P, children: List<Any> = emptyList() ) = child(this, clone(props), children) fun <P : RProps, C : Component<P, *>> child(klazz: KClass<C>, handler: RHandler<P>): ReactElement { val rClass = klazz.js as RClass<P> return rClass(handler) } inline fun <P : RProps, reified C : Component<P, *>> child(noinline handler: RHandler<P>) = child(C::class, handler) fun <P : RProps, C : Component<P, *>> node( klazz: KClass<C>, props: P, children: List<Any> = emptyList() ): ReactElement { val rClass = klazz.js as RClass<P> return rClass.node(props, children) } inline fun <P : RProps, reified C : Component<P, *>> node(props: P, children: List<Any> = emptyList()) = node(C::class, props, children) fun RProps.children() { childList.addAll(Children.toArray(children)) } } @ReactDsl open class RBuilder { val childList = mutableListOf<Any>() -
ίʔυϦʔσΟϯά LPUMJOSFBDU 3#VJMEFSΫϥε w %0.ཁૉΛ֨ೲ͢ΔDIJME-JTUϓϩύςΟΛ࣋ͭ w DIJMEϝιουΛͬͯཁૉΛՃͰ͖Δ @ReactDsl open class
RBuilder { val childList = mutableListOf<Any>() fun child(element: ReactElement): ReactElement { childList.add(element) return element } operator fun String.unaryPlus() { childList.add(this) } fun <P : RProps> child(type: Any, props: P, children: List<Any>) = child(createElement(type, props, *children.toTypedArray())) fun <P : RProps> child(type: Any, props: P, handler: RHandler<P>): ReactElement { val children = with(RElementBuilder(props)) { handler() childList } return child(type, props, children) } operator fun <P : RProps> RClass<P>.invoke(handler: RHandler<P>) = child(this, jsObject {}, handler) operator fun <T> RProvider<T>.invoke(value: T, handler: RHandler<RProviderProps<T>>) = child(this, jsObject { this.value = value }, handler) operator fun <T> RConsumer<T>.invoke(handler: RBuilder.(T) -> Unit) = child(this, jsObject<RConsumerProps<T>> { this.children = { value -> buildElements { handler(value) } } }) {} fun <P : RProps> RClass<P>.node( props: P, children: List<Any> = emptyList() ) = child(this, clone(props), children) fun <P : RProps, C : Component<P, *>> child(klazz: KClass<C>, handler: RHandler<P>): ReactElement { val rClass = klazz.js as RClass<P> return rClass(handler) } inline fun <P : RProps, reified C : Component<P, *>> child(noinline handler: RHandler<P>) = child(C::class, handler) fun <P : RProps, C : Component<P, *>> node( klazz: KClass<C>, props: P, children: List<Any> = emptyList() ): ReactElement { val rClass = klazz.js as RClass<P> return rClass.node(props, children) } inline fun <P : RProps, reified C : Component<P, *>> node(props: P, children: List<Any> = emptyList()) = node(C::class, props, children) fun RProps.children() { childList.addAll(Children.toArray(children)) } } fun child(element: ReactElement): ReactElement { childList.add(element) return element } -
@ReactDsl open class RBuilder { val childList = mutableListOf<Any>() fun
child(element: ReactElement): ReactElement { childList.add(element) return element } operator fun String.unaryPlus() { childList.add(this) } fun <P : RProps> child(type: Any, props: P, children: List<Any>) = child(createElement(type, props, *children.toTypedArray())) fun <P : RProps> child(type: Any, props: P, handler: RHandler<P>): ReactElement { val children = with(RElementBuilder(props)) { handler() childList } return child(type, props, children) } operator fun <P : RProps> RClass<P>.invoke(handler: RHandler<P>) = child(this, jsObject {}, handler) operator fun <T> RProvider<T>.invoke(value: T, handler: RHandler<RProviderProps<T>>) = child(this, jsObject { this.value = value }, handler) operator fun <T> RConsumer<T>.invoke(handler: RBuilder.(T) -> Unit) = child(this, jsObject<RConsumerProps<T>> { this.children = { value -> buildElements { handler(value) } } }) {} fun <P : RProps> RClass<P>.node( props: P, children: List<Any> = emptyList() ) = child(this, clone(props), children) fun <P : RProps, C : Component<P, *>> child(klazz: KClass<C>, handler: RHandler<P>): ReactElement { val rClass = klazz.js as RClass<P> return rClass(handler) } inline fun <P : RProps, reified C : Component<P, *>> child(noinline handler: RHandler<P>) = child(C::class, handler) fun <P : RProps, C : Component<P, *>> node( klazz: KClass<C>, props: P, children: List<Any> = emptyList() ): ReactElement { val rClass = klazz.js as RClass<P> return rClass.node(props, children) } inline fun <P : RProps, reified C : Component<P, *>> node(props: P, children: List<Any> = emptyList()) = node(C::class, props, children) fun RProps.children() { childList.addAll(Children.toArray(children)) } } ίʔυϦʔσΟϯά LPUMJOSFBDU 3#VJMEFSΫϥε w %0.ཁૉΛ֨ೲ͢ΔDIJME-JTUϓϩύςΟΛ࣋ͭ w DIJMEϝιουɺଞʹͭ w 3$PNQPOFOUΛՃ w QSPQTͱࢠཁૉΛݸผͷҾͰՃFUD fun child(type: Any, props: P, children: List<Any>): ReactElement fun child(type: Any, props: P, handler: RHandler<P>): ReactElement fun child(klazz: KClass<C>, handler: RHandler<P>): ReactElement inline fun child(noinline handler: RHandler<P>): ReactElement -
ίʔυϦʔσΟϯά LPUMJOSFBDU DSFBUF&MFNFOUϝιου w τοϓϨϕϧؔɺ3FBDUKTͷ3FBDUDSFBUF&MFNFOUͱରԠ w 3#VJMEFSDIJME-JTU3FBDU&MFNFOUʹม w SFOEFSϝιουͰ࠷ऴతʹݺΕΔ external
fun <P : RProps> createElement( type: Any, props: P, vararg child: Any? ): ReactElement -
ίʔυϦʔσΟϯά LPUMJOSFBDU EJWϝιου w 3#VJMEFSͷ֦ுؔͱͯ͠ఆٛ w ҾCMPDLͷܕ3%0.#VJMEFS%*7 6OJU w CMPDLͷதͰEJWͦͷଞ)UNM5BHͷϝιουΛ࠶ؼతʹهड़Ͱ͖Δʂ
w )UNM5BH༻ϝιουશ֦ͯுؔˠ3#VJMEFS͕ͪ͝Ό͔ͭͳ͍ʂ inline fun RBuilder.div( classes: String? = null, block: RDOMBuilder<DIV>.() -> Unit ): ReactElement = tag(block) { DIV(attributesMapOf("class", classes), it) } 3%0.#VJMEFS ˠ3#VJMEFSͷࢠΫϥε EJW\ TQBO\ B\^ ^ ^ -
ίʔυϦʔσΟϯά LPUMJOSFBDU UBHϝιου w )UNM5BH༻ϝιου͔Β࣮ࡍʹݺͼग़͞ΕΔϝιου w 3#VJMEFSDIJME ϝιουΛ࣮ߦ w
,PUMJO+4Ͱಠࣗ)5.-λάΛѻ͍͍ͨʂˠUBHϝιουͰΑ͠ͳʹ inline fun <T : Tag> RBuilder.tag( block: RDOMBuilder<T>.() -> Unit, noinline factory: (TagConsumer<Unit>) -> T ): ReactElement = child(RDOMBuilder(factory).apply { block() }.create()) -
ίʔυϦʔσΟϯά LPUMJOSFBDU ,PUMJO+4ͷίʔσΟϯά w SFOEFSϝιου͕3#VJMEFSͷ֦ுؔͱͯ͠ఆٛ ˠSFOEFSϝιουͷείʔϓɺUIJT͕3#VJMEFSͷΠϯελϯεͱͳΔ ˠ,PUMJOΒ͘͠)5.-ͷݟͨΛଛͳΘͳ͍ܗͰ)5.-Λॻ͚Δʂ class App :
RComponent<RProps, RState>() { override fun RBuilder.render() { div { +"Hello, World!" } } } ֦ுؔ ϥϜμࣜͷޮՌతͳ࣮༻ྫ -
ίʔυϦʔσΟϯά LPUMJOSFBDU ,PUMJO+4ͷίʔσΟϯά w SFOEFSϝιου͕3#VJMEFSͷ֦ுؔͱͯ͠ఆٛ ˠSFOEFSϝιουͷείʔϓɺUIJT͕3#VJMEFSͷΠϯελϯεͱͳΔ ˠ,PUMJOΒ͘͠)5.-ͷݟͨΛଛͳΘͳ͍ܗͰ)5.-Λॻ͚Δʂ class App :
RComponent<RProps, RState>() { override fun RBuilder.render() { div { +"Hello, World!" } } } EJW\DIJME )FMMP 8PSME ^ͱಉ͡ Ͱݟͨ͜ͱͳ͍ॻ͖ํʜ -
ίʔυϦʔσΟϯά LPUMJOSFBDU 3#VJMEFSΫϥε w 4USJOHVOBSZ1MVT ϝιου w ԋࢉࢠΦʔόʔϩʔυ ˠจࣈྻͷ୯߲ԋࢉ IPHF
Λ্ॻ͖ w 3#VJMEFSΫϥεͷ෦Ͱએݴ ɹˠUIJT͕3#VJMEFSͷ࣌ͷΈ༗ޮ ɹɹར༻ൣғΛݶఆ͠ɺ෭࡞༻Λ͑Δʂ @ReactDsl open class RBuilder { val childList = mutableListOf<Any>() fun child(element: ReactElement): ReactElement { childList.add(element) return element } operator fun String.unaryPlus() { childList.add(this) } fun <P : RProps> child(type: Any, props: P, children: List<Any>) = child(createElement(type, props, *children.toTypedArray())) fun <P : RProps> child(type: Any, props: P, handler: RHandler<P>): ReactElement { val children = with(RElementBuilder(props)) { handler() childList } return child(type, props, children) } operator fun <P : RProps> RClass<P>.invoke(handler: RHandler<P>) = child(this, jsObject {}, handler) operator fun <T> RProvider<T>.invoke(value: T, handler: RHandler<RProviderProps<T>>) = child(this, jsObject { this.value = value }, handler) operator fun <T> RConsumer<T>.invoke(handler: RBuilder.(T) -> Unit) = child(this, jsObject<RConsumerProps<T>> { this.children = { value -> buildElements { handler(value) } } }) {} fun <P : RProps> RClass<P>.node( props: P, children: List<Any> = emptyList() ) = child(this, clone(props), children) fun <P : RProps, C : Component<P, *>> child(klazz: KClass<C>, handler: RHandler<P>): ReactElement { val rClass = klazz.js as RClass<P> return rClass(handler) } inline fun <P : RProps, reified C : Component<P, *>> child(noinline handler: RHandler<P>) = child(C::class, handler) fun <P : RProps, C : Component<P, *>> node( klazz: KClass<C>, props: P, children: List<Any> = emptyList() ): ReactElement { val rClass = klazz.js as RClass<P> return rClass.node(props, children) } inline fun <P : RProps, reified C : Component<P, *>> node(props: P, children: List<Any> = emptyList()) = node(C::class, props, children) fun RProps.children() { childList.addAll(Children.toArray(children)) } } operator fun String.unaryPlus() { childList.add(this) }