Skip to content

Commit

Permalink
chore: Update package version to 1.0.182 and add onAddSection and onA…
Browse files Browse the repository at this point in the history
…ddSectionItem to Accordion component
  • Loading branch information
agjs committed May 7, 2024
1 parent b4a4cc8 commit e82c98d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@programmer_network/yail",
"version": "1.0.181",
"version": "1.0.182",
"description": "Programmer Network's official UI library for React",
"author": "Aleksandar Grbic - (https://programmer.network)",
"publishConfig": {
Expand Down
50 changes: 40 additions & 10 deletions src/Components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export const Primary = () => {
setSections(dummySections);
}, []);

Check warning on line 51 in src/Components/Accordion/Accordion.stories.tsx

View workflow job for this annotation

GitHub Actions / lint-and-test

React Hook useEffect has a missing dependency: 'sections.length'. Either include it or remove the dependency array

useEffect(() => {
console.log("Changed", sections);
}, [sections]);

if (!sections.length) {
return <div>Loading...</div>;
}
Expand Down Expand Up @@ -94,10 +90,6 @@ export const NonInteractive = () => {
setSections(dummySections);
}, []);

Check warning on line 91 in src/Components/Accordion/Accordion.stories.tsx

View workflow job for this annotation

GitHub Actions / lint-and-test

React Hook useEffect has a missing dependency: 'sections.length'. Either include it or remove the dependency array

useEffect(() => {
console.log("Changed", sections);
}, [sections]);

if (!sections.length) {
return <div>Loading...</div>;
}
Expand Down Expand Up @@ -133,9 +125,40 @@ export const WithoutDragAndDrop = () => {
setSections(dummySections);
}, []);

Check warning on line 126 in src/Components/Accordion/Accordion.stories.tsx

View workflow job for this annotation

GitHub Actions / lint-and-test

React Hook useEffect has a missing dependency: 'sections.length'. Either include it or remove the dependency array

if (!sections.length) {
return <div>Loading...</div>;
}

return (
<div>
<Accordion
className='yl-w-[500px]'
selectedId={2}
sections={sections}
setSections={setSections}
expanded={expandedSections}
setExpanded={(expanded: number[]) => {
setExpandedSections(expanded);
}}
onSelected={item => {
action("onSelected")(item);
}}
/>
</div>
);
};

export const AddSectionAndSectionItem = () => {
const [sections, setSections] = useState<ISection[]>([]);
const [expandedSections, setExpandedSections] = useState<number[]>([1]);

useEffect(() => {
console.log("Changed", sections);
}, [sections]);
if (sections.length) {
return;
}

setSections(dummySections);
}, []);

Check warning on line 161 in src/Components/Accordion/Accordion.stories.tsx

View workflow job for this annotation

GitHub Actions / lint-and-test

React Hook useEffect has a missing dependency: 'sections.length'. Either include it or remove the dependency array

