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

Dmitriy Malayev

03/30/2023, 5:10 PM
Copy code
Hello Everyone I have this checkbox working for select all. But the individual ones don't click. How do I fix it?        


         Box {
            Checkbox {
                onChange = { _, checked ->
                    checkAll = checked

                    console.log(selectedChecksState.forEach { a -> console.log(a) })
                    +"You have selected $selectedChecksState"
                }
            }
            Typography {
                variant = TypographyVariant.button
                +"Select All?"
            }
        }
        Box {
            for (item in <http://Title.ET|Title.ET>) {
                Box {
                    Checkbox {
                        checked = checkAll

                        onChange = { _, checked ->
                            if (checked) {
                                selectedChecksState.add(item)
                            } else if (!checked && selectedChecksState.isNotEmpty())
                                selectedChecksState.remove(item)

                            console.log(selectedChecksState.forEach { a -> console.log(a) })
                            +"You have selected $selectedChecksState"
                        }
                    }
                    +"${item}"
                }
            }
        }
a

andylamax

03/30/2023, 10:53 PM
All of your Checkboxes are showing the same state
checkAll
. Looks like thats not the behavior you want. Try giving every Checkbox its own individual state
d

Dmitriy Malayev

03/30/2023, 10:54 PM
I tried but it's not working
they are interdependant of one another
a

andylamax

03/30/2023, 10:54 PM
what exactly did you try?
d

Dmitriy Malayev

03/30/2023, 10:57 PM
I tried checking in the for loop if checked is true
I tried creating an additional state
I don't understand why I can't have onClick instead of onChange
why does that checkbox run if I don't click it
The for loop creates a checkbox for each string
Can you provide an example please
a

andylamax

03/30/2023, 11:08 PM
I am currently tied in something else, shifting focus to help you debug your code will derail me at the moment
d

Dmitriy Malayev

03/30/2023, 11:09 PM
ok no problem. If you can provide any direction or guidance I'll greatly appreciate it.
Thanks again.
3 Views