-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chipGroup): Add Chip Group component to pf4 (#1245)
- Loading branch information
1 parent
bb2c768
commit d3f2cc9
Showing
27 changed files
with
470 additions
and
116 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
packages/patternfly-4/react-core/src/components/Chip/Chip.docs.js
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
packages/patternfly-4/react-core/src/components/Chip/examples/SimpleChip.js
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/patternfly-4/react-core/src/components/Chip/index.d.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
packages/patternfly-4/react-core/src/components/ChipGroup/ChipGroup.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ReactNode, HTMLProps } from 'react'; | ||
|
||
export interface ChipGroupProps extends HTMLProps<HTMLDivElement> { | ||
children: ReactNode; | ||
collapsedText?: string; | ||
expandedText?: string; | ||
withToolbar?: boolean; | ||
} | ||
|
||
declare const ChipGroup: React.ComponentClass<ChipGroupProps>; | ||
|
||
export default ChipGroup; |
27 changes: 27 additions & 0 deletions
27
packages/patternfly-4/react-core/src/components/ChipGroup/ChipGroup.docs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ChipGroup, ChipGroupToolbarItem, Chip } from '@patternfly/react-core'; | ||
import Toolbar from './examples/ToolbarChipGroup'; | ||
import MultiSelect from './examples/MultiSelectChipGroup'; | ||
import BadgeChip from './examples/BadgeChip'; | ||
|
||
export default { | ||
title: 'ChipGroup', | ||
components: { | ||
ChipGroup, | ||
ChipGroupToolbarItem, | ||
Chip | ||
}, | ||
examples: [ | ||
{ | ||
component: Toolbar, | ||
title: 'Chip Group Toolbar' | ||
}, | ||
{ | ||
component: MultiSelect, | ||
title: 'Chip Group Multi-Select' | ||
}, | ||
{ | ||
component: BadgeChip, | ||
title: 'Badge Chip Group' | ||
} | ||
] | ||
}; |
90 changes: 90 additions & 0 deletions
90
packages/patternfly-4/react-core/src/components/ChipGroup/ChipGroup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import PropTypes from 'prop-types'; | ||
import styles from '@patternfly/patternfly/components/ChipGroup/chip-group.css'; | ||
import { Chip } from '@patternfly/react-core'; | ||
import { fillTemplate } from '../../helpers'; | ||
|
||
class ChipGroup extends React.Component { | ||
state = { | ||
isOpen: false | ||
}; | ||
|
||
toggleCollapse = () => { | ||
this.setState(prevState => ({ | ||
isOpen: !prevState.isOpen | ||
})); | ||
}; | ||
|
||
renderToolbarGroup() { | ||
const { isOpen } = this.state; | ||
return <InnerChipGroup {...this.props} isOpen={isOpen} onToggleCollapse={this.toggleCollapse} />; | ||
} | ||
|
||
renderChipGroup() { | ||
const { className } = this.props; | ||
const { isOpen } = this.state; | ||
return ( | ||
<div className={css(styles.chipGroup, className)}> | ||
<InnerChipGroup {...this.props} isOpen={isOpen} onToggleCollapse={this.toggleCollapse} /> | ||
</div> | ||
); | ||
} | ||
|
||
render() { | ||
const { withToolbar, children } = this.props; | ||
if (React.Children.count(children)) { | ||
return withToolbar ? this.renderToolbarGroup() : this.renderChipGroup(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
const InnerChipGroup = props => { | ||
const { children, expandedText, isOpen, onToggleCollapse, collapsedText } = props; | ||
|
||
const collapsedTextResult = fillTemplate(collapsedText, { remaining: React.Children.count(children) - 1 }); | ||
return ( | ||
<React.Fragment> | ||
{isOpen ? ( | ||
<React.Fragment>{children}</React.Fragment> | ||
) : ( | ||
<React.Fragment> | ||
{React.Children.map(children, (child, i) => { | ||
if (i === 0) return child; | ||
})} | ||
</React.Fragment> | ||
)} | ||
{React.Children.count(children) > 1 && ( | ||
<Chip isOverflowChip onClick={onToggleCollapse}> | ||
{isOpen ? expandedText : collapsedTextResult} | ||
</Chip> | ||
)} | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
ChipGroup.propTypes = { | ||
/** Content rendered inside the chip text */ | ||
children: PropTypes.node, | ||
/** Additional classes added to the chip item */ | ||
className: PropTypes.string, | ||
/** Customizable "Show Less" text string */ | ||
expandedText: PropTypes.string, | ||
/** | ||
* Customizeable template string. Use variable "${remaining}" for the overflow chip count. | ||
*/ | ||
collapsedText: PropTypes.string, | ||
/** Flag for grouping with a toolbar & category name */ | ||
withToolbar: PropTypes.bool | ||
}; | ||
|
||
ChipGroup.defaultProps = { | ||
children: null, | ||
className: '', | ||
expandedText: 'Show Less', | ||
collapsedText: '${remaining} more', | ||
withToolbar: false | ||
}; | ||
|
||
export default ChipGroup; |
44 changes: 44 additions & 0 deletions
44
packages/patternfly-4/react-core/src/components/ChipGroup/ChipGroup.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { shallow, mount } from 'enzyme'; | ||
import { Chip, ChipGroup, ChipGroupToolbarItem } from './index'; | ||
|
||
describe('ChipGroup', () => { | ||
test('chip group default', () => { | ||
const view = shallow( | ||
<ChipGroup> | ||
<Chip>1.1</Chip> | ||
</ChipGroup> | ||
); | ||
|
||
expect(view).toMatchSnapshot(); | ||
}); | ||
|
||
test('chip group with toolbar', () => { | ||
const view = shallow( | ||
<ChipGroup withToolbar> | ||
<ChipGroupToolbarItem> | ||
<Chip>1.1</Chip> | ||
</ChipGroupToolbarItem> | ||
</ChipGroup> | ||
); | ||
expect(view).toMatchSnapshot(); | ||
}); | ||
|
||
test('chip group expanded', () => { | ||
const view = mount( | ||
<ChipGroup> | ||
<Chip>1</Chip> | ||
<Chip>2</Chip> | ||
</ChipGroup> | ||
); | ||
const overflowButton = view.find('.pf-m-overflow .pf-c-chip__text'); | ||
expect(overflowButton.text()).toBe('1 more'); | ||
overflowButton.simulate('click'); | ||
expect(overflowButton.text()).toBe('Show Less'); | ||
}); | ||
|
||
test('chip group will not render if no children passed', () => { | ||
const view = shallow(<ChipGroup />); | ||
expect(view.equals(null)).toBe(true); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/patternfly-4/react-core/src/components/ChipGroup/ChipGroupToolbarItem.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { SFC, HTMLProps, ReactNode } from 'react'; | ||
|
||
export interface ChipGroupToolbarItemProps extends HTMLProps<HTMLDivElement> { | ||
categoryName?: string; | ||
} | ||
|
||
declare const ChipGroupToolbarItem: SFC<ChipGroupToolbarItemProps>; | ||
|
||
export default ChipGroupToolbarItem; |
Oops, something went wrong.