From e2391b55b246ca769b17f5cd1476988a09108768 Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Thu, 14 Sep 2023 15:02:31 +0530 Subject: [PATCH] make radio button on item change non-nullable --- .../common/screens/RadioButtonInputScreen.kt | 7 ++++++ .../ui/designsystem/component/RadioButton.kt | 6 ++--- .../component/RadioButtonInput.kt | 4 ++-- .../component/RadioButtonInputTest.kt | 24 +++++++++++++++---- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/common/src/commonMain/kotlin/org/hisp/dhis/common/screens/RadioButtonInputScreen.kt b/common/src/commonMain/kotlin/org/hisp/dhis/common/screens/RadioButtonInputScreen.kt index f172b3a2f..6a84d29ca 100644 --- a/common/src/commonMain/kotlin/org/hisp/dhis/common/screens/RadioButtonInputScreen.kt +++ b/common/src/commonMain/kotlin/org/hisp/dhis/common/screens/RadioButtonInputScreen.kt @@ -60,6 +60,10 @@ fun RadioButtonInputScreen() { mutableStateOf(null) } + var selectedItemDisabled by remember { + mutableStateOf(null) + } + var selectedItemHorizontal by remember { mutableStateOf(radioButtonDataItemsHorizontal[0]) } @@ -88,6 +92,9 @@ fun RadioButtonInputScreen() { title = "Label", radioButtonData = radioButtonDataItemsDisabled, state = InputShellState.DISABLED, + onItemChange = { + selectedItemDisabled = it + }, ) Spacer(Modifier.size(Spacing.Spacing18)) SubTitle("Horizontal") diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt index fa1c34321..9014893e5 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt @@ -108,7 +108,7 @@ fun RadioButtonBlock( orientation: Orientation, content: List, itemSelected: RadioButtonData?, - onItemChange: ((RadioButtonData) -> Unit)? = null, + onItemChange: (RadioButtonData) -> Unit, ) { if (orientation == Orientation.HORIZONTAL) { FlowRowComponentsContainer( @@ -124,7 +124,7 @@ fun RadioButtonBlock( radioButtonData.textInput, ), ) { - onItemChange?.invoke(radioButtonData) + onItemChange.invoke(radioButtonData) } } }, @@ -143,7 +143,7 @@ fun RadioButtonBlock( radioButtonData.textInput, ), ) { - onItemChange?.invoke(radioButtonData) + onItemChange.invoke(radioButtonData) } } }, diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInput.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInput.kt index bf4bfa973..9e64f2a88 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInput.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInput.kt @@ -39,7 +39,7 @@ fun RadioButtonInput( legendData: LegendData? = null, isRequired: Boolean = false, itemSelected: RadioButtonData? = null, - onItemChange: ((RadioButtonData?) -> Unit)? = null, + onItemChange: (RadioButtonData?) -> Unit, ) { InputShell( modifier = modifier.testTag("RADIO_BUTTON_INPUT"), @@ -84,7 +84,7 @@ fun RadioButtonInput( ) }, onClick = { - onItemChange?.invoke(null) + onItemChange.invoke(null) }, ) } diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt index 9bc2be39b..bdd81a29e 100644 --- a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt @@ -29,7 +29,7 @@ class RadioButtonInputTest { RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), ) - val selectedItem by remember { + var selectedItem by remember { mutableStateOf(radioButtonData[0]) } RadioButtonInput( @@ -37,6 +37,9 @@ class RadioButtonInputTest { radioButtonData = radioButtonData, itemSelected = selectedItem, modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + onItemChange = { + selectedItem = it + }, ) } rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() @@ -105,7 +108,7 @@ class RadioButtonInputTest { RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), ) - val selectedItem by remember { + var selectedItem by remember { mutableStateOf(radioButtonData[0]) } RadioButtonInput( @@ -113,6 +116,9 @@ class RadioButtonInputTest { radioButtonData = radioButtonData, modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), itemSelected = selectedItem, + onItemChange = { + selectedItem = it + }, ) } rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() @@ -127,11 +133,16 @@ class RadioButtonInputTest { RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), ) - + var selectedItem by remember { + mutableStateOf(null) + } RadioButtonInput( title = "Label", radioButtonData = radioButtonData, modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + onItemChange = { + selectedItem = it + }, ) } rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() @@ -146,7 +157,7 @@ class RadioButtonInputTest { RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), ) - val selectedItem by remember { + var selectedItem by remember { mutableStateOf(radioButtonData[0]) } RadioButtonInput( @@ -155,6 +166,9 @@ class RadioButtonInputTest { state = InputShellState.DISABLED, itemSelected = selectedItem, modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + onItemChange = { + selectedItem = it + }, ) } rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() @@ -201,6 +215,7 @@ class RadioButtonInputTest { title = "Label", radioButtonData = radioButtonData, legendData = LegendData(SurfaceColor.CustomGreen, "Legend"), + onItemChange = {}, ) } @@ -221,6 +236,7 @@ class RadioButtonInputTest { title = "Label", radioButtonData = radioButtonData, supportingText = listOf(SupportingTextData("Supporting text", SupportingTextState.DEFAULT)), + onItemChange = {}, ) } rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists()