Skip to content

Commit

Permalink
fix: ensure LinksInput handles array values correctly (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
sktbrd authored Dec 20, 2024
1 parent d5d3282 commit 2408cbc
Showing 1 changed file with 93 additions and 81 deletions.
174 changes: 93 additions & 81 deletions src/common/components/molecules/LinksInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,23 @@ const LinksInput = forwardRef<HTMLInputElement, LinksInputProps>(
(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);
},
[value, onChange],
);

// Add a new link
const addNewLink = () => {
const newLink = {
text: "New Link",
Expand All @@ -102,7 +106,9 @@ const LinksInput = forwardRef<HTMLInputElement, LinksInputProps>(
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);
Expand All @@ -112,98 +118,104 @@ const LinksInput = forwardRef<HTMLInputElement, LinksInputProps>(
setVisibleFields(updatedVisibleFields);
};

// Show additional fields for a link
const showAdditionalFields = (index: number) => {
const updatedVisibleFields = [...visibleFields];
updatedVisibleFields[index] = true;
setVisibleFields(updatedVisibleFields);
};

// Render LinksInput component
return (
<Box>
{value.map((link, index) => (
<LinkContainer key={index}>
<Title>{link.text || `Link ${index + 1}`}</Title>
<SubTitle>URL {index + 1}</SubTitle>
<TextFieldRoot isActive={visibleFields[index]}>
<TextFieldSlot>
<FaLink color="lightgray" size={14} />
</TextFieldSlot>
<TextFieldInput
placeholder="Link URL"
value={link.url}
onChange={(e: any) => {
handleLinkChange(index, { ...link, url: e.target.value });
showAdditionalFields(index);
}}
onFocus={() => showAdditionalFields(index)}
/>
<TextFieldSlot>
<p
style={{
color: "lightgray",
cursor: "pointer",
marginRight: "2px",
{Array.isArray(value) &&
value.map((link, index) => (
<LinkContainer key={index}>
<Title>{link.text || `Link ${index + 1}`}</Title>
<SubTitle>URL {index + 1}</SubTitle>
<TextFieldRoot isActive={visibleFields[index]}>
<TextFieldSlot>
<FaLink color="lightgray" size={14} />
</TextFieldSlot>
<TextFieldInput
placeholder="Link URL"
value={link.url}
onChange={(e: any) => {
handleLinkChange(index, { ...link, url: e.target.value });
showAdditionalFields(index);
}}
onClick={() => removeLink(index)}
>
x
</p>
</TextFieldSlot>
</TextFieldRoot>
{visibleFields[index] && (
<>
<SubTitle>Title</SubTitle>
<TextFieldRoot isActive={true}>
<TextFieldSlot>
<FaPencil color="lightgray" size={14} />
</TextFieldSlot>
<TextFieldInput
placeholder="Link text"
value={link.text}
onChange={(e: any) =>
handleLinkChange(index, { ...link, text: e.target.value })
}
onFocus={() => showAdditionalFields(index)}
/>
<TextFieldSlot>
<p
style={{
color: "lightgray",
cursor: "pointer",
marginRight: "2px",
}}
onClick={() => removeLink(index)}
>
x
</p>
</TextFieldSlot>
</TextFieldRoot>
{visibleFields[index] && (
<>
<SubTitle>Title</SubTitle>
<TextFieldRoot isActive={true}>
<TextFieldSlot>
<FaPencil color="lightgray" size={14} />
</TextFieldSlot>
<TextFieldInput
placeholder="Link text"
value={link.text}
onChange={(e: any) =>
handleLinkChange(index, {
...link,
text: e.target.value,
})
}
onFocus={() => showAdditionalFields(index)}
/>
</TextFieldRoot>
<SubTitle>Avatar</SubTitle>
<TextFieldRoot isActive={true}>
<TextFieldSlot>
{link.avatar ? (
<Avatar style={{ width: "24px", height: "24px" }}>
<AvatarImage src={link.avatar} alt={"choose image"} />
<AvatarFallback>
<span className="sr-only">{link.text}</span>
</AvatarFallback>
</Avatar>
) : (
<FaImage color="lightgray" size={14} />
)}
</TextFieldSlot>
<TextFieldInput
placeholder="Avatar URL"
value={link.avatar}
onChange={(e: any) =>
handleLinkChange(index, {
...link,
avatar: e.target.value,
})
}
onFocus={() => showAdditionalFields(index)}
/>
</TextFieldRoot>
<SubTitle>Description</SubTitle>
<CSSInput
value={link.description || ""}
onFocus={() => showAdditionalFields(index)}
/>
</TextFieldRoot>
<SubTitle>Avatar</SubTitle>
<TextFieldRoot isActive={true}>
<TextFieldSlot>
{link.avatar ? (
<Avatar style={{ width: "24px", height: "24px" }}>
<AvatarImage src={link.avatar} alt={"choose image"} />
<AvatarFallback>
<span className="sr-only">{link.text}</span>
</AvatarFallback>
</Avatar>
) : (
<FaImage color="lightgray" size={14} />
)}
</TextFieldSlot>
<TextFieldInput
placeholder="Avatar URL"
value={link.avatar}
onChange={(e: any) =>
handleLinkChange(index, {
...link,
avatar: e.target.value,
})
onChange={(description) =>
handleLinkChange(index, { ...link, description })
}
onFocus={() => showAdditionalFields(index)}
/>
</TextFieldRoot>
<SubTitle>Description</SubTitle>
<CSSInput
value={link.description || ""}
onFocus={() => showAdditionalFields(index)}
onChange={(description) =>
handleLinkChange(index, { ...link, description })
}
/>
</>
)}
</LinkContainer>
))}
</>
)}
</LinkContainer>
))}
<AddLinkButton type="button" onClick={addNewLink}>
<FaPlus style={{ marginRight: "5px" }} /> Add link
</AddLinkButton>
Expand Down

0 comments on commit 2408cbc

Please sign in to comment.