Skip to content

Commit f4db71d

Browse files
committed
wip
1 parent 6df04cc commit f4db71d

File tree

4 files changed

+58
-48
lines changed

4 files changed

+58
-48
lines changed

packages/patternfly-4/react-core/src/components/Chip/Chip.d.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ import { SFC, HTMLProps } from 'react';
22
import { Omit, OneOf } from '../../typeUtils';
33
import { TooltipPosition } from '../Tooltip';
44

5-
export const ChipVariant = {
6-
overflow: 'overflow',
7-
closable: 'closable'
8-
};
9-
10-
export interface ChipProps extends Omit<HTMLProps<HTMLDivElement>, 'children' > {
5+
export interface ChipProps extends Omit<HTMLProps<HTMLDivElement> {
116
children: string;
12-
isOverflowed?: boolean;
13-
variant: OneOf<typeof ChipVariant, keyof typeof ChipVariant>;
7+
isOverflow: boolean;
148
position: OneOf<typeof TooltipPosition, keyof typeof TooltipPosition>;
159
}
1610

packages/patternfly-4/react-core/src/components/Chip/Chip.js

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,81 @@ import ChipButton from './ChipButton';
55
import { Tooltip, TooltipPosition } from '../Tooltip';
66
import { TimesCircleIcon } from '@patternfly/react-icons';
77
import styles from '@patternfly/patternfly-next/components/Chip/chip.css';
8-
import { getUniqueId } from '../../internal/util';
8+
import GenerateId from '../../internal/GenerateId/GenerateId';
99

10-
export const ChipVariant = {
11-
overflow: 'overflow',
12-
closable: 'closable'
13-
};
14-
15-
const Chip = ({ variant, onClick, children, id, position, className, ...props }) => {
16-
const idChip = id || getUniqueId()
17-
switch (variant) {
18-
case ChipVariant.overflow:
19-
return (
20-
<div className={css(styles.chip, styles.modifiers.overflow, className)} {...props}>
21-
<ChipButton onClick={onClick} aria-label="Expand chip">
22-
<span className={css(styles.chipText)}>{children}</span>
23-
</ChipButton>
24-
</div>
25-
);
26-
default:
27-
const ChipComponent = (
28-
<div className={css(styles.chip, className)} {...props}>
29-
<span className={css(styles.chipText)} id={idChip}>
30-
{children}
31-
</span>
32-
<ChipButton onClick={onClick} aria-label="Remove" id={`remove_${idChip}`} aria-labelledby={`remove_${idChip} ${idChip}`}>
33-
<TimesCircleIcon aria-hidden="true" />
34-
</ChipButton>
35-
</div>
36-
);
37-
return children.length < 16 ? (
38-
ChipComponent
39-
) : (
10+
class Chip extends React.Component {
11+
renderOverflowChip = () => {
12+
const { children, className, onClick } = this.props;
13+
return (
14+
<div className={css(styles.chip, styles.modifiers.overflow, className)}>
15+
<ChipButton onClick={onClick} aria-label="Expand Chip">
16+
<span className={css(styles.chipText)}>{children}</span>
17+
</ChipButton>
18+
</div>
19+
);
20+
}
21+
renderChip = (randomId) => {
22+
const { children, position, className, onClick, minTooltipChars } = this.props;
23+
const ChipComponent = (
24+
<div className={css(styles.chip, className)}>
25+
<span className={css(styles.chipText)} id={randomId}>
26+
{children}
27+
</span>
28+
<ChipButton onClick={onClick} aria-label="Remove" id={`remove_${randomId}`} aria-labelledby={`remove_${randomId} ${randomId}`}>
29+
<TimesCircleIcon aria-hidden="true" />
30+
</ChipButton>
31+
</div>
32+
);
33+
return children.length < minTooltipChars ? (
34+
ChipComponent
35+
) : (
4036
<Tooltip position={position} content={children}>
4137
{ChipComponent}
4238
</Tooltip>
4339
);
40+
4441
}
45-
};
4642

43+
render() {
44+
const {
45+
isOverflow,
46+
} = this.props;
47+
return (
48+
<GenerateId>
49+
{(randomId) =>
50+
(
51+
<React.Fragment>
52+
{isOverflow ? this.renderOverflowChip() : this.renderChip(randomId)}
53+
</React.Fragment>
54+
)
55+
}
56+
</GenerateId>
57+
)
58+
}
59+
}
4760
Chip.propTypes = {
4861
/** Content rendered inside the chip text */
4962
children: PropTypes.string.isRequired,
5063
/** ID of the chip */
5164
id: PropTypes.string,
5265
/** Additional classes added to the chip item */
5366
className: PropTypes.string,
54-
/** Builds the chip structure according to variant */
55-
variant: PropTypes.oneOf(Object.values(ChipVariant)),
67+
/** Flag indicating if the chip has overflow*/
68+
isOverflow: PropTypes.bool,
5669
/** Position of the tooltip which is displayed if text is longer */
5770
position: PropTypes.oneOf(Object.values(TooltipPosition)),
5871
/** Function that is called when clicking on the chip button */
59-
onClick: PropTypes.func
72+
onClick: PropTypes.func,
73+
/** Min character limit before showing tooltip. */
74+
minTooltipChars: PropTypes.number
6075
};
6176

6277
Chip.defaultProps = {
6378
id: undefined,
6479
className: '',
6580
position: 'top',
66-
variant: ChipVariant.closable
81+
isOverflow: false,
82+
minTooltipChars: 16
6783
};
6884

6985
export default Chip;

packages/patternfly-4/react-core/src/components/Chip/Chip.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('ChipButton', () => {
1515
describe('Chip', () => {
1616
test('overflow', () => {
1717
const view = shallow(
18-
<Chip className="my-chp-cls" variant="overflow">
18+
<Chip className="my-chp-cls" isOverflow={true}>
1919
4 more
2020
</Chip>
2121
);
@@ -24,7 +24,7 @@ describe('Chip', () => {
2424

2525
test('closable', () => {
2626
const view = shallow(
27-
<Chip className="my-chp-cls" variant="closable" id="chip_one">
27+
<Chip className="my-chp-cls" id="chip_one">
2828
Chip
2929
</Chip>
3030
);
@@ -34,7 +34,7 @@ describe('Chip', () => {
3434

3535
test('closable with tooltip', () => {
3636
const view = shallow(
37-
<Chip className="my-chp-cls" variant="closable" id="chip_one">
37+
<Chip className="my-chp-cls" id="chip_one">
3838
12345678901234567891
3939
</Chip>
4040
);

packages/patternfly-4/react-core/src/components/Chip/examples/SimpleChip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SimpleChip extends React.Component {
3333
)}
3434
<br />
3535
<br />
36-
<Chip variant="overflow">4 more</Chip>
36+
<Chip isOverflow={true}>4 more</Chip>
3737
</React.Fragment>
3838
);
3939
}

0 commit comments

Comments
 (0)