Skip to content

Commit b27c23f

Browse files
Card component destructure aria data props (#3071)
* Add functionality to destructure aria and data props on SLDSCard component * Add test to ensure aria and data props are correctly destructured on SLDSCard component * Add aria and data props to storybook story * Update snapshot to ensure aria and data props display in stories * Add updated component-docs file * try different approach for failing unit test
1 parent ca399f6 commit b27c23f

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ exports[`DOM snapshots SLDSCard Custom Header 1`] = `
88
className="slds-grid slds-grid_vertical"
99
>
1010
<article
11+
aria-label="SLDSCard Component"
1112
className="slds-card"
13+
data-description="Description of the Card component"
1214
id="ExampleCard"
1315
>
1416
<div
@@ -247,7 +249,9 @@ exports[`DOM snapshots SLDSCard Custom Heading 1`] = `
247249
className="slds-grid slds-grid_vertical"
248250
>
249251
<article
252+
aria-label="SLDSCard Component"
250253
className="slds-card"
254+
data-description="Description of the Card component"
251255
id="ExampleCard"
252256
>
253257
<div
@@ -662,7 +666,9 @@ exports[`DOM snapshots SLDSCard Empty 1`] = `
662666
className="slds-grid slds-grid_vertical"
663667
>
664668
<article
669+
aria-label="SLDSCard Component"
665670
className="slds-card"
671+
data-description="Description of the Card component"
666672
id="ExampleCard"
667673
>
668674
<div
@@ -866,7 +872,9 @@ exports[`DOM snapshots SLDSCard w/ Items 1`] = `
866872
className="slds-grid slds-grid_vertical"
867873
>
868874
<article
875+
aria-label="SLDSCard Component"
869876
className="slds-card"
877+
data-description="Description of the Card component"
870878
id="ExampleCard"
871879
>
872880
<div

components/card/__docs__/storybook-stories.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class DemoCard extends React.Component {
114114
heading={heading}
115115
icon={<Icon category="standard" name="document" size="small" />}
116116
empty={isEmpty ? <CardEmpty heading="No Related Items" /> : null}
117+
aria-label="SLDSCard Component"
118+
data-description="Description of the Card component"
117119
>
118120
<DataTable id="SLDSDataTableExample-1" items={items}>
119121
<DataTableColumn label="Opportunity Name" property="name" truncate>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ describe('Card: ', () => {
118118
name: 'default',
119119
size: 'small',
120120
});
121+
const ariaLabel = 'aria-label';
122+
const dataLabel = 'data-label';
121123

122124
const optionalProps = assign(requiredProps, {
123125
bodyClassName: 'this-is-a-custom-body-class',
@@ -127,6 +129,8 @@ describe('Card: ', () => {
127129
filter: renderFilter,
128130
icon: renderIcon,
129131
style: { background: 'rgb(18, 49, 35)' },
132+
[ariaLabel]: ariaLabel,
133+
[dataLabel]: dataLabel,
130134
});
131135

132136
describe('Optional Structure', () => {
@@ -187,6 +191,16 @@ describe('Card: ', () => {
187191
)[0];
188192
headerActionsChildren.should.not.be.undefined;
189193
});
194+
195+
it('correctly destructures `aria-` props', function () {
196+
const card = getCard(this.dom);
197+
card.getAttribute(ariaLabel).should.equal(ariaLabel);
198+
});
199+
200+
it('correctly destructures `data-` props', function () {
201+
const card = getCard(this.dom);
202+
card.dataset.label.should.equal(dataLabel);
203+
});
190204
});
191205

192206
describe('Accepts a custom node as heading', () => {

components/card/index.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Footer from './private/footer';
2323
import Empty from './empty';
2424

2525
import { CARD } from '../../utilities/constants';
26+
import getAriaProps from '../../utilities/get-aria-props';
27+
import getDataProps from '../../utilities/get-data-props';
2628

2729
const idSuffixes = {
2830
body: '__body',
@@ -32,9 +34,11 @@ const idSuffixes = {
3234
};
3335

3436
/**
35-
* Cards are used to apply a container around a related grouping of information. It has a header, a body, and an optional footer. It often contains a DataTable or Tile (coming soon). Actions associated with selected items or with all items are included within the header actions. Footer often contains pagination.
37+
* Cards are used to apply a container around a related grouping of information. It has a header, a body, and an optional footer. It often contains a DataTable or Tile (coming soon). Actions associated with selected items or with all items are included within the header actions. Footer often contains pagination. `aria-` and `data-` props can be provided and will be destructured on the root `article` element.
3638
*/
3739
const Card = (props) => {
40+
const ariaProps = getAriaProps(props);
41+
const dataProps = getDataProps(props);
3842
const bodyId = props.id ? props.id + idSuffixes.body : null;
3943
const filterId = props.id ? props.id + idSuffixes.filter : null;
4044
const headingId = props.id ? props.id + idSuffixes.heading : null;
@@ -51,6 +55,8 @@ const Card = (props) => {
5155
id={props.id}
5256
className={classnames('slds-card', props.className)}
5357
style={props.style}
58+
{...ariaProps}
59+
{...dataProps}
5460
>
5561
{!props.hasNoHeader && (
5662
<Header

components/component-docs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@
24972497
"dependencies": []
24982498
},
24992499
"card": {
2500-
"description": "Cards are used to apply a container around a related grouping of information. It has a header, a body, and an optional footer. It often contains a DataTable or Tile (coming soon). Actions associated with selected items or with all items are included within the header actions. Footer often contains pagination.",
2500+
"description": "Cards are used to apply a container around a related grouping of information. It has a header, a body, and an optional footer. It often contains a DataTable or Tile (coming soon). Actions associated with selected items or with all items are included within the header actions. Footer often contains pagination. `aria-` and `data-` props can be provided and will be destructured on the root `article` element.",
25012501
"methods": [],
25022502
"props": {
25032503
"bodyClassName": {

0 commit comments

Comments
 (0)