if (!sections.length) {
return <div>Loading...</div>;
Expand All @@ -149,8 +172,15 @@ export const WithoutDragAndDrop = () => {
sections={sections}
setSections={setSections}
expanded={expandedSections}
onAddSection={() => {
action("onAddSection")();
}}
onAddSectionItem={section => {
action("onAddSectionItem")(section);
}}
setExpanded={(expanded: number[]) => {
setExpandedSections(expanded);
action("setExpanded")(expanded);
}}
onSelected={item => {
action("onSelected")(item);
Expand Down
111 changes: 76 additions & 35 deletions src/Components/Accordion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { FC, useEffect, useState } from "react";

import DraggableList from "Components/DraggableList";
import { IDraggableListItem } from "Components/DraggableList/types";
import { IconDrag, IconExpandLess, IconExpandMore } from "Components/Icons";
import {
IconAddCircle,
IconDrag,
IconExpandLess,
IconExpandMore
} from "Components/Icons";
import { Paragraph } from "Components/Typography";

import ArrayUtils from "Utils/Array";
Expand All @@ -18,6 +23,8 @@ const Accordion: FC<IAccordionProps> = ({
onSectionItemClick,
onSectionClick,
onSelected,
onAddSection,
onAddSectionItem,
expanded,
setExpanded,
selectedId,
Expand Down Expand Up @@ -91,7 +98,8 @@ const Accordion: FC<IAccordionProps> = ({
{
"yl-border-b-0": idx !== sections.length - 1,
"yl-rounded-tl-md yl-rounded-tr-md": idx === 0,
"yl-rounded-bl-md yl-rounded-br-md": idx === sections.length - 1
"yl-rounded-bl-md yl-rounded-br-md":
idx === sections.length - 1 && !onAddSection
},
{
"yl-bg-primary-text-color/5": draggedOverId === section.id,
Expand Down Expand Up @@ -162,42 +170,75 @@ const Accordion: FC<IAccordionProps> = ({
</h3>

{expanded.includes(section.id) && (
<DraggableList
items={section.items}
onClick={(item: IDraggableListItem) => {
onSectionItemClick?.(item);
onSelected?.(item);
setSelectedItemId(item.id);
}}
onDragged={(items: IDraggableListItem[]) => {
setSections(
[
...sections.map(s =>
s.id === section.id ? { ...s, items } : s
)
],
{ sectionId: section.id, items }
);
}}
isDraggable={hasDraggableSectionItems}
className='yl-gap-4 yl-flex yl-flex-col yl-py-4'
draggedOverClassName='yl-border-t-2 yl-border-primary'
liClassName={(item: IDraggableListItem) =>
classNames(
"yl-cursor-default hover:text-primary break-all leading-normal yl-px-4",
{
"yl-cursor-pointer": onSectionItemClick,
"yl-text-primary-text-color": selectedItemId !== item.id,
"yl-text-primary":
onSectionItemClick && selectedItemId === item.id,
"yl-pl-9": section.items.length === 1
}
)
}
/>
<>
<DraggableList
items={section.items}
onClick={(item: IDraggableListItem) => {
onSectionItemClick?.(item);
onSelected?.(item);
setSelectedItemId(item.id);
}}
onDragged={(items: IDraggableListItem[]) => {
setSections(
[
...sections.map(s =>
s.id === section.id ? { ...s, items } : s
)
],
{ sectionId: section.id, items }
);
}}
isDraggable={hasDraggableSectionItems}
className='yl-gap-4 yl-flex yl-flex-col yl-py-4'
draggedOverClassName='yl-border-t-2 yl-border-primary'
liClassName={(item: IDraggableListItem) =>
classNames(
"yl-cursor-default hover:text-primary break-all leading-normal yl-px-4",
{
"yl-cursor-pointer": onSectionItemClick,
"yl-text-primary-text-color":
selectedItemId !== item.id,
"yl-text-primary":
onSectionItemClick && selectedItemId === item.id,
"yl-pl-9": section.items.length === 1
}
)
}
/>
{onAddSectionItem && (
<div
onClick={() => onAddSectionItem(section)}
className={classNames(
"yl-cursor-pointer hover:yl-text-primary yl-pb-4 yl-text-primary-text-color yl-text-center yl-flex yl-items-center yl-justify-center yl-gap-1"
)}
>
<IconAddCircle className='yl-w-6' />
</div>
)}
</>
)}
</div>
))}
{onAddSection && (
<div
onClick={onAddSection}
className={classNames(
"yl-group yl-border-2 yl-border-t-0 yl-border-primary-text-color/20 hover:yl-bg-primary-text-color/5 yl-rounded-bl-md yl-rounded-br-md yl-cursor-pointer"
)}
role='presentation'
>
<h3
className={classNames(
"yl-flex yl-flex-col yl-items-center yl-justify-center yl-select-none yl-font-semibold yl-p-4 yl-text-center"
)}
role='button'
>
<Paragraph className='yl-text-primary-text-color/70 group-hover:yl-text-primary yl-flex yl-items-center yl-gap-1'>
<IconAddCircle className='yl-w-6 group-hover:yl-text-primary' />
</Paragraph>
</h3>
</div>
)}
</section>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Accordion/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface IAccordionProps {
onSectionItemClick?: (item: IDraggableListItem) => void;
onSectionClick?: (item: IDraggableListItem) => void;
onSelected?: (item: IDraggableListItem) => void;
onAddSection?: () => void;
onAddSectionItem?: (item: ISection) => void;
expanded: number[];
setExpanded: (sections: number[]) => void;
selectedId?: number;
Expand Down

0 comments on commit e82c98d

Please sign in to comment.