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

fix(button): Fix Component Selector Issue #474

Merged
merged 5 commits into from Feb 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions modules/button/react/lib/IconButton.tsx
Expand Up @@ -8,7 +8,7 @@ import {colors} from '@workday/canvas-kit-react-core';
import {SystemIcon} from '@workday/canvas-kit-react-icon';
import {focusRing, mouseFocusBehavior} from '@workday/canvas-kit-react-common';
import {CanvasSystemIcon} from '@workday/design-assets-types';
import {CSSObject} from '@emotion/core';
import {ClassNames, CSSObject} from '@emotion/core';

export interface IconButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
/**
Expand Down Expand Up @@ -68,6 +68,8 @@ function getAccentSelector(fillColor: string): CSSObject {
};
}

export const iconButtonIdentifier = 'wdc-ckr-icon-button';

export const IconButtonCon = styled('button', {
shouldForwardProp: prop => isPropValid(prop) && prop !== 'size',
})<IconButtonProps>(
Expand Down Expand Up @@ -243,20 +245,26 @@ export default class IconButton extends React.Component<IconButtonProps> {
icon,
toggled,
children,
className,
...elemProps
} = this.props;

return (
<IconButtonCon
toggled={toggled}
ref={buttonRef}
variant={variant}
size={size}
aria-pressed={toggled}
{...elemProps}
>
{icon ? <SystemIcon icon={icon} /> : children}
</IconButtonCon>
<ClassNames>
{({cx}) => (
<IconButtonCon
toggled={toggled}
ref={buttonRef}
variant={variant}
size={size}
aria-pressed={toggled}
className={cx(iconButtonIdentifier, className)}
{...elemProps}
>
{icon ? <SystemIcon icon={icon} /> : children}
</IconButtonCon>
)}
</ClassNames>
);
}
}
4 changes: 2 additions & 2 deletions modules/button/react/lib/IconButtonToggleGroup.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
import styled from '@emotion/styled';
import {spacing, borderRadius} from '@workday/canvas-kit-react-core';
import IconButton, {IconButtonCon, IconButtonProps} from './IconButton';
import IconButton, {IconButtonProps, iconButtonIdentifier} from './IconButton';

export interface IconButtonToggleGroupProps {
/**
Expand All @@ -28,7 +28,7 @@ export interface IconButtonToggleGroupProps {
}

const Container = styled('div')({
[IconButtonCon as any]: {
[`.${iconButtonIdentifier}`]: {
borderRadius: borderRadius.zero,
borderWidth: '1px',
marginLeft: '-1px',
Expand Down
23 changes: 22 additions & 1 deletion modules/button/react/spec/IconButton.spec.tsx
@@ -1,6 +1,7 @@
import * as React from 'react';
import {mount} from 'enzyme';
import IconButton from '../lib/IconButton';
import {render} from '@testing-library/react';
import IconButton, {iconButtonIdentifier} from '../lib/IconButton';
import {SystemIcon} from '@workday/canvas-kit-react-icon';
import {activityStreamIcon} from '@workday/canvas-system-icons-web';
import ReactDOMServer from 'react-dom/server';
Expand All @@ -22,6 +23,26 @@ describe('Icon Button', () => {
component.unmount();
});

it('should have an identifier class', () => {
const {getByRole} = render(
<IconButton aria-label="Activity Stream">
<SystemIcon icon={activityStreamIcon} />
</IconButton>
);

expect(getByRole('button').className).toContain(iconButtonIdentifier);
});
it('should compose custom classNames with the identifier class', () => {
const testClassName = 'test classname';
const {getByRole} = render(
<IconButton className={testClassName} aria-label="Activity Stream">
<SystemIcon icon={activityStreamIcon} />
</IconButton>
);

expect(getByRole('button').className).toContain(`${iconButtonIdentifier} ${testClassName}`);
});

test('should call a callback function', () => {
const component = mount(
<IconButton onClick={cb} aria-label="Activity Stream">
Expand Down