Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Chip list component updates #668

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@
"eslint --fix --ext ts,tsx,js,jsx"
]
}
}
}
6 changes: 3 additions & 3 deletions src/components/CatalogCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function CatalogCardRef(
{(category || tags?.length > 0) && (
<Flex
alignItems="start"
gap="large"
gap="small"
justifyContent={category ? 'space-between' : 'end'}
marginTop={theme.spacing.medium}
>
Expand All @@ -118,6 +118,7 @@ function CatalogCardRef(
size="small"
border="none"
fillLevel={3}
truncateWidth={70}
>
{category}
</Chip>
Expand All @@ -128,8 +129,7 @@ function CatalogCardRef(
fillLevel={3}
values={tags ?? []}
limit={1}
truncateWidth={100}
wrap="nowrap"
truncateWidth={70}
emptyState={null}
/>
</Flex>
Expand Down
80 changes: 34 additions & 46 deletions src/components/ChipList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Flex, type FlexBaseProps, Span } from 'honorable'
import isEmpty from 'lodash-es/isEmpty'
import {
type ComponentProps,
type Dispatch,
type ReactElement,
useState,
useCallback,
} from 'react'

import { HamburgerMenuCollapseIcon } from '../icons'

import Chip, { type ChipProps } from './Chip'
import Flex from './Flex'

type TransformFn<TValue> = (
value: TValue
Expand All @@ -19,7 +17,6 @@ export type ChipListProps<TValue> = {
values: TValue[]
transformValue?: TransformFn<TValue>
limit: number
wrap?: Pick<FlexBaseProps, 'wrap'>
emptyState?: JSX.Element | null
onClickCondition?: (value: TValue) => boolean
onClick?: Dispatch<TValue>
Expand All @@ -29,60 +26,51 @@ function ChipList<TValue = string>({
values = [],
transformValue,
limit = 4,
wrap = 'wrap',
emptyState,
onClickCondition,
onClick,
...props
}: ChipListProps<TValue>): ReactElement {
const [collapsed, setCollapsed] = useState(true)
const chip = useCallback(
(v: TValue, i: number) => {
const clickable = onClickCondition?.(v) ?? false

return (
<Chip
key={(v as any).key || i}
clickable={clickable}
onClick={() => clickable && onClick(v)}
{...props}
>
{transformValue ? transformValue(v) : `${v}`}
</Chip>
)
},
[onClick, onClickCondition, props, transformValue]
)

return (
<Flex
gap="xsmall"
wrap={wrap}
wrap="wrap"
>
{isEmpty(values) &&
(emptyState !== undefined ? (
emptyState
) : (
<Span body2>There is nothing to display here.</Span>
))}
{values.slice(0, collapsed ? limit : undefined).map((v, i) => {
const clickable = onClickCondition?.(v) ?? false

return (
<Chip
key={(v as any).key || i}
clickable={clickable}
onClick={() => clickable && onClick(v)}
{...props}
>
{transformValue ? transformValue(v) : `${v}`}
</Chip>
)
})}
(emptyState !== undefined
? emptyState
: 'There is nothing to display here.')}
{values.slice(0, limit).map(chip)}
{values.length > limit && (
<>
{collapsed && (
<Chip
onClick={() => setCollapsed(false)}
{...props}
clickable
>
{`+${values.length - limit}`}
</Chip>
)}
{!collapsed && (
<Chip
onClick={() => setCollapsed(true)}
{...props}
clickable
<Chip
{...props}
tooltip={
<Flex
gap="xsmall"
wrap="wrap"
>
<HamburgerMenuCollapseIcon />
</Chip>
)}
</>
{values.slice(limit, values.length).map(chip)}
</Flex>
}
>{`+${values.length - limit}`}</Chip>
)}
</Flex>
)
Expand Down
Loading