Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chip] Add containerElement property #6237

Merged
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
12 changes: 11 additions & 1 deletion src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Chip extends Component {
* CSS `className` of the root element.
*/
className: PropTypes.node,
/**
* The element to use as the container for the Chip. Either a string to
* use a DOM element or a ReactElement.
*/
containerElement: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
/**
* Override the label color.
*/
Expand Down Expand Up @@ -103,6 +111,7 @@ class Chip extends Component {
};

static defaultProps = {
containerElement: 'div', // Firefox doesn't support nested buttons
onBlur: () => {},
onFocus: () => {},
onKeyDown: () => {},
Expand Down Expand Up @@ -234,6 +243,7 @@ class Chip extends Component {

const {
children: childrenProp,
containerElement,
style,
className,
labelStyle,
Expand Down Expand Up @@ -278,7 +288,7 @@ class Chip extends Component {
{...other}
{...buttonEventHandlers}
className={className}
containerElement="div" // Firefox doesn't support nested buttons
containerElement={containerElement}
disableTouchRipple={true}
disableFocusRipple={true}
style={Object.assign(styles.root, style)}
Expand Down
27 changes: 27 additions & 0 deletions src/Chip/Chip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,31 @@ describe('<Chip />', () => {
});
});
});

describe('prop: containerElement', () => {
it('should use div if no containerElement specified', () => {
const wrapper = themedShallow(
<Chip>Label</Chip>
);
const button = wrapper.dive({context: {muiTheme}});
assert.strictEqual(button.is('div'), true, 'should match an div element');
});

it('should use the given string containerElement prop', () => {
const wrapper = themedShallow(
<Chip containerElement="span">Label</Chip>
);
const button = wrapper.dive({context: {muiTheme}});
assert.strictEqual(button.is('span'), true, 'should match an span element');
});

it('should use the given ReactElement containerElement prop', () => {
const CustomElement = (props) => <a {...props} />;
const wrapper = themedShallow(
<Chip containerElement={<CustomElement />}>Label</Chip>
);
const button = wrapper.dive({context: {muiTheme}});
assert.strictEqual(button.is(CustomElement), true, 'should match the custom element');
});
});
});