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

feat(react): update wrapComponent to bring in usePrefix #11575

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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import wrapComponent from '../../tools/wrapComponent';
const TableActionList = wrapComponent({
name: 'TableActionList',
type: 'div',
className: `cds--action-list`,
className: (prefix) => `${prefix}--action-list`,
});

export default TableActionList;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import wrapComponent from '../../tools/wrapComponent';
const TableToolbarContent = wrapComponent({
name: 'TableToolbarContent',
type: 'div',
className: `cds--toolbar-content`,
className: (prefix) => `${prefix}--toolbar-content`,
});

export default TableToolbarContent;
2 changes: 1 addition & 1 deletion packages/react/src/components/UIShell/HeaderGlobalBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import wrapComponent from '../../tools/wrapComponent';
*/
export default wrapComponent({
name: 'HeaderGlobalBar',
className: `cds--header__global`,
className: (prefix) => `${prefix}--header__global`,
type: 'div',
});
58 changes: 58 additions & 0 deletions packages/react/src/tools/__tests__/wrapComponent-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { render } from '@testing-library/react';
import React from 'react';
import wrapComponent from '../wrapComponent';

describe('wrapComponent', () => {
it('should render the outermost element as the given type', () => {
const WrappedComponent = wrapComponent({
name: 'WrappedComponent',
type: 'div',
});
const { container } = render(<WrappedComponent />);
expect(container.firstChild.tagName).toBe('DIV');
});

it('should set the `displayName` for a component', () => {
const WrappedComponent = wrapComponent({
name: 'WrappedComponent',
type: 'div',
});
expect(WrappedComponent.displayName).toBe('WrappedComponent');
});

it('should support static class names with `className`', () => {
const WrappedComponent = wrapComponent({
name: 'WrappedComponent',
type: 'div',
className: 'test',
});
const { container } = render(<WrappedComponent />);
expect(container.firstChild).toHaveClass('test');
});

it('should support prefix class names with `className`', () => {
const WrappedComponent = wrapComponent({
name: 'WrappedComponent',
type: 'div',
className: (prefix) => `${prefix}--test`,
});
const { container } = render(<WrappedComponent />);
expect(container.firstChild).toHaveClass('cds--test');
});

it('should spread additional props on the outermost node', () => {
const WrappedComponent = wrapComponent({
name: 'WrappedComponent',
type: 'div',
});
const { container } = render(<WrappedComponent data-testid="test" />);
expect(container.firstChild).toHaveAttribute('data-testid', 'test');
});
});
16 changes: 12 additions & 4 deletions packages/react/src/tools/wrapComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { usePrefix } from '../internal/usePrefix';

const wrapComponent = ({ name, className: getClassName, type }) => {
function Component({ className: baseClassName, ...other }) {
const prefix = usePrefix();
const componentClass = cx(
typeof getClassName === 'function' ? getClassName(prefix) : getClassName,
baseClassName
);

const wrapComponent = ({ name, className, type }) => {
const Component = ({ className: baseClassName, ...other }) => {
const componentClass = cx(className, baseClassName);
return React.createElement(type, {
...other,
// Prevent Weird quirk where `cx` will evaluate to an empty string, '',
// and so we have empty `class` attributes in the resulting markup
// eslint-disable-next-line no-extra-boolean-cast
className: !!componentClass ? componentClass : undefined,
});
};
}

Component.displayName = name;
Component.propTypes = {
className: PropTypes.string,
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
};

return Component;
};

Expand Down