Skip to content

Commit 7c7820d

Browse files
buttonBuilder for fontAwesomeIcons (#1338)
* buttonBuilder for fontAwesomeIcons ### What's done: * Implemented buttonBuilder for icons * Started to replace existing button tags with buttonBuilders (#1320) Co-authored-by: Peter Trifanov <peter.trifanov@gmail.com>
1 parent fa2c946 commit 7c7820d

3 files changed

Lines changed: 78 additions & 69 deletions

File tree

save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/basic/codeeditor/EditorToolbar.kt

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import com.saveourtool.save.frontend.externals.reactace.AceModes
88
import com.saveourtool.save.frontend.externals.reactace.AceThemes
99
import com.saveourtool.save.frontend.utils.buttonBuilder
1010
import com.saveourtool.save.frontend.utils.selectorBuilder
11-
import com.saveourtool.save.frontend.utils.withUnusedArg
1211
import csstype.ClassName
1312
import react.ChildrenBuilder
14-
import react.dom.html.ButtonType
15-
import react.dom.html.ReactHTML.button
1613
import react.dom.html.ReactHTML.div
1714

1815
private val toolbarCard = cardComponent(isBordered = true)
@@ -54,24 +51,11 @@ fun ChildrenBuilder.displayCodeEditorToolbar(
5451
className = ClassName("input-group")
5552
div {
5653
className = ClassName("input-group-prepend")
57-
58-
button {
59-
type = ButtonType.button
60-
className = ClassName("btn btn-outline-primary")
61-
onClick = onUploadChanges.withUnusedArg()
62-
fontAwesomeIcon(icon = faUpload)
63-
asDynamic()["data-toggle"] = "tooltip"
64-
asDynamic()["data-placement"] = "top"
65-
title = "Save changes on server"
54+
buttonBuilder(faUpload, isOutline = true, title = "Save changes on server") {
55+
onUploadChanges()
6656
}
67-
button {
68-
type = ButtonType.button
69-
className = ClassName("btn btn-outline-primary")
70-
onClick = onReloadChanges.withUnusedArg()
71-
fontAwesomeIcon(icon = faDownload)
72-
asDynamic()["data-toggle"] = "tooltip"
73-
asDynamic()["data-placement"] = "top"
74-
title = "Load changes from server"
57+
buttonBuilder(faDownload, isOutline = true, title = "Save changes on server") {
58+
onReloadChanges()
7559
}
7660
FileType.values().forEach { fileType ->
7761
val buttonStyle = if (hasUncommittedChanges.getValue(fileType)) {
@@ -107,24 +91,11 @@ fun ChildrenBuilder.displayCodeEditorToolbar(
10791
}
10892
div {
10993
className = ClassName("input-group-append")
110-
111-
button {
112-
type = ButtonType.button
113-
className = ClassName("btn btn-outline-info")
114-
onClick = onResultReload.withUnusedArg()
115-
fontAwesomeIcon(icon = faReload)
116-
asDynamic()["data-toggle"] = "tooltip"
117-
asDynamic()["data-placement"] = "top"
118-
title = "Fetch debug info"
94+
buttonBuilder(faReload, style = "info", isOutline = true, title = "Fetch debug info") {
95+
onResultReload()
11996
}
120-
button {
121-
type = ButtonType.button
122-
className = ClassName("btn btn-outline-success")
123-
onClick = onRunExecution.withUnusedArg()
124-
fontAwesomeIcon(icon = faCaretSquareRight)
125-
asDynamic()["data-toggle"] = "tooltip"
126-
asDynamic()["data-placement"] = "top"
127-
title = "Run execution"
97+
buttonBuilder(faCaretSquareRight, style = "success", isOutline = true, title = "Run execution") {
98+
onRunExecution()
12899
}
129100
}
130101
}

save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/SandboxView.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,9 @@ class SandboxView : AbstractView<Props, SandboxViewState>(true) {
198198
role = "alert".unsafeCast<AriaRole>()
199199
div {
200200
displayTestResultDebugInfoStatus(debugInfo)
201-
button {
202-
type = ButtonType.button
203-
className = ClassName("btn btn-link p-0")
204-
+"See more details..."
205-
onClick = {
206-
setState {
207-
isModalOpen = true
208-
}
201+
buttonBuilder("See more details...", "link", classes = "p-0") {
202+
setState {
203+
isModalOpen = true
209204
}
210205
}
211206
}

save-frontend/src/main/kotlin/com/saveourtool/save/frontend/utils/ChildrenBuilderUtils.kt

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package com.saveourtool.save.frontend.utils
88

9+
import com.saveourtool.save.frontend.externals.fontawesome.FontAwesomeIconModule
10+
import com.saveourtool.save.frontend.externals.fontawesome.fontAwesomeIcon
911
import csstype.ClassName
1012
import dom.html.HTMLButtonElement
1113
import dom.html.HTMLSelectElement
@@ -29,16 +31,16 @@ enum class ConfirmationType {
2931
}
3032

3133
/**
32-
* Function to create buttons for modals
34+
* Function to create buttons for modals with text as content
3335
*
3436
* @param label text that will be displayed on button
3537
* @param style color-defining string
3638
* @param isDisabled flag that might disable button
3739
* @param isOutline flag that defines either usual button or outlined will be displayed
3840
* @param isActive flag that defines whether button should be displayed as pressed or not
3941
* @param classes additional classes for button
40-
* @param onClickFun button click handler
4142
* @param title title for tooltip
43+
* @param onClickFun button click handler
4244
*/
4345
@Suppress("TOO_MANY_PARAMETERS", "LongParameterList")
4446
fun ChildrenBuilder.buttonBuilder(
@@ -51,28 +53,33 @@ fun ChildrenBuilder.buttonBuilder(
5153
title: String? = null,
5254
onClickFun: MouseEventHandler<HTMLButtonElement>,
5355
) {
54-
button {
55-
type = ButtonType.button
56-
val outline = if (isOutline) {
57-
"outline-"
58-
} else {
59-
""
60-
}
61-
val active = if (isActive) {
62-
"active"
63-
} else {
64-
""
65-
}
66-
className = ClassName("btn btn-$outline$style $active $classes")
67-
disabled = isDisabled
68-
onClick = onClickFun
69-
title?.let {
70-
asDynamic()["data-toggle"] = "tooltip"
71-
asDynamic()["data-placement"] = "top"
72-
}
73-
this.title = title
74-
+label
75-
}
56+
buttonBuilder({ +label }, style, isDisabled, isOutline, isActive, classes, title, onClickFun)
57+
}
58+
59+
/**
60+
* Function to create buttons for modals with icon as content
61+
*
62+
* @param icon icon that will be displayed on button
63+
* @param style color-defining string
64+
* @param isDisabled flag that might disable button
65+
* @param isOutline flag that defines either usual button or outlined will be displayed
66+
* @param isActive flag that defines whether button should be displayed as pressed or not
67+
* @param classes additional classes for button
68+
* @param title title for tooltip
69+
* @param onClickFun button click handler
70+
*/
71+
@Suppress("TOO_MANY_PARAMETERS", "LongParameterList")
72+
fun ChildrenBuilder.buttonBuilder(
73+
icon: FontAwesomeIconModule,
74+
style: String = "primary",
75+
isDisabled: Boolean = false,
76+
isOutline: Boolean = false,
77+
isActive: Boolean = false,
78+
classes: String = "",
79+
title: String? = null,
80+
onClickFun: MouseEventHandler<HTMLButtonElement>,
81+
) {
82+
buttonBuilder({ fontAwesomeIcon(icon) }, style, isDisabled, isOutline, isActive, classes, title, onClickFun)
7683
}
7784

7885
/**
@@ -104,3 +111,39 @@ fun ChildrenBuilder.selectorBuilder(
104111
}
105112
}
106113
}
114+
115+
@Suppress("TOO_MANY_PARAMETERS", "LongParameterList", "LAMBDA_IS_NOT_LAST_PARAMETER")
116+
private fun ChildrenBuilder.buttonBuilder(
117+
labelBuilder: ChildrenBuilder.() -> Unit,
118+
style: String = "primary",
119+
isDisabled: Boolean = false,
120+
isOutline: Boolean = false,
121+
isActive: Boolean = false,
122+
classes: String = "",
123+
title: String? = null,
124+
onClickFun: MouseEventHandler<HTMLButtonElement>,
125+
) {
126+
button {
127+
type = ButtonType.button
128+
val builtClasses = buildString {
129+
append("btn btn-")
130+
if (isOutline) {
131+
append("outline-")
132+
}
133+
append(style)
134+
if (isActive) {
135+
append(" active")
136+
}
137+
append(" $classes")
138+
}
139+
className = ClassName(builtClasses)
140+
disabled = isDisabled
141+
onClick = onClickFun
142+
title?.let {
143+
asDynamic()["data-toggle"] = "tooltip"
144+
asDynamic()["data-placement"] = "top"
145+
this.title = title
146+
}
147+
labelBuilder()
148+
}
149+
}

0 commit comments

Comments
 (0)