Skip to content

Commit 10ef1e6

Browse files
committed
change Pill component to FC
1 parent 034d87a commit 10ef1e6

File tree

1 file changed

+72
-60
lines changed

1 file changed

+72
-60
lines changed

src/scripts/Pill.tsx

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import React, {
2-
Component,
32
ReactHTML,
43
HTMLAttributes,
54
MouseEvent,
65
KeyboardEvent,
6+
Ref,
7+
FC,
78
} from 'react';
89
import classnames from 'classnames';
910
import { Icon, IconCategory } from './Icon';
1011
import { Button } from './Button';
12+
import { useCallback } from '@storybook/addons';
1113

14+
/**
15+
*
16+
*/
1217
export type PillProps = {
1318
label?: string;
1419
truncate?: boolean;
@@ -18,68 +23,75 @@ export type PillProps = {
1823
category?: IconCategory;
1924
icon?: string;
2025
};
21-
pillRef?: (node: HTMLElement) => void;
26+
pillRef?: Ref<HTMLElement>;
2227
onRemove?: () => void;
2328
} & HTMLAttributes<HTMLSpanElement>;
2429

25-
export class Pill extends Component<PillProps> {
26-
onPillClick = (e: MouseEvent<HTMLElement>) => {
27-
if (this.props.onClick) {
28-
this.props.onClick(e);
29-
}
30-
};
31-
32-
onPillRemove = (e: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => {
33-
e.preventDefault();
34-
e.stopPropagation();
35-
if (this.props.onRemove) {
36-
this.props.onRemove();
37-
}
38-
};
30+
/**
31+
*
32+
*/
33+
export const Pill: FC<PillProps> = (props) => {
34+
const {
35+
icon,
36+
disabled,
37+
label,
38+
tag,
39+
truncate,
40+
className,
41+
pillRef,
42+
onClick,
43+
onRemove,
44+
} = props;
45+
const onPillRemove = useCallback(
46+
(e: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => {
47+
e.preventDefault();
48+
e.stopPropagation();
49+
onRemove?.();
50+
},
51+
[onRemove]
52+
);
3953

40-
onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
41-
if (e.keyCode === 8 || e.keyCode === 46) {
42-
// Bacspace / DEL
43-
this.onPillRemove(e);
44-
}
45-
};
54+
const onKeyDown = useCallback(
55+
(e: KeyboardEvent<HTMLElement>) => {
56+
if (e.keyCode === 8 || e.keyCode === 46) {
57+
// Bacspace / DEL
58+
onPillRemove(e);
59+
}
60+
},
61+
[onPillRemove]
62+
);
4663

47-
render() {
48-
const { icon, disabled, label, tag, pillRef, truncate, className } =
49-
this.props;
50-
const Tag: any = disabled ? 'span' : tag || 'a';
51-
const pillClassNames = classnames(
52-
'slds-pill',
53-
{ 'slds-truncate': truncate },
54-
className
55-
);
56-
return (
57-
<Tag
58-
ref={(node: HTMLElement) => {
59-
if (pillRef) pillRef(node);
60-
}}
61-
className={pillClassNames}
62-
onKeyDown={this.onKeyDown}
63-
onClick={this.onPillClick}
64-
>
65-
{icon && icon.icon ? (
66-
<Icon
67-
className='slds-pill__icon'
68-
category={icon.category}
69-
icon={icon.icon}
70-
/>
71-
) : undefined}
72-
<span className='slds-pill__label'>{label}</span>
73-
<Button
74-
disabled={disabled}
75-
className='slds-pill__remove'
76-
type='icon-bare'
77-
icon='close'
78-
alt='Remove'
79-
tabIndex={-1}
80-
onClick={this.onPillRemove}
64+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
65+
const Tag: any = disabled ? 'span' : tag || 'a';
66+
const pillClassNames = classnames(
67+
'slds-pill',
68+
{ 'slds-truncate': truncate },
69+
className
70+
);
71+
return (
72+
<Tag
73+
ref={pillRef}
74+
className={pillClassNames}
75+
onKeyDown={onKeyDown}
76+
onClick={onClick}
77+
>
78+
{icon && icon.icon ? (
79+
<Icon
80+
className='slds-pill__icon'
81+
category={icon.category}
82+
icon={icon.icon}
8183
/>
82-
</Tag>
83-
);
84-
}
85-
}
84+
) : undefined}
85+
<span className='slds-pill__label'>{label}</span>
86+
<Button
87+
disabled={disabled}
88+
className='slds-pill__remove'
89+
type='icon-bare'
90+
icon='close'
91+
alt='Remove'
92+
tabIndex={-1}
93+
onClick={onPillRemove}
94+
/>
95+
</Tag>
96+
);
97+
};

0 commit comments

Comments
 (0)