Skip to content

Commit

Permalink
Merge pull request #12753 from woocommerce/custom-fields/disable-buttons
Browse files Browse the repository at this point in the history
[Custom Fields] Update buttons to be disabled instead of hidden when there are no changes
  • Loading branch information
JorgeMucientes authored Oct 3, 2024
2 parents 286d336 + ec00c7f commit fd4629a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ private fun CustomFieldsEditorScreen(
title = "Custom Field",
onNavigationButtonClick = onBackButtonClick,
actions = {
if (state.showDoneButton) {
WCTextButton(
onClick = onDoneClicked,
text = stringResource(R.string.done)
)
}
WCTextButton(
onClick = onDoneClicked,
text = stringResource(R.string.done),
enabled = state.enableDoneButton
)
WCOverflowMenu(
items = listOfNotNull(
R.string.custom_fields_editor_copy_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class CustomFieldsEditorViewModel @Inject constructor(
val keyErrorMessage: UiString? = null,
val isCreatingNewItem: Boolean = false,
) {
val showDoneButton
val enableDoneButton
get() = customField.key.isNotEmpty() && hasChanges && keyErrorMessage == null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ private fun CustomFieldsScreen(
title = stringResource(id = R.string.custom_fields_list_title),
onNavigationButtonClick = onBackClick,
actions = {
if (state.hasChanges) {
WCTextButton(
text = stringResource(id = R.string.save),
onClick = onSaveClicked
)
}
WCTextButton(
text = stringResource(id = R.string.save),
onClick = onSaveClicked,
enabled = state.hasChanges
)
}
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,47 +119,47 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {
}

@Test
fun `given editing an existing field, when key is changed, then show done button`() = testBlocking {
fun `given editing an existing field, when key is changed, then enable done button`() = testBlocking {
setup(editing = true)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("new key")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `given editing an existing field, when value is changed, then show done button`() = testBlocking {
fun `given editing an existing field, when value is changed, then enable done button`() = testBlocking {
setup(editing = true)

val state = viewModel.state.runAndCaptureValues {
viewModel.onValueChanged("new value")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `given creating a new field, when the key is not empty, then show done button`() = testBlocking {
fun `given creating a new field, when the key is not empty, then enable done button`() = testBlocking {
setup(editing = false)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("key")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `when key is empty, then hide done button`() = testBlocking {
fun `when key is empty, then disable done button`() = testBlocking {
setup(editing = false)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("")
}.last()

assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down Expand Up @@ -249,7 +249,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {

assertThat(state.keyErrorMessage)
.isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_duplicate))
assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down Expand Up @@ -291,7 +291,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {

assertThat(state.keyErrorMessage)
.isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_underscore))
assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down

0 comments on commit fd4629a

Please sign in to comment.