Skip to content

Commit c4effed

Browse files
authored
Adds support for data-* attributes to Button. (#2749)
1 parent c26d12f commit c4effed

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

components/button/__docs__/__snapshots__/storybook-stories.storyshot

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`DOM snapshots SLDSButton Aria attribute 1`] = `
4+
<div
5+
className="slds-p-around_medium"
6+
>
7+
<button
8+
aria-label="Base"
9+
className="slds-button"
10+
disabled={false}
11+
onClick={[Function]}
12+
type="button"
13+
>
14+
Base
15+
</button>
16+
</div>
17+
`;
18+
319
exports[`DOM snapshots SLDSButton Base 1`] = `
420
<div
521
className="slds-p-around_medium"
@@ -15,6 +31,22 @@ exports[`DOM snapshots SLDSButton Base 1`] = `
1531
</div>
1632
`;
1733

34+
exports[`DOM snapshots SLDSButton Data attribute 1`] = `
35+
<div
36+
className="slds-p-around_medium"
37+
>
38+
<button
39+
className="slds-button"
40+
data-some-property="Some value"
41+
disabled={false}
42+
onClick={[Function]}
43+
type="button"
44+
>
45+
Base
46+
</button>
47+
</div>
48+
`;
49+
1850
exports[`DOM snapshots SLDSButton Disabled 1`] = `
1951
<div
2052
className="slds-p-around_medium"

components/button/__docs__/storybook-stories.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ storiesOf(BUTTON, module)
2929
</div>
3030
))
3131
.add('Base', () => getButton({ label: 'Base', variant: 'base' }))
32+
.add('Aria attribute', () =>
33+
getButton({ label: 'Base', 'aria-label': 'Base', variant: 'base' })
34+
)
35+
.add('Data attribute', () =>
36+
getButton({
37+
label: 'Base',
38+
'data-some-property': 'Some value',
39+
variant: 'base',
40+
})
41+
)
3242
.add('Neutral', () => getButton({ label: 'Neutral' }))
3343
.add('Neutral with id', () =>
3444
getButton({ label: 'Neutral', id: 'custom-id' })

components/button/__tests__/button.browser-test.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ describe('SLDSButton: ', () => {
102102
});
103103
});
104104

105+
describe('Data Props Render ', () => {
106+
let cmp;
107+
let btn;
108+
109+
beforeEach(() => {
110+
cmp = getButton({
111+
id: 'custom-id',
112+
'data-some-attribute': 'some value',
113+
});
114+
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
115+
});
116+
117+
afterEach(() => {
118+
removeButton();
119+
});
120+
121+
it('renders data-some-attribute prop', () => {
122+
expect(btn.getAttribute('data-some-attribute')).to.equal('some value');
123+
});
124+
});
125+
105126
describe('Icon with Label Button Props Render', () => {
106127
let cmp;
107128
let btn;

components/button/index.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import componentDoc from './component.json';
1515
import Tooltip from '../tooltip';
1616

1717
import getAriaProps from '../../utilities/get-aria-props';
18+
import getDataProps from '../../utilities/get-data-props';
1819
import getFormProps from '../../utilities/get-form-props';
1920

2021
import { BUTTON } from '../../utilities/constants';
@@ -32,7 +33,7 @@ const defaultProps = {
3233
/**
3334
* The Button component is the Lightning Design System Button component. The Button should be used for label buttons, icon buttons, or buttons that have both labels and icons.
3435
* Either a <code>label</code> or <code>assistiveText.icon</code> is required; see the Prop Details table below. For buttons that maintain selected/unselected states, use the <a href="#/button-stateful">ButtonStateful</a> component.
35-
* Although not listed in the prop table, all `aria-*` and `form*` props will be added to the `button` element if passed in.
36+
* Although not listed in the prop table, all `aria-*`, `data-*` and `form*` props will be added to the `button` element if passed in.
3637
*/
3738
class Button extends React.Component {
3839
static displayName = BUTTON;
@@ -305,6 +306,7 @@ class Button extends React.Component {
305306

306307
renderButton = () => {
307308
const ariaProps = getAriaProps(this.props);
309+
const dataProps = getDataProps(this.props);
308310
const formProps = getFormProps(this.props);
309311

310312
return (
@@ -334,6 +336,7 @@ class Button extends React.Component {
334336
type={this.props.type || 'button'}
335337
style={this.props.style}
336338
{...ariaProps}
339+
{...dataProps}
337340
{...formProps}
338341
>
339342
{this.props.iconPosition === 'right' ? this.renderLabel() : null}

components/component-docs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,7 @@
20332033
"dependencies": []
20342034
},
20352035
"button": {
2036-
"description": "The Button component is the Lightning Design System Button component. The Button should be used for label buttons, icon buttons, or buttons that have both labels and icons.\nEither a <code>label</code> or <code>assistiveText.icon</code> is required; see the Prop Details table below. For buttons that maintain selected/unselected states, use the <a href=\"#/button-stateful\">ButtonStateful</a> component.\nAlthough not listed in the prop table, all `aria-*` and `form*` props will be added to the `button` element if passed in.",
2036+
"description": "The Button component is the Lightning Design System Button component. The Button should be used for label buttons, icon buttons, or buttons that have both labels and icons.\nEither a <code>label</code> or <code>assistiveText.icon</code> is required; see the Prop Details table below. For buttons that maintain selected/unselected states, use the <a href=\"#/button-stateful\">ButtonStateful</a> component.\nAlthough not listed in the prop table, all `aria-*`, `data-*` and `form*` props will be added to the `button` element if passed in.",
20372037
"methods": [
20382038
{
20392039
"name": "getClassName",

0 commit comments

Comments
 (0)