Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for checked Attribute in BSSwitch Component #24

Open
Zoran-Janjic opened this issue Jul 29, 2024 · 2 comments
Open

Add Support for checked Attribute in BSSwitch Component #24

Zoran-Janjic opened this issue Jul 29, 2024 · 2 comments

Comments

@Zoran-Janjic
Copy link

Zoran-Janjic commented Jul 29, 2024

Description
The current implementation of the BSSwitch component in the kotlinbs library uses the defaultChecked attribute to set the initial state of the switch. This approach does not allow for dynamic updates to the switch state after the initial render. As a result, changes to the switch state are not reflected in the UI when the underlying state changes.

Expected Behavior
The BSSwitch component should update its checked state dynamically based on the checked attribute. This will ensure that any changes to the underlying state are accurately reflected in the UI.

Proposed Solution
Modify the BSSwitch component to use the checked attribute instead of defaultChecked. The checked attribute will be passed as a parameter to the BSSwitch function, allowing it to update dynamically.

Current Issue example:

`var isChecked by remember { mutableStateOf(false) }

BSSwitch(
    label = "Example Switch",
    defaultChecked = isChecked,
    onClick = {
        isChecked = it
    }
)
`

Proposed Solution

@Composable
fun BSSwitch(
    modifier: Modifier = Modifier,
    id: String? = null,
    label: String,
    checked: Boolean = false,
    disabled: Boolean = false,
    onClick: (Boolean) -> Unit
) {
    val randomId = remember {
        id ?: UniqueIdGenerator.generateUniqueId("switch")
    }
    Div(
        attrs = modifier
            .classNames("form-check", "form-switch")
            .toAttrs()
    ) {
        Input(
            attrs = Modifier
                .id(randomId)
                .classNames("form-check-input")
                .toAttrs {
                    attr("role", "switch")
                    [email protected](checked)
                    if (disabled) disabled()
                    onClick {
                        onClick((document.getElementById(randomId) as HTMLInputElement).checked)
                    }
                },
            type = InputType.Checkbox
        )
        Label(
            attrs = Modifier
                .classNames("form-check-label")
                .toAttrs(),
            forId = randomId
        ) {
            Text(value = label)
        }
    }
}

Usage of same
var isChecked by remember { mutableStateOf(false) }

BSSwitch(
    label = "Example Switch",
    checked = isChecked,  // This should dynamically update the switch state
    onClick = {
        isChecked = it
    }
)
@stevdza-san
Copy link
Owner

Thanks for your suggestion, I'll take a look at it today!

@stevdza-san
Copy link
Owner

stevdza-san commented Jul 29, 2024

I haven't been able to reproduce the issue with the provided code. When I click the BSSwitch component it does indeed changes/updates it's state. However I may update the name of the parameter from defaultChecked to isChecked or checked. Your solution also works well btw.

var isChecked by remember { mutableStateOf(false) }

BSSwitch(
    label = "Example Switch",
    defaultChecked = isChecked,
    onClick = {
        isChecked = it
    }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants