Skip to content

Commit

Permalink
[added] additional data attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik94 authored and diasbruno committed Jul 4, 2018
1 parent e5a80d6 commit c3e06ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ import ReactModal from 'react-modal';
labelledby: "heading",
describedby: "full_description"
}}
/*
Additional data attributes (optional).
*/
data={{
background: "green"
}}
/*
Overlay ref callback.
*/
Expand Down
11 changes: 11 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ export default () => {
unmountModal();
});

it("additional data attributes", () => {
const modal = renderModal(
{ isOpen: true, data: { background: "green" } },
"hello"
);
mcontent(modal)
.getAttribute("data-background")
.should.be.eql("green");
unmountModal();
});

it("raises an exception if the appElement selector does not match", () => {
should(() => ariaAppHider.setElement(".test")).throw();
});
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Modal extends Component {
shouldReturnFocusAfterClose: PropTypes.bool,
parentSelector: PropTypes.func,
aria: PropTypes.object,
data: PropTypes.object,
role: PropTypes.string,
contentLabel: PropTypes.string,
shouldCloseOnEsc: PropTypes.bool,
Expand Down
9 changes: 5 additions & 4 deletions src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class ModalPortal extends Component {
role: PropTypes.string,
contentLabel: PropTypes.string,
aria: PropTypes.object,
data: PropTypes.object,
children: PropTypes.node,
shouldCloseOnEsc: PropTypes.bool,
overlayRef: PropTypes.func,
Expand Down Expand Up @@ -315,9 +316,9 @@ export default class ModalPortal extends Component {
: className;
};

ariaAttributes = items =>
attributesFromObject = (prefix, items) =>
Object.keys(items).reduce((acc, name) => {
acc[`aria-${name}`] = items[name];
acc[`${prefix}-${name}`] = items[name];
return acc;
}, {});

Expand Down Expand Up @@ -346,8 +347,8 @@ export default class ModalPortal extends Component {
onClick={this.handleContentOnClick}
role={this.props.role}
aria-label={this.props.contentLabel}
{...this.ariaAttributes(this.props.aria || {})}
data-testid={this.props.testId}
{...this.attributesFromObject("aria", this.props.aria || {})}
{...this.attributesFromObject("data", this.props.data || {})}
>
{this.props.children}
</div>
Expand Down

0 comments on commit c3e06ab

Please sign in to comment.