Skip to content
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
70 changes: 65 additions & 5 deletions packages/react-core/src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,76 @@
import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/Label/label';
import { Button } from '../Button';
import { css } from '@patternfly/react-styles';
import TimesIcon from '@patternfly/react-icons/dist/js/icons/times-icon';

export interface LabelProps extends React.HTMLProps<HTMLSpanElement> {
/** Content rendered inside the label. */
children: React.ReactNode;
/** Additional classes added to the label. */
className?: string;
/** Color of the label. */
color?: 'blue' | 'cyan' | 'green' | 'orange' | 'purple' | 'red' | 'grey';
/** Variant of the label. */
variant?: 'outline' | 'filled';
/** Icon added to the left of the label text. */
icon?: React.ReactNode;
/** Close click callback for removable labels. If present, label will have a close button. */
onClose?: (event: React.MouseEvent) => void;
/** Node for custom close button. */
closeBtn?: React.ReactNode;
/** Additional properties for the default close button. */
closeBtnProps?: any;
/** Href for a label that is a link. If present, the label will change to an anchor element. */
href?: string;
}

export const Label: React.FunctionComponent<LabelProps> = ({ children, className = '', ...props }: LabelProps) => (
<span {...props} className={css(styles.label, className)}>
{children}
</span>
);
const colorStyles = {
blue: styles.modifiers.blue,
cyan: styles.modifiers.cyan,
green: styles.modifiers.green,
orange: styles.modifiers.orange,
purple: styles.modifiers.purple,
red: styles.modifiers.red,
grey: ''
};

export const Label: React.FunctionComponent<LabelProps> = ({
children,
className = '',
color = 'grey',
variant = 'filled',
icon,
onClose,
closeBtn,
closeBtnProps,
href,
...props
}: LabelProps) => {
const Component = href ? 'a' : 'span';
const button = closeBtn ? (
closeBtn
) : (
<Button
type="button"
variant="plain"
onClick={onClose}
{...{ 'aria-label': 'label-close-button', ...closeBtnProps }}
>
<TimesIcon />
</Button>
);

return (
<span
{...props}
className={css(styles.label, colorStyles[color], variant === 'outline' && styles.modifiers.outline, className)}
>
<Component className={css(styles.labelContent)} {...(href && { href })}>
{icon && <span className={css(styles.labelIcon)}>{icon}</span>}
{children}
</Component>
{onClose && button}
</span>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ exports[`Label should match snapshot (auto-generated) 1`] = `
className="pf-c-label ''"
isCompact={false}
>
<div>
ReactNode
</div>
<span
className="pf-c-label__content"
>
<div>
ReactNode
</div>
</span>
</span>
`;
37 changes: 35 additions & 2 deletions packages/react-core/src/components/Label/__tests__/Label.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,46 @@ import { Label } from '../Label';
test('label', () => {
const view = shallow(<Label>Something</Label>);
expect(view).toMatchSnapshot();
const outline = shallow(<Label variant="outline">Something</Label>);
expect(outline).toMatchSnapshot();
});

test('compact label', () => {
const view = shallow(<Label isCompact>Something</Label>);
test('label with href', () => {
const view = shallow(<Label href="#">Something</Label>);
expect(view).toMatchSnapshot();
const outline = shallow(
<Label href="#" variant="outline">
Something
</Label>
);
expect(outline).toMatchSnapshot();
});

test('label with close button', () => {
const view = shallow(<Label onClose={jest.fn()}>Something</Label>);
expect(view).toMatchSnapshot();
const outline = shallow(
<Label onClose={jest.fn()} variant="outline">
Something
</Label>
);
expect(outline).toMatchSnapshot();
});

['blue', 'cyan', 'green', 'orange', 'purple', 'red', 'grey'].forEach(
(color: 'blue' | 'cyan' | 'green' | 'orange' | 'purple' | 'red' | 'grey') =>
test(`label with ${color} color`, () => {
const view = shallow(<Label color={color}>Something</Label>);
expect(view).toMatchSnapshot();
const outline = shallow(
<Label color={color} variant="outline">
Something
</Label>
);
expect(outline).toMatchSnapshot();
})
);

test('label with additional class name', () => {
const view = shallow(<Label className="klass1">Something</Label>);
expect(view).toMatchSnapshot();
Expand Down
Loading