Skip to content

Commit c0a760e

Browse files
committed
Implement
1 parent 2844d00 commit c0a760e

File tree

7 files changed

+135
-2
lines changed

7 files changed

+135
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
__tests__/**/*.d.ts
40+
__tests__/**/*.js

__tests__/test.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from "react"
2+
import * as ReactDOM from "react-dom"
3+
import FindElement from ".."
4+
5+
class Component extends FindElement<{}, {}> {
6+
}
7+
8+
test("FindElement finds child DOM element", () => {
9+
const root = document.createElement("div")
10+
const view = <Component>
11+
<div id="elem" />
12+
</Component>
13+
const component = ReactDOM.render(view, root) as Component
14+
expect(component.element.id).toBe("elem")
15+
})

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference types="react" />
2+
import * as React from "react";
3+
export default class FindElement<TProps, TState> extends React.Component<TProps, TState> {
4+
element: Element | undefined;
5+
mapElement(elem: React.ReactChild): React.ReactChild;
6+
render(): React.ReactElement<any>;
7+
}

index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || function (d, b) {
3+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4+
function __() { this.constructor = d; }
5+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6+
};
7+
var React = require("react");
8+
var FindElement = (function (_super) {
9+
__extends(FindElement, _super);
10+
function FindElement() {
11+
_super.apply(this, arguments);
12+
}
13+
FindElement.prototype.mapElement = function (elem) {
14+
var _this = this;
15+
if (typeof elem == "object") {
16+
if (typeof elem.type == "string") {
17+
var origRef_1 = elem.ref;
18+
return React.cloneElement(elem, {
19+
ref: function (elem) {
20+
if (origRef_1) {
21+
origRef_1(elem);
22+
}
23+
_this.element = elem;
24+
}
25+
});
26+
}
27+
else {
28+
return React.cloneElement(elem, {
29+
children: React.Children.map(elem.props.children, this.mapElement.bind(this))
30+
});
31+
}
32+
}
33+
return elem;
34+
};
35+
FindElement.prototype.render = function () {
36+
var child = this.mapElement(React.Children.only(this.props.children));
37+
if (typeof child == "object") {
38+
return child;
39+
}
40+
else {
41+
throw new Error("Child must not be string nor number");
42+
}
43+
};
44+
return FindElement;
45+
}(React.Component));
46+
Object.defineProperty(exports, "__esModule", { value: true });
47+
exports.default = FindElement;

index.tsx

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as React from "react"
2+
3+
export default
4+
class FindElement<TProps, TState> extends React.Component<TProps, TState> {
5+
element: Element|undefined
6+
7+
mapElement(elem: React.ReactChild): React.ReactChild {
8+
if (typeof elem == "object") {
9+
if (typeof elem.type == "string") {
10+
const origRef = (elem as any).ref
11+
return React.cloneElement(elem, {
12+
ref: (elem: Element) => {
13+
if (origRef) {
14+
origRef(elem)
15+
}
16+
this.element = elem
17+
}
18+
})
19+
} else {
20+
return React.cloneElement(elem, {
21+
children: React.Children.map(elem.props.children, this.mapElement.bind(this))
22+
})
23+
}
24+
}
25+
return elem
26+
}
27+
28+
render() {
29+
const child = this.mapElement(React.Children.only(this.props.children))
30+
if (typeof child == "object") {
31+
return child
32+
} else {
33+
throw new Error("Child must not be string nor number")
34+
}
35+
}
36+
}

package.json

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "Base class for React components that finds and taps descendant DOM element",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"build": "tsc",
8+
"pretest": "npm run build",
9+
"test": "jest"
810
},
911
"repository": {
1012
"type": "git",
@@ -15,5 +17,19 @@
1517
"bugs": {
1618
"url": "https://github.com/sketchglass/react-find-element/issues"
1719
},
18-
"homepage": "https://github.com/sketchglass/react-find-element#readme"
20+
"homepage": "https://github.com/sketchglass/react-find-element#readme",
21+
"devDependencies": {
22+
"@types/jest": "^16.0.0",
23+
"@types/react": "^0.14.50",
24+
"@types/react-dom": "^0.14.19",
25+
"jest": "^17.0.3",
26+
"jest-cli": "^17.0.3",
27+
"react": "^15.4.1",
28+
"react-dom": "^15.4.1",
29+
"typescript": "^2.0.10"
30+
},
31+
"jest": {
32+
"verbose": true,
33+
"watchman": false
34+
}
1935
}

tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"noImplicitAny": true,
6+
"declaration": true,
7+
"jsx": "react"
8+
}
9+
}

0 commit comments

Comments
 (0)