Skip to content

Commit 4c1cf7f

Browse files
authored
feat: add conditional onClick action to chip list (#579)
1 parent f9d498d commit 4c1cf7f

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/components/ChipList.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Flex, Span } from 'honorable'
22
import isEmpty from 'lodash-es/isEmpty'
3-
import { type ComponentProps, type ReactElement, useState } from 'react'
3+
import {
4+
type ComponentProps,
5+
type Dispatch,
6+
type ReactElement,
7+
useState,
8+
} from 'react'
49

510
import { HamburgerMenuCollapseIcon } from '../icons'
611

@@ -15,13 +20,17 @@ export type ChipListProps<TValue> = {
1520
transformValue?: TransformFn<TValue>
1621
limit: number
1722
emptyState?: JSX.Element | null
23+
onClickCondition?: (value: TValue) => boolean
24+
onClick?: Dispatch<TValue>
1825
} & ChipProps
1926

2027
function ChipList<TValue = string>({
2128
values = [],
2229
transformValue,
2330
limit = 4,
2431
emptyState,
32+
onClickCondition,
33+
onClick,
2534
...props
2635
}: ChipListProps<TValue>): ReactElement {
2736
const [collapsed, setCollapsed] = useState(true)
@@ -37,14 +46,20 @@ function ChipList<TValue = string>({
3746
) : (
3847
<Span body2>There is nothing to display here.</Span>
3948
))}
40-
{values.slice(0, collapsed ? limit : undefined).map((v, i) => (
41-
<Chip
42-
key={(v as any).key || i}
43-
{...props}
44-
>
45-
{transformValue ? transformValue(v) : `${v}`}
46-
</Chip>
47-
))}
49+
{values.slice(0, collapsed ? limit : undefined).map((v, i) => {
50+
const clickable = onClickCondition?.(v) ?? false
51+
52+
return (
53+
<Chip
54+
key={(v as any).key || i}
55+
clickable={clickable}
56+
onClick={() => clickable && onClick(v)}
57+
{...props}
58+
>
59+
{transformValue ? transformValue(v) : `${v}`}
60+
</Chip>
61+
)
62+
})}
4863
{values.length > limit && (
4964
<>
5065
{collapsed && (

src/stories/ChipList.stories.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ function TextTemplate({ onFillLevel, ...args }: any) {
6868
)
6969
}
7070

71+
function CustomClickTemplate({ onFillLevel, ...args }: any) {
72+
const VALUES = [
73+
'avengers',
74+
'iron man',
75+
'doctor strange',
76+
'thor',
77+
'black panther',
78+
'guardians of the galaxy',
79+
]
80+
81+
return (
82+
<WrapWithIf
83+
condition={onFillLevel > 0}
84+
wrapper={
85+
<Card
86+
width="600px"
87+
padding="medium"
88+
fillLevel={onFillLevel}
89+
/>
90+
}
91+
>
92+
<ChipList
93+
values={VALUES}
94+
{...args}
95+
/>
96+
</WrapWithIf>
97+
)
98+
}
99+
71100
interface Label {
72101
key?: string
73102
value: string
@@ -142,3 +171,12 @@ Empty.args = {
142171
size: 'small',
143172
onFillLevel: 0,
144173
}
174+
175+
export const CustomClick = CustomClickTemplate.bind({})
176+
CustomClick.args = {
177+
severity: 'info',
178+
size: 'small',
179+
onFillLevel: 0,
180+
onClickCondition: (value: string) => value === 'avengers',
181+
onClick: (value: string) => alert(value),
182+
}

0 commit comments

Comments
 (0)