Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 796850b

Browse files
guguMatt Goo
authored and
Matt Goo
committed
feat(button): trailing icon support (#622)
1 parent bf487d7 commit 796850b

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"devDependencies": {
6161
"@google-cloud/storage": "^1.6.0",
62-
"@material/button": "^0.41.0",
62+
"@material/button": "^0.43.0",
6363
"@material/card": "^0.41.0",
6464
"@material/checkbox": "^0.41.0",
6565
"@material/chips": "^0.41.0",

packages/button/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ unelevated | Boolean | Enables unelevated variant.
4848
outlined | Boolean | Enables outlined variant.
4949
dense | Boolean | Enables dense variant.
5050
icon | Element | Icon to render within root element.
51+
trailingIcon | Element | Icon to render on the right side of the element
5152
children | String | Text to be displayed within root element.
5253
disabled | Boolean | Disables button if true.
5354
href | String | Sets a hyperlink & uses anchor tag instead of a button.

packages/button/index.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface ButtonProps<T extends ButtonTypes> extends Ripple.InjectedProps
3737
className?: string;
3838
icon?: React.ReactElement<React.HTMLProps<HTMLOrSVGElement>>;
3939
href?: string;
40+
trailingIcon?: React.ReactElement<React.HTMLProps<HTMLOrSVGElement>>;
4041
}
4142

4243
export const Button = <T extends ButtonTypes>(
@@ -51,6 +52,7 @@ export const Button = <T extends ButtonTypes>(
5152
href,
5253
children,
5354
initRipple,
55+
trailingIcon,
5456
// eslint disabled since we do not want to include
5557
// this in ...otherProps.
5658
// if unbounded is passed to the <button> element, it will throw
@@ -74,16 +76,22 @@ export const Button = <T extends ButtonTypes>(
7476
if (href) {
7577
return (
7678
<a {...props as React.HTMLProps<HTMLAnchorElement>} href={href}>
77-
{renderIcon(icon)}
78-
{children}
79+
{!trailingIcon ? renderIcon(icon) : null}
80+
<span className='mdc-button__label'>
81+
{children}
82+
</span>
83+
{trailingIcon ? renderIcon(trailingIcon) : null}
7984
</a>
8085
);
8186
}
8287

8388
return (
8489
<button {...props as React.HTMLProps<HTMLButtonElement>}>
85-
{renderIcon(icon)}
86-
{children}
90+
{!trailingIcon ? renderIcon(icon) : null}
91+
<span className='mdc-button__label'>
92+
{children}
93+
</span>
94+
{trailingIcon ? renderIcon(trailingIcon) : null}
8795
</button>
8896
);
8997
};

packages/button/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"url": "https://github.com/material-components/material-components-web-react.git"
1818
},
1919
"dependencies": {
20-
"@material/button": "^0.41.0",
20+
"@material/button": "^0.43.0",
2121
"@material/react-ripple": "^0.8.0",
2222
"classnames": "^2.2.5",
2323
"react": "^16.4.2"

test/screenshot/button/index.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ const ButtonScreenshotTest = () => {
6767
Svg Icon
6868
</Button>
6969
</div>
70+
71+
<div className='button-container'>
72+
<Button raised trailingIcon={<MaterialIcon icon='menu' />}>
73+
Raised
74+
</Button>
75+
</div>
76+
77+
<div className='button-container'>
78+
<Button raised trailingIcon={svgIcon}>
79+
Svg Icon
80+
</Button>
81+
</div>
7082
</div>
7183
);
7284
};

test/screenshot/golden.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"button": "8941ec5719bb5c82418d335f37dd6e4d523bf8a1121b50a4df4c5a784ca41fbe",
2+
"button": "57cb8769600669ec4d44dd23fcf2f6ded528ce8eec612d832c2b9e0271a640c3",
33
"card": "b2fd82763c383be438ff6578083bf9009711c7470333d07eb916ab690fc42d31",
44
"checkbox": "9c61177f0f927e178e7c6687d74cdfa08abc15ea8fc3c381f570b7c7d1f46d2a",
55
"chips": "e100a23df0b92c37920127c62d7d694ce3fe40c101c0ed05d535f5cafee62b27",

test/unit/button/index.test.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('classNames adds classes', () => {
1313
assert.isTrue(wrapper.hasClass('mdc-button'));
1414
});
1515

16-
test('does not render icon if props.icon is null', () => {
16+
test('does not render icon if props.icon is null and props.trailingIcon is null', () => {
1717
const wrapper = shallow(<Button />);
1818
assert.equal(wrapper.find('.mdc-button__icon').length, 0);
1919
});
@@ -24,6 +24,18 @@ test('renders an icon', () => {
2424
assert.isTrue(wrapper.find('.test-icon').hasClass('mdc-button__icon'));
2525
});
2626

27+
test('renders a trailing icon', () => {
28+
const icon = <i className='test-icon' />;
29+
const wrapper = shallow(<Button trailingIcon={icon} />);
30+
assert.isTrue(wrapper.find('.test-icon').hasClass('mdc-button__icon'));
31+
});
32+
33+
test('renders a link with trailing icon', () => {
34+
const icon = <i className='test-icon' />;
35+
const wrapper = shallow(<Button href='/' trailingIcon={icon} />);
36+
assert.isTrue(wrapper.find('.test-icon').hasClass('mdc-button__icon'));
37+
});
38+
2739
test('renders a raised button', () => {
2840
const wrapper = shallow(<Button raised />);
2941
assert.isTrue(wrapper.hasClass('mdc-button--raised'));

0 commit comments

Comments
 (0)