Skip to content

Commit 56e373a

Browse files
committed
Prettier
1 parent 17caa86 commit 56e373a

File tree

7 files changed

+829
-173
lines changed

7 files changed

+829
-173
lines changed

example/.eslintcache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"/Users/morant84/Work/react-native-nested-listview/example/__tests__/index.android.js":{"size":284,"mtime":1499290295000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/__tests__/index.android.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/__tests__/index.ios.js":{"size":280,"mtime":1499290295000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/__tests__/index.ios.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/ExampleApp.js":{"size":1936,"mtime":1506268108000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/ExampleApp.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/index.android.js":{"size":1097,"mtime":1499290295000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/index.android.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/index.ios.js":{"size":215,"mtime":1505974689000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/index.ios.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/nestedListView/index.js":{"size":2547,"mtime":1506407640000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/nestedListView/index.js","messages":[],"errorCount":0,"warningCount":0}},"/Users/morant84/Work/react-native-nested-listview/example/nestedListView/NodeView.js":{"size":1428,"mtime":1506407813000,"hashOfConfig":"1y1d1hv","results":{"filePath":"/Users/morant84/Work/react-native-nested-listview/example/nestedListView/NodeView.js","messages":[],"errorCount":0,"warningCount":0}}}

example/.eslintrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"parser": "babel-eslint",
3+
"parserOptions": {
4+
"sourceType": "module"
5+
},
6+
"plugins": ["react", "react-native"],
7+
"ecmaFeatures": {
8+
"jsx": true
9+
}
10+
}

example/ExampleApp.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ const data = [
4141
},
4242
]
4343

