-
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.
- Loading branch information
1 parent
c994f47
commit 58be575
Showing
6 changed files
with
178 additions
and
70 deletions.
There are no files selected for viewing
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
28 changes: 0 additions & 28 deletions
28
dashboard/src/components/FilterButton/FilterButton.stories.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
dashboard/src/components/FilterList/FilterList.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,65 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { fn } from '@storybook/test'; | ||
import { IntlProvider } from 'react-intl'; | ||
import { flatten } from 'flat'; | ||
|
||
import { LOCALES } from '../../locales/constants'; | ||
|
||
import { messages } from '../../locales/messages'; | ||
|
||
import FilterList from './FilterList'; | ||
|
||
const ActionsData = { | ||
onClickItem: fn(), | ||
onClickClean: fn(), | ||
}; | ||
|
||
const meta: Meta<typeof FilterList> = { | ||
title: 'FilterList', | ||
component: FilterList, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
args: { | ||
...ActionsData, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
itens: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'], | ||
}, | ||
decorators: [ | ||
(story): JSX.Element => ( | ||
<IntlProvider | ||
messages={flatten(messages[LOCALES.EN_US])} | ||
locale={LOCALES.EN_US} | ||
> | ||
{story()} | ||
</IntlProvider> | ||
), | ||
], | ||
}; | ||
|
||
export const MultipleLines: Story = { | ||
args: { | ||
itens: [ | ||
'linux-5.15.y', | ||
'Status:failed', | ||
'Status: Warnings', | ||
'Status:failed', | ||
'Status: Warnings', | ||
], | ||
}, | ||
decorators: [ | ||
(story): JSX.Element => ( | ||
<IntlProvider locale="en"> | ||
<div className="w-[500px]">{story()}</div> | ||
</IntlProvider> | ||
), | ||
], | ||
}; |
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,111 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { IoClose } from 'react-icons/io5'; | ||
import classNames from 'classnames'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { Button } from '../ui/button'; | ||
|
||
interface IFilterList { | ||
itens: string[]; | ||
onClickItem: (itemIdx: number) => void; | ||
onClickClean: () => void; | ||
removeOnEmpty?: boolean; | ||
} | ||
|
||
interface IFilterItem extends IFilterButton { | ||
idx: number; | ||
onClickItem: (idx: number) => void; | ||
} | ||
|
||
export interface IFilterButton | ||
extends React.ButtonHTMLAttributes<HTMLButtonElement> { | ||
text: string; | ||
variant?: 'primary' | 'secondary'; | ||
className?: string; | ||
} | ||
|
||
const baseButtonClassNames = | ||
'text-sm h-10 pr-2 rounded-md flex items-center bg-darkGray hover:bg-mediumGray'; | ||
const primaryClassNames = 'bg-lightBlue text-white'; | ||
const secondaryClassNames = 'bg-darkGray text-black'; | ||
|
||
const FilterButton = ({ | ||
text, | ||
variant = 'secondary', | ||
className, | ||
...props | ||
}: IFilterButton): JSX.Element => { | ||
const buttonClassNames = classNames( | ||
baseButtonClassNames, | ||
variant === 'primary' ? primaryClassNames : secondaryClassNames, | ||
className, | ||
); | ||
|
||
return ( | ||
<Button className={buttonClassNames} {...props}> | ||
{text} | ||
<IoClose className="size-6" /> | ||
</Button> | ||
); | ||
}; | ||
|
||
const FilterItem = ({ | ||
text, | ||
variant, | ||
onClickItem, | ||
idx, | ||
...props | ||
}: IFilterItem): JSX.Element => { | ||
const onClickHandler = useCallback( | ||
() => onClickItem(idx), | ||
[onClickItem, idx], | ||
); | ||
|
||
return ( | ||
<FilterButton | ||
text={text} | ||
variant={variant} | ||
onClick={onClickHandler} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
const FilterList = ({ | ||
itens, | ||
onClickItem, | ||
onClickClean, | ||
removeOnEmpty = false, | ||
}: IFilterList): JSX.Element => { | ||
const intl = useIntl(); | ||
|
||
const buttonList = useMemo( | ||
() => | ||
itens.map((item, idx) => ( | ||
<FilterItem | ||
key={idx + item} | ||
text={item} | ||
onClickItem={onClickItem} | ||
idx={idx} | ||
/> | ||
)), | ||
[itens, onClickItem], | ||
); | ||
|
||
if (removeOnEmpty && !itens) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-wrap gap-4"> | ||
{buttonList} | ||
<FilterButton | ||
text={intl.formatMessage({ id: 'global.cleanAll' })} | ||
variant="primary" | ||
onClick={onClickClean} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FilterList; |
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