Skip to content

Commit 06df55b

Browse files
committed
Convert RelExprTree to functional component
1 parent d7a387d commit 06df55b

1 file changed

Lines changed: 41 additions & 43 deletions

File tree

src/RelExprTree.js

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import React, {Component} from 'react';
2+
import React from 'react';
33
import TreeMenu, {ItemComponent} from 'react-simple-tree-menu';
44
import {v4 as uuidv4} from 'uuid';
55
import {
@@ -17,7 +17,7 @@ import {changeExpr} from './modules/data';
1717
import '../node_modules/react-simple-tree-menu/dist/main.css';
1818
import './RelExprTree.css';
1919

20-
import type {Node} from 'react';
20+
import type {Node, StatelessFunctionalComponent} from 'react';
2121

2222
type Props = {
2323
changeExpr: typeof changeExpr,
@@ -26,18 +26,18 @@ type Props = {
2626
};
2727

2828
/** A graphical representation of a relational algebra expression */
29-
class RelExprTree extends Component<Props> {
29+
const RelExprTree: StatelessFunctionalComponent<Props> = (props) => {
3030
/**
3131
* @param expr - a relational algebra expression object to render
3232
* @param keys - an array where all created paths should be saved
3333
* @param path - the path to the current node
3434
* @return a tree structure representing the exppression
3535
*/
36-
buildTree(
36+
const buildTree = (
3737
expr: {[string]: any},
3838
keys: Array<string>,
3939
path: Array<string> = []
40-
): {} {
40+
): {} => {
4141
// Don't try to render empty expressions
4242
if (!expr || Object.keys(expr).length === 0) {
4343
return {};
@@ -56,7 +56,7 @@ class RelExprTree extends Component<Props> {
5656
return {
5757
key: key,
5858
expr: expr,
59-
nodes: [this.buildTree(expr[type].children[0], keys, newPath)],
59+
nodes: [buildTree(expr[type].children[0], keys, newPath)],
6060
};
6161
case 'relation':
6262
return {
@@ -71,16 +71,16 @@ class RelExprTree extends Component<Props> {
7171
key: key,
7272
expr: expr,
7373
nodes: [
74-
this.buildTree(expr[type].left, keys, newPath),
75-
this.buildTree(expr[type].right, keys, newPath),
74+
buildTree(expr[type].left, keys, newPath),
75+
buildTree(expr[type].right, keys, newPath),
7676
],
7777
};
7878
default:
7979
throw new Error('Invalid expression ' + JSON.stringify(expr) + '.');
8080
}
81-
}
81+
};
8282

83-
getLabel(expr: {[string]: any}): Node {
83+
const getLabel = (expr: {[string]: any}): Node => {
8484
if (!expr || Object.keys(expr).length === 0) {
8585
return '';
8686
}
@@ -111,40 +111,38 @@ class RelExprTree extends Component<Props> {
111111
default:
112112
throw new Error('Invalid expression ' + JSON.stringify(expr) + '.');
113113
}
114-
}
114+
};
115115

116-
render(): Node {
117-
const keys = [];
118-
const data = [this.buildTree(this.props.expr, keys)];
119-
return (
120-
<div className="RelExprTree">
121-
<TreeMenu
122-
data={data}
123-
initialOpenNodes={keys}
124-
hasSearch={false}
125-
disableKeyboard
126-
onClickItem={(props) => {
127-
this.props.changeExpr(props.expr, null);
116+
const keys = [];
117+
const data = [buildTree(props.expr, keys)];
118+
return (
119+
<div className="RelExprTree">
120+
<TreeMenu
121+
data={data}
122+
initialOpenNodes={keys}
123+
hasSearch={false}
124+
disableKeyboard
125+
onClickItem={(clickProps) => {
126+
props.changeExpr(clickProps.expr, null);
128127

129-
this.props.ReactGA.event({
130-
category: 'User Selecting Relational Algebra Tree',
131-
action: Object.keys(props.expr)[0],
132-
});
133-
}}
134-
>
135-
{({search, items}) => (
136-
<div>
137-
{items.map(({key, ...props}) => {
138-
const newProps = {label: this.getLabel(props.expr)};
139-
Object.assign(props, newProps);
140-
return <ItemComponent key={key} {...props} />;
141-
})}
142-
</div>
143-
)}
144-
</TreeMenu>
145-
</div>
146-
);
147-
}
148-
}
128+
props.ReactGA.event({
129+
category: 'User Selecting Relational Algebra Tree',
130+
action: Object.keys(clickProps.expr)[0],
131+
});
132+
}}
133+
>
134+
{({search, items}) => (
135+
<div>
136+
{items.map(({key, ...props}) => {
137+
const newProps = {label: getLabel(props.expr)};
138+
Object.assign(props, newProps);
139+
return <ItemComponent key={key} {...props} />;
140+
})}
141+
</div>
142+
)}
143+
</TreeMenu>
144+
</div>
145+
);
146+
};
149147

150148
export default RelExprTree;

0 commit comments

Comments
 (0)