-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CheckboxSection filter component
- Loading branch information
1 parent
eb5a150
commit e206809
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
dashboard/src/components/Filter/CheckboxSection.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { fn } from '@storybook/test'; | ||
|
||
import CheckboxSection from './CheckboxSection'; | ||
|
||
const ActionsData = { | ||
onClickItem: fn(), | ||
}; | ||
|
||
const meta: Meta<typeof CheckboxSection> = { | ||
title: 'Filter CheckboxSection', | ||
component: CheckboxSection, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
args: { | ||
...ActionsData, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
items: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'], | ||
title: 'Branch', | ||
subtitle: 'Please select one or more Branches', | ||
}, | ||
}; | ||
|
||
export const WithSubSections: Story = { | ||
args: { | ||
title: 'Error Summary', | ||
subsections: [ | ||
{ | ||
title: 'Errors', | ||
items: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'], | ||
onClickItem: ActionsData.onClickItem, | ||
}, | ||
{ | ||
title: 'Warnings', | ||
items: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'], | ||
onClickItem: ActionsData.onClickItem, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import cls from 'classnames'; | ||
|
||
import { useCallback, useMemo } from 'react'; | ||
|
||
import Checkbox from '../Checkbox/Checkbox'; | ||
|
||
type TOnClickItem = (itemIdx: number, isChecked: boolean) => void; | ||
|
||
interface ICheckboxSectionItem { | ||
text: string; | ||
onClickItem: TOnClickItem; | ||
idx: number; | ||
} | ||
|
||
interface ICheckboxList { | ||
items: string[]; | ||
onClickItem: TOnClickItem; | ||
} | ||
|
||
interface ICheckboxSubsection { | ||
items: string[]; | ||
title: string; | ||
onClickItem: TOnClickItem; | ||
} | ||
|
||
interface ICheckboxSection { | ||
items?: string[]; | ||
title: string; | ||
subtitle?: string; | ||
subsections?: ICheckboxSubsection[]; | ||
onClickItem: TOnClickItem; | ||
className?: string; | ||
} | ||
|
||
const CheckboxSectionItem = ({ | ||
text, | ||
onClickItem, | ||
idx, | ||
}: ICheckboxSectionItem): JSX.Element => { | ||
const handleOnToggle = useCallback( | ||
(isChecked: boolean) => onClickItem(idx, isChecked), | ||
[idx, onClickItem], | ||
); | ||
return <Checkbox onToggle={handleOnToggle} text={text} />; | ||
}; | ||
|
||
const CheckboxList = ({ items, onClickItem }: ICheckboxList): JSX.Element => { | ||
const itemComponents = useMemo( | ||
() => | ||
items.map((text, idx) => ( | ||
<CheckboxSectionItem | ||
key={text} | ||
text={text} | ||
idx={idx} | ||
onClickItem={onClickItem} | ||
/> | ||
)), | ||
[items, onClickItem], | ||
); | ||
|
||
return ( | ||
<div className="flex flex-wrap gap-y-6 gap-x-4 max-w-[1000px]"> | ||
{itemComponents} | ||
</div> | ||
); | ||
}; | ||
|
||
const CheckboxSubsection = ({ | ||
title, | ||
items, | ||
onClickItem, | ||
}: ICheckboxSubsection): JSX.Element => { | ||
return ( | ||
<div> | ||
<h4 className="font-medium text-sm text-dimGray mb-2 mt-6">{title}</h4> | ||
<CheckboxList items={items} onClickItem={onClickItem} /> | ||
</div> | ||
); | ||
}; | ||
|
||
const CheckboxSection = ({ | ||
items, | ||
title, | ||
subtitle, | ||
onClickItem, | ||
subsections, | ||
className, | ||
}: ICheckboxSection): JSX.Element => { | ||
const subsectionComponents = useMemo( | ||
() => | ||
subsections?.map(subsection => ( | ||
<CheckboxSubsection | ||
key={subsection.title} | ||
title={subsection.title} | ||
items={subsection.items} | ||
onClickItem={subsection.onClickItem} | ||
/> | ||
)), | ||
[subsections], | ||
); | ||
|
||
return ( | ||
<div className={cls(className)}> | ||
<h3 className="font-semibold text-xl text-dimGray mb-2">{title}</h3> | ||
<h4 className="text-sm text-dimGray mb-6">{subtitle}</h4> | ||
{items && <CheckboxList items={items} onClickItem={onClickItem} />} | ||
{subsectionComponents} | ||
</div> | ||
); | ||
}; | ||
|
||
export default CheckboxSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters