Skip to content

Commit 40931ac

Browse files
authored
UserTable Improvements
Improve UserTable
2 parents d8322a0 + c870b58 commit 40931ac

File tree

10 files changed

+97
-100
lines changed

10 files changed

+97
-100
lines changed

build.gradle.kts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,20 @@ plugins {
22
kotlin("js") version "1.4.21"
33
}
44

5-
group = "com.github.aerialist7"
6-
version = "1.0.0-SNAPSHOT"
7-
85
repositories {
96
jcenter()
10-
mavenCentral()
11-
maven { url = uri("https://dl.bintray.com/kotlin/kotlin-js-wrappers") }
127
}
138

149
dependencies {
15-
testImplementation(kotlin("test-js"))
16-
implementation("org.jetbrains:kotlin-react:17.0.1-pre.136-kotlin-1.4.10")
17-
implementation("org.jetbrains:kotlin-react-dom:17.0.1-pre.136-kotlin-1.4.10")
18-
implementation("org.jetbrains:kotlin-react-table:7.6.2-pre.136-kotlin-1.4.10")
19-
implementation("org.jetbrains:kotlin-styled:5.2.0-pre.136-kotlin-1.4.10")
10+
implementation("org.jetbrains:kotlin-react:17.0.1-pre.137-kotlin-1.4.21")
11+
implementation("org.jetbrains:kotlin-react-dom:17.0.1-pre.137-kotlin-1.4.21")
12+
implementation("org.jetbrains:kotlin-react-table:7.6.3-pre.137-kotlin-1.4.21")
13+
implementation("org.jetbrains:kotlin-styled:5.2.0-pre.137-kotlin-1.4.21")
2014
}
2115

