https://kotlinlang.org logo
#javascript
Title
# javascript
d

Dmitriy Malayev

10/16/2023, 3:35 AM
I am using kotlin/js. I want to make one of the checkboxes to always be selected and disabled. This is my code.
val SelectGseSectionsComponent = FC<SelectGseSectionsComponentProps> { props ->
val gseReportState = props.gseReportStateProps
var checksState by useState(
mutableMapOf<GseSection, Boolean>().also {
GseSection.entries.forEach { enumValue ->
it[enumValue] = false
it[GseSection.GENERALS] = true
}
}
)
Box {
className = styles.MAIN_GSE_FORM.cssClass
Box {
Checkbox {
onChange = { _, checked ->
checksState = checksState.mapValues { checked } as MutableMap<GseSection, Boolean>
if (checked) {
gseReportState.sections.addAll(GseSection.entries.toTypedArray())
} else {
gseReportState.sections.removeAll(GseSection.entries.toTypedArray())
}
}
}
+"Select All?"
}
Box {
className = styles.GSE_CHECKBOXES.cssClass
checksState.forEach { (label, check) ->
Box {
Checkbox {
checked = check
onChange = { _, checked ->
checksState = checksState.toMutableMap().apply {
put(label, !check)
}
if (checked) {
gseReportState.sections.add(label)
} else {
gseReportState.sections.remove(label)
}
}
}
+label.description
Tooltip {
IconButton {
InfoOutlined()
}
title = ReactNode(<http://label.info|label.info>)
}
}
}
}
}
}
is there a way to add a condition to removeAll and addAll
a

Artem Kobzar

10/16/2023, 12:15 PM
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove-all.html Yes, for
MutableIterable
there is an overload of the
removeAll
with the predicate function
d

Dmitriy Malayev

10/16/2023, 1:58 PM
Thanks Artem. Looks like removeAll has predicate but addAll doesn't I need one of the checkboxes to not change
a

Artem Kobzar

10/16/2023, 3:09 PM
gseReportState.sections.addAll(GseSection.entries.filter { CONDITION() }.toTypedArray())
Will this work for you?
d

Dmitriy Malayev

10/16/2023, 3:09 PM
I'll try this thanks
Copy code
gseReportState.sections.removeAll(GseSections.entries.filter { !it.equals(GENERALS) }
    .toTypedArray())
not working
Box { Checkbox { onChange = { _, checked -> checksState = checksState.mapValues { checked } as MutableMapGseSection, Boolean if (checked) { esgReportState.sections.addAll(GseSection.entries.toTypedArray()) } else { // esgReportState.sections.removeAll { _ -> !GseSection.entries.contains(GseSection.GENERALS) esgReportState.sections.removeAll(GseSection.entries.filter { !it.equals(GENERALS) } .toTypedArray()) // contains(GseSection.GENERALS) // gseReportState.sections.addAll(GseSection.entries.filter { CONDITION() }.toTypedArray()) // // } } } } +"Select All?" }
a

Artem Kobzar

10/16/2023, 3:51 PM
I suggested it only for
addAll
For
removeAll
you could use the overload with a predicate-function param