Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit ade7571

Browse files
authored
Merge pull request #6 from rpearce/forward-refs
Handle forwarded refs
2 parents 9f81a6c + 9b181a2 commit ade7571

File tree

7 files changed

+364
-212
lines changed

7 files changed

+364
-212
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"func-call-spacing": [ "error", "never" ],
3030
"indent": [ "error", 2 ],
3131
"linebreak-style": [ "error", "unix" ],
32+
"no-console": "off",
3233
"no-else-return": [ "error" ],
3334
"no-empty-function": [ "error" ],
3435
"no-multiple-empty-lines": [2, { "max": 1, "maxEOF": 1 }],

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ typings/
6161
.next
6262

6363
src/
64+
*.tmp

docs/app.js

+56-4
Original file line numberDiff line numberDiff line change
@@ -23493,9 +23493,34 @@ function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.itera
2349323493
Object.defineProperty(exports, "__esModule", {
2349423494
value: true
2349523495
});
23496-
exports.default = void 0;
23496+
exports.default = exports.ButtonA11y = void 0;
2349723497

23498-
var _react = require("react");
23498+
var _react = _interopRequireWildcard(require("react"));
23499+
23500+
function _interopRequireWildcard(obj) {
23501+
if (obj && obj.__esModule) {
23502+
return obj;
23503+
} else {
23504+
var newObj = {};
23505+
23506+
if (obj != null) {
23507+
for (var key in obj) {
23508+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
23509+
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
23510+
23511+
if (desc.get || desc.set) {
23512+
Object.defineProperty(newObj, key, desc);
23513+
} else {
23514+
newObj[key] = obj[key];
23515+
}
23516+
}
23517+
}
23518+
}
23519+
23520+
newObj.default = obj;
23521+
return newObj;
23522+
}
23523+
}
2349923524

2350023525
function _typeof(obj) {
2350123526
if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") {
@@ -23511,6 +23536,24 @@ function _typeof(obj) {
2351123536
return _typeof(obj);
2351223537
}
2351323538

23539+
function _extends() {
23540+
_extends = Object.assign || function (target) {
23541+
for (var i = 1; i < arguments.length; i++) {
23542+
var source = arguments[i];
23543+
23544+
for (var key in source) {
23545+
if (Object.prototype.hasOwnProperty.call(source, key)) {
23546+
target[key] = source[key];
23547+
}
23548+
}
23549+
}
23550+
23551+
return target;
23552+
};
23553+
23554+
return _extends.apply(this, arguments);
23555+
}
23556+
2351423557
function _objectSpread(target) {
2351523558
for (var i = 1; i < arguments.length; i++) {
2351623559
var source = arguments[i] != null ? arguments[i] : {};
@@ -23695,8 +23738,9 @@ function (_PureComponent) {
2369523738
var _this$props2 = this.props,
2369623739
children = _this$props2.children,
2369723740
element = _this$props2.element,
23741+
forwardedRef = _this$props2.forwardedRef,
2369823742
strictMode = _this$props2.strictMode,
23699-
rest = _objectWithoutProperties(_this$props2, ["children", "element", "strictMode"]);
23743+
rest = _objectWithoutProperties(_this$props2, ["children", "element", "forwardedRef", "strictMode"]);
2370023744

2370123745
if (strictMode && !children && !rest['aria-label']) {
2370223746
throw new Error('react-button-a11y: `aria-label` required for accessibility');
@@ -23705,6 +23749,7 @@ function (_PureComponent) {
2370523749
return (0, _react.createElement)(element, _objectSpread({}, rest, {
2370623750
children: children,
2370723751
onKeyDown: this.handleKeyDown,
23752+
ref: forwardedRef,
2370823753
role: 'button',
2370923754
tabIndex: '0'
2371023755
}));
@@ -23714,12 +23759,19 @@ function (_PureComponent) {
2371423759
return ButtonA11y;
2371523760
}(_react.PureComponent);
2371623761

23717-
exports.default = ButtonA11y;
23762+
exports.ButtonA11y = ButtonA11y;
2371823763
ButtonA11y.defaultProps = {
2371923764
element: 'div',
2372023765
onClick: Function.prototype,
2372123766
onKeyDown: Function.prototype,
2372223767
strictMode: true
2372323768
};
23769+
var WrappedComponent = (0, _react.forwardRef)(function (props, ref) {
23770+
return _react.default.createElement(ButtonA11y, _extends({}, props, {
23771+
forwardedRef: ref
23772+
}));
23773+
});
23774+
var _default = WrappedComponent;
23775+
exports.default = _default;
2372423776

2372523777
},{"react":12}]},{},[1]);

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"src/"
3434
],
3535
"scripts": {
36-
"build": "npm run lint && npm run build:js && npm run build:docs",
36+
"build": "npm run build:js && npm run build:docs",
3737
"build:docs": "browserify example/app.js -o docs/app.js -t [ babelify ]",
3838
"build:js": "babel source --out-dir src",
3939
"contributors:add": "all-contributors add",
4040
"contributors:generate": "all-contributors generate",
4141
"coverage": "jest --coverage --coverageReporters=text-lcov | coveralls",
4242
"lint": "eslint --ext .js .",
43-
"prepublishOnly": "npm run build",
43+
"prepublishOnly": "npm run lint && npm run build",
4444
"test": "npm run lint && npm run build && jest --coverage"
4545
},
4646
"jest": {
@@ -75,8 +75,8 @@
7575
"react-dom": "^16.6.3"
7676
},
7777
"peerDependencies": {
78-
"react": ">= 15",
79-
"react-dom": ">= 15"
78+
"react": ">= 16.3",
79+
"react-dom": ">= 16.3"
8080
},
8181
"dependencies": {
8282
"@babel/polyfill": "^7.0.0"

source/react-button-a11y.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PureComponent, createElement } from 'react'
1+
import React, { PureComponent, createElement, forwardRef } from 'react'
22

33
// isSelect :: KeyboardEvent -> Bool
44
const isSelect = e =>
@@ -8,7 +8,7 @@ const isSelect = e =>
88
e.keyCode === 13 ||
99
e.keyCode === 32
1010

11-
export default class ButtonA11y extends PureComponent {
11+
export class ButtonA11y extends PureComponent {
1212
constructor(...params) {
1313
super(...params)
1414
this.handleKeyDown = this.handleKeyDown.bind(this)
@@ -26,7 +26,7 @@ export default class ButtonA11y extends PureComponent {
2626
}
2727

2828
render() {
29-
const { children, element, strictMode, ...rest } = this.props
29+
const { children, element, forwardedRef, strictMode, ...rest } = this.props
3030

3131
if (strictMode && !children && !rest['aria-label']) {
3232
throw new Error('react-button-a11y: `aria-label` required for accessibility')
@@ -36,6 +36,7 @@ export default class ButtonA11y extends PureComponent {
3636
...rest,
3737
children,
3838
onKeyDown: this.handleKeyDown,
39+
ref: forwardedRef,
3940
role: 'button',
4041
tabIndex: '0'
4142
})
@@ -48,3 +49,9 @@ ButtonA11y.defaultProps = {
4849
onKeyDown: Function.prototype,
4950
strictMode: true
5051
}
52+
53+
const WrappedComponent = forwardRef((props, ref) =>
54+
<ButtonA11y {...props} forwardedRef={ref} />
55+
)
56+
57+
export default WrappedComponent

test/__snapshots__/react-button-a11y.test.js.snap

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`react-button-a11y renders, children 1`] = `
3+
exports[`react-button-a11y component renders, children 1`] = `
44
<div
55
aria-label="Mock Label"
66
className="mock-className"
@@ -13,7 +13,7 @@ exports[`react-button-a11y renders, children 1`] = `
1313
</div>
1414
`;
1515

16-
exports[`react-button-a11y renders, no children 1`] = `
16+
exports[`react-button-a11y component renders, no children 1`] = `
1717
<div
1818
aria-label="Mock Label"
1919
className="mock-className"
@@ -24,7 +24,37 @@ exports[`react-button-a11y renders, no children 1`] = `
2424
/>
2525
`;
2626

27-
exports[`react-button-a11y renders, span element 1`] = `
27+
exports[`react-button-a11y component renders, ref passed in 1`] = `
28+
<ButtonA11y
29+
aria-label="Mock Label"
30+
className="mock-className"
31+
element="div"
32+
forwardedRef={
33+
Object {
34+
"current": <div
35+
aria-label="Mock Label"
36+
class="mock-className"
37+
role="button"
38+
tabindex="0"
39+
/>,
40+
}
41+
}
42+
onClick={[MockFunction]}
43+
onKeyDown={[Function]}
44+
strictMode={true}
45+
>
46+
<div
47+
aria-label="Mock Label"
48+
className="mock-className"
49+
onClick={[MockFunction]}
50+
onKeyDown={[Function]}
51+
role="button"
52+
tabIndex="0"
53+
/>
54+
</ButtonA11y>
55+
`;
56+
57+
exports[`react-button-a11y component renders, span element 1`] = `
2858
<span
2959
aria-label="Mock Label"
3060
className="mock-className"
@@ -35,12 +65,25 @@ exports[`react-button-a11y renders, span element 1`] = `
3565
/>
3666
`;
3767

38-
exports[`react-button-a11y renders, strict mode disabled 1`] = `
39-
<span
68+
exports[`react-button-a11y component renders, strict mode disabled 1`] = `
69+
<div
70+
aria-label="Mock Label"
4071
className="mock-className"
4172
onClick={[MockFunction]}
4273
onKeyDown={[Function]}
4374
role="button"
4475
tabIndex="0"
4576
/>
4677
`;
78+
79+
exports[`react-button-a11y container renders 1`] = `
80+
<ButtonA11y
81+
aria-label="Mock Label"
82+
className="mock-className"
83+
element="div"
84+
forwardedRef={null}
85+
onClick={[MockFunction]}
86+
onKeyDown={[Function]}
87+
strictMode={true}
88+
/>
89+
`;

0 commit comments

Comments
 (0)