22-
kotlin {
23-
js(LEGACY) {
24-
browser {
25-
binaries.executable()
26-
webpackTask {
27-
cssSupport.enabled = true
28-
}
29-
runTask {
30-
cssSupport.enabled = true
31-
}
32-
testTask {
33-
useKarma {
34-
useChromeHeadless()
35-
webpackConfig.cssSupport.enabled = true
36-
}
37-
}
38-
}
16+
kotlin.js {
17+
browser {
18+
binaries.executable()
3919
}
4020
}
4121

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
group=com.github.aerialist7
2+
version=1.0.0-SNAPSHOT
3+
14
kotlin.code.style=official
25
kotlin.js.generate.executable.default=false

src/main/kotlin/Colors.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import kotlinx.css.Color
2+
3+
object Colors {
4+
object Text {
5+
val Black: Color = Color("#2E2E2E")
6+
val Gray: Color = Color("#75829E")
7+
}
8+
9+
object Background {
10+
val Gray: Color = Color("#EDEDF3")
11+
val White: Color = Color("#FFFFFF")
12+
}
13+
14+
object Stroke {
15+
val LightGray: Color = Color("#F4F4F4")
16+
val Gray: Color = Color("#DEE1E9")
17+
}
18+
}

src/main/kotlin/Hooks.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import react.useCallback
2+
import react.useMemo
3+
4+
fun <T> useMemo(
5+
vararg dependencies: dynamic,
6+
callback: () -> T,
7+
): T =
8+
useMemo(callback, dependencies)
9+
10+
fun <T : Function<*>> useCallback(
11+
vararg dependencies: dynamic,
12+
callback: () -> T,
13+
): () -> T =
14+
useCallback(callback, dependencies)

src/main/kotlin/Main.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import kotlinx.browser.document
2+
import react.child
3+
import react.dom.render
4+
5+
fun main() {
6+
render(document.getElementById("root")) {
7+
child(UserTable) {
8+
attrs.data = arrayOf(
9+
User("Victor", 32),
10+
User("Alexander", 26),
11+
User("Catherine", 25),
12+
User("Nicolas", 25),
13+
)
14+
}
15+
}
16+
}

src/main/kotlin/Tags.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import kotlinext.js.Object
2+
import kotlinx.html.Tag
3+
import react.RProps
4+
5+
var Tag.extraAttrs: RProps
6+
@Deprecated(level = DeprecationLevel.HIDDEN, message = "write only")
7+
get() = error("write only")
8+
set(value) {
9+
for (key in Object.keys(value)) {
10+
@Suppress("UnsafeCastFromDynamic")
11+
attributes[key] = value.asDynamic()[key]
12+
}
13+
}

src/main/kotlin/User.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data class User(
2+
val name: String,
3+
val age: Int,
4+
)

src/main/kotlin/table.kt renamed to src/main/kotlin/UserTable.kt

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import kotlinext.js.Object
21
import kotlinx.browser.window
32
import kotlinx.css.*
4-
import kotlinx.html.Tag
53
import kotlinx.html.js.onClickFunction
64
import react.RProps
75
import react.dom.tr
@@ -12,49 +10,51 @@ import react.table.useTable
1210
import styled.*
1311

1412
external interface UserTableProps : RProps {
15-
var headers: Array<String>
16-
var users: Array<User>
13+
var data: Array<out User>
1714
}
1815

19-
data class User(
20-
val name: String,
21-
val age: Int,
22-
)
23-
2416
val UserTable = functionalComponent<UserTableProps> { props ->
25-
val onRowClick = { user: User ->
26-
window.alert(user.name)
17+
val onRowClick = useCallback {
18+
{ user: User -> window.alert(user.name) }
2719
}
2820

29-
val table = useTable<User> {
30-
data = props.users
31-
columns = columns {
21+
val columns = useMemo {
22+
columns<User> {
3223
column<String> {
33-
header = props.headers[0]
24+
header = "Name"
3425
accessorFunction = { it.name }
3526
}
3627
column<Int> {
37-
header = props.headers[1]
28+
header = "Age"
3829
accessorFunction = { it.age }
3930
}
4031
}
4132
}
4233

34+
val table = useTable<User> {
35+
this.data = props.data
36+
this.columns = columns
37+
}
38+
4339
styledDiv {
4440
styledTable {
4541
css {
46-
width = 100.pct
42+
width = 400.px
4743
borderSpacing = 0.px
4844
borderCollapse = BorderCollapse.collapse
4945
whiteSpace = WhiteSpace.nowrap
46+
borderWidth = 2.px
47+
borderStyle = BorderStyle.solid
48+
borderColor = Colors.Stroke.Gray
49+
margin(LinearDimension.auto)
5050
}
5151
attrs {
5252
extraAttrs = table.getTableProps()
5353
}
5454
styledThead {
5555
css {
5656
color = Colors.Text.Gray
57-
fontSize = 12.px
57+
fontSize = 18.px
5858
backgroundColor = Colors.Background.Gray
5959
}
6060

@@ -109,7 +109,7 @@ val UserTable = functionalComponent<UserTableProps> { props ->
109109

110110
styledTr {
111111
css {
112-
fontSize = 14.px
112+
fontSize = 16.px
113113
cursor = Cursor.pointer
114114
borderBottom = solid(Colors.Stroke.LightGray)
115115
hover {
@@ -119,7 +119,7 @@ val UserTable = functionalComponent<UserTableProps> { props ->
119119

120120
attrs {
121121
extraAttrs = row.getRowProps()
122-
onClickFunction = { onRowClick(row.original) }
122+
onClickFunction = { onRowClick()(row.original) }
123123
}
124124

125125
for (cell in row.cells) {
@@ -142,35 +142,8 @@ val UserTable = functionalComponent<UserTableProps> { props ->
142142
}
143143
}
144144

145-
object Colors {
146-
object Text {
147-
val Black: Color = Color("#2E2E2E")
148-
val Gray: Color = Color("#75829E")
149-
}
150-
151-
object Background {
152-
val Gray: Color = Color("#EDEDF3")
153-
val White: Color = Color("#FFFFFF")
154-
}
155-
156-
object Stroke {
157-
val LightGray: Color = Color("#F4F4F4")
158-
val Gray: Color = Color("#DEE1E9")
159-
}
160-
}
161-
162-
fun solid(
145+
private fun solid(
163146
color: Color,
164147
thickness: Int = 1,
165148
): String =
166149
"${thickness}px solid $color"
167-
168-
var Tag.extraAttrs: RProps
169-
@Deprecated(level = DeprecationLevel.HIDDEN, message = "write only")
170-
get() = error("write only")
171-
set(value) {
172-
for (key in Object.keys(value)) {
173-
@Suppress("UnsafeCastFromDynamic")
174-
attributes[key] = value.asDynamic()[key]
175-
}
176-
}

src/main/kotlin/client.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/resources/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>Kotlin React Table</title>
66
</head>
77
<body>
8-
<script src="kotlin-react-table-sample.js"></script>
98
<div id="root"></div>
9+
<script src="kotlin-react-table-sample.js"></script>
1010
</body>
1111
</html>

0 commit comments

Comments
 (0)