From 466a0b48f8ce6f185e4d97b642a6ab4b689dc9e7 Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Mon, 30 Jan 2023 10:52:38 +0100
Subject: [PATCH] Fix AutocompleteArrayInput when value is not an array
---
.../src/input/AutocompleteArrayInput.spec.tsx | 39 +++++++++++++++++++
.../src/input/AutocompleteInput.tsx | 2 +-
2 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/packages/ra-ui-materialui/src/input/AutocompleteArrayInput.spec.tsx b/packages/ra-ui-materialui/src/input/AutocompleteArrayInput.spec.tsx
index 38f0796f355..30643aa7ef5 100644
--- a/packages/ra-ui-materialui/src/input/AutocompleteArrayInput.spec.tsx
+++ b/packages/ra-ui-materialui/src/input/AutocompleteArrayInput.spec.tsx
@@ -988,4 +988,43 @@ describe('', () => {
expect(noOptionsAppeared).toBe(false);
});
+
+ it('should not crash if its value is not an array', () => {
+ render(
+
+
+
+
+
+ );
+ expect(screen.queryByRole('textbox')).not.toBeNull();
+ });
+
+ it('should not crash if its value is not an array and is empty', () => {
+ render(
+
+
+
+
+
+ );
+ expect(screen.queryByRole('textbox')).not.toBeNull();
+ });
});
diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
index da1ae1c68c9..b7b8c7c09a6 100644
--- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
+++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
@@ -742,7 +742,7 @@ const getSelectedItems = (
multiple
) => {
if (multiple) {
- return (Array.isArray(value || []) ? value : [value])
+ return (Array.isArray(value ?? []) ? value : [value])
.map(item =>
choices.find(
choice => String(item) === String(get(choice, optionValue))