Skip to content

Commit 85139cb

Browse files
committed
Change Readme file
1 parent bf255b1 commit 85139cb

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
11
# react-native-nested-listview
2-
Nested Listview for React native
2+
Nested listview component for React native that it allows to create a listview that has N levels of nesting
3+
4+
5+
## Table of contents
6+
7+
1. [Usage](#usage)
8+
1. [Props](#props)
9+
1. [Example](#example)
10+
1. [Roadmap](#roadmap)
11+
12+
13+
## Usage
14+
15+
```
16+
yarn add react-native-nested-listview
17+
```
18+
19+
```javascript
20+
import NestedListview from 'react-native-nested-listview'
21+
22+
const data = [{title: 'Node 1', items: [{title: 'Node 1.1'}, {title: 'Node 1.2'}]}]
23+
24+
<NestedListView
25+
data={data}
26+
getChildrenName={(node) => 'items'}
27+
onNodePressed={(node) => alert('Selected node')}
28+
renderNode={(node, level) => <Text>{node.name}</Text>}
29+
/>
30+
```
31+
32+
## Props
33+
34+
### Required
35+
36+
Prop | Description | Type | Default
37+
------ | ------ | ------ | ------
38+
**`data`** | Array of nested items | Array | **Required**
39+
**`renderNode`** | Takes a node from data and renders it into the NestedlistView. The function receives `{node, level}` (see [Usage](#usage)) and must return a React element. | Function | **Required**
40+
**`getChildrenName`** | Function to determine in a node where are the children, by default NestedListView will try to find them in **items** | Function | **items**
41+
**`onNodePressed`** | Function called when a node is pressed by a user | Function | Not required
42+
43+
## Example
44+
You can find the following example in the [`/example` folder](https://github.com/fjmorant/react-native-nested-listview/tree/master/example).
45+
46+
![react-native-nested-listview](https://imgur.com/OqvopyK.gif)
47+
48+
## Roadmap
49+
50+
- Autoscrolling optionally
51+
- Expand/contract nodes programatically
52+
- Support animations
53+
- Add more examples of usage

example/ExampleApp.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ const colorLevels = {
5656
}
5757

5858
const styles = StyleSheet.create({
59-
container: {flex: 1, backgroundColor: 'rgb(255, 255, 255)', padding: 10},
59+
container: {flex: 1, backgroundColor: 'rgb(255, 255, 255)', padding: 15},
6060
node: {
6161
flex: 1,
6262
padding: 10,
6363
borderWidth: 1,
6464
borderColor: 'rgb(0, 0, 0)',
6565
},
66-
nestedListView: {padding: 10, flex: 1},
6766
})
6867
export default class ExampleApp extends React.Component<Props, State> {
6968
nestedListView: any
@@ -98,11 +97,7 @@ export default class ExampleApp extends React.Component<Props, State> {
9897
data={data}
9998
getChildrenName={this.getChildrenName}
10099
onNodePressed={this.onNodePressed}
101-
ref={ref => {
102-
this.nestedListView = ref
103-
}}
104100
renderNode={this.renderNode}
105-
style={styles.nestedListView}
106101
/>
107102
</View>
108103
)

index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class NestedListView extends React.PureComponent<Props, State> {
1919
id: shortid.generate(),
2020
items: this.props.data
2121
? this.props.data.map((child: Node, index: number) =>
22-
this.generateIds(this.props.data[index])
22+
this.generateIds(this.props.data[index]),
2323
)
2424
: [],
2525
name: 'root',
@@ -35,30 +35,21 @@ export default class NestedListView extends React.PureComponent<Props, State> {
3535
return
3636
}
3737

38-
const childrenName: string = this.props.getChildrenName(node)
38+
const childrenName: string = this.props.getChildrenName(node) || 'items'
39+
const children = node[childrenName]
3940

40-
if (childrenName) {
41-
const children = node[childrenName]
42-
43-
if (children) {
44-
node[childrenName] = children.map((child, index) =>
45-
this.generateIds(children[index])
46-
)
47-
}
41+
if (children) {
42+
node[childrenName] = children.map((child, index) =>
43+
this.generateIds(children[index]),
44+
)
4845
}
4946

5047
node.id = shortid.generate()
5148

5249
return node
5350
}
5451

55-
getChildrenName = (node: Node) => {
56-
if (node.name === 'root') {
57-
return 'items'
58-
}
59-
60-
return this.props.getChildrenName(node)
61-
}
52+
getChildrenName = (node: Node) => this.props.getChildrenName(node)
6253

6354
render = () => {
6455
if (!this.props.getChildrenName) {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"url": "git+https://github.com/fjmorant/react-native-nested-listview.git"
1212
},
1313
"dependencies": {
14-
"shortid": "^2.2.8",
14+
"shortid": "^2.2.8"
15+
},
16+
"peerDependencies": {
1517
"react": "16.0.0-alpha.12",
1618
"react-native": "0.48.3"
1719
},

0 commit comments

Comments
 (0)