-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
88 lines (77 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React from 'react';
class StackItem {
constructor(component, props) {
this.component = component;
this.props = props;
}
}
let stack = new Array();
let component;
function goTo(comp, props = {}) {
stack.push(new StackItem(comp, props));
component && component.forceUpdate();
}
function goBack() {
if (stack.length > 0) {
stack.pop();
}
component && component.forceUpdate();
}
export class Link extends React.PureComponent {
constructor(props) {
super(props);
}
onClick = evt => {
evt.preventDefault();
if (this.props.component) {
goTo(this.props.component, this.props.componentProps || {});
} else if (this.props.href) {
open(this.props.href);
}
if (typeof this.props.onClick === 'function') {
this.props.onClick(evt);
}
};
render() {
const dataProps = {};
Object.keys(this.props).forEach(key => {
if (key.match('data')) {
dataProps[key] = this.props[key];
}
});
return (
<a
href={this.props.href ? this.props.href : ''}
className={this.props.className}
id={this.props.id}
{...dataProps}
onClick={this.onClick}
>
{this.props.children}
</a>
);
}
}
Link.defaultProps = {
className: '',
id: '',
};
export default class Router extends React.Component {
constructor() {
super();
this.componentDidMount = this._componentDidMount.bind(this);
}
_componentDidMount() {
component = this;
}
render() {
const { component: Component = ({ children }) => children, props } =
stack[stack.length - 1] || {};
return (
<Component {...props}>
{stack.length == 0 && this.props.children}
</Component>
);
}
}
export { goTo, goBack };