From 2408cbcda795c74ba4f0be5977d0c41e88f6274d Mon Sep 17 00:00:00 2001 From: Vlad 2 <116202536+sktbrd@users.noreply.github.com> Date: Sat, 21 Dec 2024 03:14:47 +0700 Subject: [PATCH] fix: ensure LinksInput handles array values correctly (#534) --- .../components/molecules/LinksInput.tsx | 174 ++++++++++-------- 1 file changed, 93 insertions(+), 81 deletions(-) diff --git a/src/common/components/molecules/LinksInput.tsx b/src/common/components/molecules/LinksInput.tsx index b0cdfb69..b6b2d16f 100644 --- a/src/common/components/molecules/LinksInput.tsx +++ b/src/common/components/molecules/LinksInput.tsx @@ -77,12 +77,15 @@ const LinksInput = forwardRef( (props, ref) => { const { value = [], onChange } = props; + // Initialize visibleFields state const [visibleFields, setVisibleFields] = useState( - value.map(() => false), // controls the visibility of additional inputs + Array.isArray(value) ? value.map(() => false) : [], ); + // Handle link changes const handleLinkChange = useCallback( (index: number, newLink: Link) => { + if (!Array.isArray(value)) return; // Ensure value is an array const updatedLinks = [...value]; updatedLinks[index] = newLink; onChange?.(updatedLinks); @@ -90,6 +93,7 @@ const LinksInput = forwardRef( [value, onChange], ); + // Add a new link const addNewLink = () => { const newLink = { text: "New Link", @@ -102,7 +106,9 @@ const LinksInput = forwardRef( setVisibleFields([...visibleFields, true]); }; + // Remove a link const removeLink = (index: number) => { + if (!Array.isArray(value)) return; // Ensure value is an array const updatedLinks = [...value]; updatedLinks.splice(index, 1); onChange?.(updatedLinks); @@ -112,98 +118,104 @@ const LinksInput = forwardRef( setVisibleFields(updatedVisibleFields); }; + // Show additional fields for a link const showAdditionalFields = (index: number) => { const updatedVisibleFields = [...visibleFields]; updatedVisibleFields[index] = true; setVisibleFields(updatedVisibleFields); }; + // Render LinksInput component return ( - {value.map((link, index) => ( - - {link.text || `Link ${index + 1}`} - URL {index + 1} - - - - - { - handleLinkChange(index, { ...link, url: e.target.value }); - showAdditionalFields(index); - }} - onFocus={() => showAdditionalFields(index)} - /> - -

( + + {link.text || `Link ${index + 1}`} + URL {index + 1} + + + + + { + handleLinkChange(index, { ...link, url: e.target.value }); + showAdditionalFields(index); }} - onClick={() => removeLink(index)} - > - x -

-
-
- {visibleFields[index] && ( - <> - Title - - - - - - handleLinkChange(index, { ...link, text: e.target.value }) - } + onFocus={() => showAdditionalFields(index)} + /> + +

removeLink(index)} + > + x +

+
+
+ {visibleFields[index] && ( + <> + Title + + + + + + handleLinkChange(index, { + ...link, + text: e.target.value, + }) + } + onFocus={() => showAdditionalFields(index)} + /> + + Avatar + + + {link.avatar ? ( + + + + {link.text} + + + ) : ( + + )} + + + handleLinkChange(index, { + ...link, + avatar: e.target.value, + }) + } + onFocus={() => showAdditionalFields(index)} + /> + + Description + showAdditionalFields(index)} - /> - - Avatar - - - {link.avatar ? ( - - - - {link.text} - - - ) : ( - - )} - - - handleLinkChange(index, { - ...link, - avatar: e.target.value, - }) + onChange={(description) => + handleLinkChange(index, { ...link, description }) } - onFocus={() => showAdditionalFields(index)} /> - - Description - showAdditionalFields(index)} - onChange={(description) => - handleLinkChange(index, { ...link, description }) - } - /> - - )} -
- ))} + + )} + + ))} Add link