Skip to content

Commit c3e06ab

Browse files
kaushik94diasbruno
authored andcommitted
[added] additional data attributes.
1 parent e5a80d6 commit c3e06ab

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

Diff for: docs/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ import ReactModal from 'react-modal';
112112
labelledby: "heading",
113113
describedby: "full_description"
114114
}}
115+
/*
116+
Additional data attributes (optional).
117+
*/
118+
data={{
119+
background: "green"
120+
}}
115121
/*
116122
Overlay ref callback.
117123
*/

Diff for: specs/Modal.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,17 @@ export default () => {
393393
unmountModal();
394394
});
395395

396+
it("additional data attributes", () => {
397+
const modal = renderModal(
398+
{ isOpen: true, data: { background: "green" } },
399+
"hello"
400+
);
401+
mcontent(modal)
402+
.getAttribute("data-background")
403+
.should.be.eql("green");
404+
unmountModal();
405+
});
406+
396407
it("raises an exception if the appElement selector does not match", () => {
397408
should(() => ariaAppHider.setElement(".test")).throw();
398409
});

Diff for: src/components/Modal.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Modal extends Component {
6060
shouldReturnFocusAfterClose: PropTypes.bool,
6161
parentSelector: PropTypes.func,
6262
aria: PropTypes.object,
63+
data: PropTypes.object,
6364
role: PropTypes.string,
6465
contentLabel: PropTypes.string,
6566
shouldCloseOnEsc: PropTypes.bool,

Diff for: src/components/ModalPortal.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default class ModalPortal extends Component {
5151
role: PropTypes.string,
5252
contentLabel: PropTypes.string,
5353
aria: PropTypes.object,
54+
data: PropTypes.object,
5455
children: PropTypes.node,
5556
shouldCloseOnEsc: PropTypes.bool,
5657
overlayRef: PropTypes.func,
@@ -315,9 +316,9 @@ export default class ModalPortal extends Component {
315316
: className;
316317
};
317318

318-
ariaAttributes = items =>
319+
attributesFromObject = (prefix, items) =>
319320
Object.keys(items).reduce((acc, name) => {
320-
acc[`aria-${name}`] = items[name];
321+
acc[`${prefix}-${name}`] = items[name];
321322
return acc;
322323
}, {});
323324

@@ -346,8 +347,8 @@ export default class ModalPortal extends Component {
346347
onClick={this.handleContentOnClick}
347348
role={this.props.role}
348349
aria-label={this.props.contentLabel}
349-
{...this.ariaAttributes(this.props.aria || {})}
350-
data-testid={this.props.testId}
350+
{...this.attributesFromObject("aria", this.props.aria || {})}
351+
{...this.attributesFromObject("data", this.props.data || {})}
351352
>
352353
{this.props.children}
353354
</div>

0 commit comments

Comments
 (0)