diff --git a/datacapture/src/main/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactory.kt b/datacapture/src/main/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactory.kt index 179eb7da79..884c2e7aba 100644 --- a/datacapture/src/main/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactory.kt +++ b/datacapture/src/main/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Google LLC + * Copyright 2022-2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -125,6 +125,9 @@ internal object CheckBoxGroupViewHolderFactory : when (isChecked) { true -> { val newAnswers = questionnaireViewItem.answers.toMutableList() + if (!questionnaireViewItem.questionnaireItem.repeats) { + newAnswers.clear() + } newAnswers += QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { value = answerOption.value diff --git a/datacapture/src/test/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactoryTest.kt b/datacapture/src/test/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactoryTest.kt index f5f361cc87..57b398cc12 100644 --- a/datacapture/src/test/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactoryTest.kt +++ b/datacapture/src/test/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactoryTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 Google LLC + * Copyright 2023-2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -616,4 +616,51 @@ class CheckBoxGroupViewHolderFactoryTest { }, ) } + + @Test + fun click_should_Select_Other_CheckboxButton() { + viewHolder.bind( + QuestionnaireViewItem( + Questionnaire.QuestionnaireItemComponent().apply { + repeats = false + addAnswerOption( + Questionnaire.QuestionnaireItemAnswerOptionComponent().apply { + value = + Coding().apply { + code = "code-1" + display = "display-1" + } + }, + ) + addAnswerOption( + Questionnaire.QuestionnaireItemAnswerOptionComponent().apply { + value = + Coding().apply { + code = "code-2" + display = "display-2" + } + }, + ) + }, + QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { + addAnswer( + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { + value = + Coding().apply { + code = "code-1" + display = "display-1" + } + }, + ) + }, + validationResult = NotValidated, + answersChangedCallback = { _, _, _, _ -> }, + ), + ) + + val checkBoxGroup = viewHolder.itemView.findViewById(R.id.checkbox_group) + assertThat((checkBoxGroup.getChildAt(1) as CheckBox).isChecked).isTrue() + checkBoxGroup.getChildAt(2).performClick() + assertThat((checkBoxGroup.getChildAt(2) as CheckBox).isChecked).isTrue() + } }