44+
const colorLevels = {
45+
0: 'white',
46+
1: 'blue',
47+
2: 'green',
48+
3: 'red',
49+
}
50+
4451
export default class ExampleApp extends React.Component {
4552
nestedListView: any
4653

4754
renderNode = (node: Object, level: string) => {
4855
const paddingLeft = (level + 1) * 30
56+
const backgroundColor = colorLevels[level] || 'white'
57+
4958
return (
50-
<View style={{flex: 1, padding: 10, paddingLeft, borderWidth: 1, borderColor: 'rgb(0, 0, 0)'}}>
59+
<View style={{flex: 1, padding: 10, paddingLeft, borderWidth: 1, borderColor: 'rgb(0, 0, 0)', backgroundColor}}>
5160
<Text>{node.name}</Text>
5261
</View>
5362
)

example/nestedListView/NodeView.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
/* @flow */
22

33
import React from 'react'
4-
import {TouchableOpacity, View, FlatList} from 'react-native'
4+
import {TouchableWithoutFeedback, View, FlatList} from 'react-native'
55

66
export default class NodeView extends React.PureComponent {
7-
87
componentWillMount = () => {
98
let rootChildren = this.props.getChildren(this.props.node)
109

11-
if (rootChildren) {
10+
if (rootChildren) {
1211
rootChildren = rootChildren.map((child, index) => {
1312
return this.props.generateIds(rootChildren[index])
1413
})
15-
}
16-
14+
}
15+
1716
this.setState({data: rootChildren})
1817
}
1918

2019
onNodePressed = (node: any) => {
2120
if (this.state.data) {
22-
const newState = rootChildren = this.state.data.map((child, index) => {
21+
const newState = (rootChildren = this.state.data.map((child, index) => {
2322
return this.props.searchTree(this.state.data[index], node)
24-
})
25-
23+
}))
24+
2625
this.setState({data: newState})
2726
}
28-
27+
2928
this.props.onNodePressed(node)
3029
}
3130

3231
render() {
33-
const {getChildren, node, nodeStyle, onLayout, onNodePressed, renderNode, renderChildrenNode} = this.props
32+
const {
33+
getChildren,
34+
node,
35+
nodeStyle,
36+
onLayout,
37+
onNodePressed,
38+
renderNode,
39+
renderChildrenNode,
40+
} = this.props
3441
const children = getChildren(node)
35-
42+
3643
return (
3744
<View onLayout={onLayout}>
38-
<TouchableOpacity onPress={() => this.onNodePressed(node)}>
45+
<TouchableWithoutFeedback onPress={() => this.onNodePressed(node)}>
3946
{renderNode()}
40-
</TouchableOpacity>
41-
{node.opened && this.state.data
42-
? <FlatList
43-
data={this.state.data}
44-
renderItem={({item}) => renderChildrenNode(item)}
45-
keyExtractor={(item) => item.id}/> : null}
47+
</TouchableWithoutFeedback>
48+
{node.opened && this.state.data ? (
49+
<FlatList
50+
data={this.state.data}
51+
renderItem={({item}) => renderChildrenNode(item)}
52+
keyExtractor={item => item.id}
53+
/>
54+
) : null}
4655
</View>
4756
)
4857
}

example/nestedListView/index.js

Lines changed: 29 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,117 +5,46 @@ import {ScrollView, FlatList} from 'react-native'
55
import NodeView from './NodeView'
66
import shortid from 'shortid'
77

8-
export default class NestedListView extends React.Component {
8+
export default class NestedListView extends React.PureComponent {
99
props: {
1010
data: any,
1111
renderNode: Function,
1212
onNodePressed: Function,
1313
getChildrenName: Function,
14-
autoScrollToNodeId: ?string,
1514
}
1615

17-
scrollview: any
18-
1916
state = {
2017
data: {},
2118
}
2219

2320
componentWillMount = () => {
24-
let rootChildren = this.props.data
25-
26-
27-
if (rootChildren) {
28-
rootChildren = rootChildren.map((child, index) => {
29-
return this.generateIds(rootChildren[index])
21+
const rootChildren = this.props.data
22+
if (rootChildren) {
23+
this.setState({
24+
data: rootChildren.map((child, index) =>
25+
this.generateIds(rootChildren[index])
26+
),
3027
})
31-
}
32-
33-
console.log('rootChildren: ', rootChildren)
34-
35-
this.setState({data: rootChildren})
36-
}
37-
38-
contractAllNodesInTree = () => {
39-
this.setState({data: this.compactNodeInTree(this.state.data)})
40-
}
41-
42-
expandNode = (node: any) => {
43-
this.setState({data: this.expandNodeInTree(this.state.data, node)})
44-
}
45-
46-
compactNodeInTree = (element: any) => {
47-
const childrenName = this.props.getChildrenName(element)
48-
49-
if (childrenName && element.opened) {
50-
const children = element[childrenName]
51-
52-
if (children) {
53-
for (i = 0; i < children.length; i++) {
54-
element[childrenName][i] = this.compactNodeInTree(children[i])
55-
}
56-
}
57-
58-
element.opened = false
59-
60-
return element
6128
}
62-
63-
element.opened = false
64-
65-
return element
66-
}
67-
68-
expandNodeInTree = (element: any, otherElement: any) => {
69-
const childrenName = this.props.getChildrenName(element)
70-
71-
if (otherElement && element.id == otherElement.id) {
72-
element.opened = true
73-
74-
return element
75-
} else if (childrenName) {
76-
const children = element[childrenName]
77-
78-
if (children) {
79-
var anyChildrenHasOpened = false
80-
for (i = 0; i < children.length; i++) {
81-
element[childrenName][i] = this.expandNodeInTree(
82-
children[i],
83-
otherElement,
84-
)
85-
86-
if (element[childrenName][i].opened) {
87-
anyChildrenHasOpened = true
88-
}
89-
}
90-
91-
if (anyChildrenHasOpened) {
92-
element.opened = true
93-
}
94-
}
95-
96-
return element
97-
}
98-
99-
return element
10029
}
10130

10231
searchTree = (element: any, otherElement: any) => {
10332
if (element.id === otherElement.id) {
10433
element.opened = !element.opened
10534

10635
return element
107-
}
36+
}
10837

10938
const childrenName = this.props.getChildrenName(element)
110-
39+
11140
if (childrenName) {
11241
const children = element[childrenName]
11342

114-
if (children) {
115-
for (i = 0; i < children.length; i++) {
116-
element[childrenName][i] = this.searchTree(children[i], otherElement)
117-
}
118-
}
43+
if (children) {
44+
element[childrenName] = children.map((child, index) =>
45+
this.searchTree(children[index], otherElement)
46+
)
47+
}
11948

12049
return element
12150
}
@@ -133,11 +62,11 @@ export default class NestedListView extends React.Component {
13362
if (childrenName) {
13463
const children = element[childrenName]
13564

136-
if (children) {
137-
for (i = 0; i < children.length; i++) {
138-
element[childrenName][i] = this.generateIds(children[i])
139-
}
140-
}
65+
if (children) {
66+
element[childrenName] = children.map((child, index) =>
67+
this.generateIds(children[index])
68+
)
69+
}
14170
}
14271

14372
element.id = shortid.generate()
@@ -146,11 +75,11 @@ export default class NestedListView extends React.Component {
14675
}
14776

14877
onNodePressed = (node: any) => {
149-
const newState = rootChildren = this.state.data.map((child, index) => {
150-
return this.searchTree(this.state.data[index], node)
151-
})
78+
const rootChildren = this.state.data.map((child, index) =>
79+
this.searchTree(this.state.data[index], node)
80+
)
15281

153-
this.setState({data: newState})
82+
this.setState({data: rootChildren})
15483
this.props.onNodePressed(node)
15584
}
15685

@@ -163,7 +92,8 @@ export default class NestedListView extends React.Component {
16392
searchTree={this.searchTree}
16493
generateIds={this.generateIds}
16594
onNodePressed={() => this.onNodePressed(item)}
166-
renderChildrenNode={(childrenNode: Object) => this.onCreateChildren(childrenNode, level + 1)}
95+
renderChildrenNode={(childrenNode: Object) =>
96+
this.onCreateChildren(childrenNode, level + 1)}
16797
renderNode={() => this.props.renderNode(item, level)}
16898
/>
16999
)
@@ -172,10 +102,11 @@ export default class NestedListView extends React.Component {
172102
render = () => {
173103
return (
174104
<FlatList
175-
data={this.state.data}
176-
style={this.props.style}
105+
data={this.state.data}
106+
style={this.props.style}
177107
renderItem={({item}) => this.onCreateChildren(item, 0)}
178-
keyExtractor={(item) => item.id}/>
108+
keyExtractor={item => item.id}
109+
/>
179110
)
180111
}
181112
}

example/package.json

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
{
2-
"name": "example",
3-
"version": "0.0.1",
4-
"private": true,
5-
"scripts": {
6-
"start": "node node_modules/react-native/local-cli/cli.js start",
7-
"test": "jest"
8-
},
9-
"dependencies": {
10-
"react": "16.0.0-alpha.12",
11-
"react-native": "0.48.3",
12-
"react-native-nested-listview": "file:.."
13-
},
14-
"devDependencies": {
15-
"babel-jest": "20.0.3",
16-
"babel-preset-react-native": "2.1.0",
17-
"jest": "20.0.4",
18-
"react-test-renderer": "16.0.0-alpha.12"
19-
},
20-
"jest": {
21-
"preset": "react-native"
22-
}
2+
"name": "example",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"start": "node node_modules/react-native/local-cli/cli.js start",
7+
"test": "jest",
8+
"lint": "eslint --cache ."
9+
},
10+
"dependencies": {
11+
"react": "16.0.0-alpha.12",
12+
"react-native": "0.48.3"
13+
},
14+
"devDependencies": {
15+
"babel-eslint": "7",
16+
"babel-jest": "20.0.3",
17+
"babel-preset-react-native": "2.1.0",
18+
"eslint": "3.x",
19+
"eslint-plugin-react": "^7.4.0",
20+
"eslint-plugin-react-native": "^3.1.0",
21+
"jest": "20.0.4",
22+
"prettier": "^1.7.0",
23+
"prettier-eslint": "^8.1.1",
24+
"react-test-renderer": "16.0.0-alpha.12"
25+
},
26+
"jest": {
27+
"preset": "react-native"
28+
}
2329
}

0 commit comments

Comments
 (0)