diff --git a/.DS_Store b/.DS_Store
index 9f7e6dc..16fd36d 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/RCTTableView/JSONDataSource.m b/RCTTableView/JSONDataSource.m
index 43fe315..10d1dc8 100644
--- a/RCTTableView/JSONDataSource.m
+++ b/RCTTableView/JSONDataSource.m
@@ -17,14 +17,16 @@ -(id)initWithFilename:(NSString *)filename filter:(NSString *)filter args:(NSArr
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
NSArray *json = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
- options:kNilOptions
+ options:NSJSONReadingMutableContainers
error:&error];
NSAssert(error==nil, @"JSON Error %@", [error description]);
NSAssert([json isKindOfClass:[NSArray class]], @"JSON should be NSArray type");
if (filter){
- json = [json filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
+ for (NSMutableDictionary *sections in json){
+ sections[@"items"] = [sections[@"items"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
+ }
}
_sections = json;
diff --git a/RCTTableView/RCTTableView.h b/RCTTableView/RCTTableView.h
index 3c278d8..7440da6 100644
--- a/RCTTableView/RCTTableView.h
+++ b/RCTTableView/RCTTableView.h
@@ -24,6 +24,7 @@
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
@property (nonatomic, copy) NSMutableArray *sections;
+@property (nonatomic, copy) NSArray *additionalItems;
@property (nonatomic, strong) NSString *json;
@property (nonatomic, strong) NSString *filter;
@property (nonatomic, strong) NSArray *filterArgs;
diff --git a/RCTTableView/RCTTableView.m b/RCTTableView/RCTTableView.m
index 2496c3e..2df025d 100644
--- a/RCTTableView/RCTTableView.m
+++ b/RCTTableView/RCTTableView.m
@@ -83,8 +83,14 @@ - (void)setSections:(NSArray *)sections
_sections = [NSMutableArray arrayWithCapacity:[sections count]];
for (NSDictionary *section in sections){
NSMutableDictionary *sectionData = [NSMutableDictionary dictionaryWithDictionary:section];
- NSMutableArray *items = [NSMutableArray arrayWithCapacity:[sectionData[@"items"] count]];
- for (NSDictionary *item in sectionData[@"items"]){
+ NSMutableArray *allItems = [NSMutableArray array];
+ if (self.additionalItems){
+ [allItems addObjectsFromArray:self.additionalItems];
+ }
+ [allItems addObjectsFromArray:sectionData[@"items"]];
+
+ NSMutableArray *items = [NSMutableArray arrayWithCapacity:[allItems count]];
+ for (NSDictionary *item in allItems){
NSMutableDictionary *itemData = [NSMutableDictionary dictionaryWithDictionary:item];
if (self.selectedValue && [self.selectedValue isEqualToString:item[@"value"]]){
_selectedSection = [_sections count];
diff --git a/RCTTableView/RCTTableViewManager.m b/RCTTableView/RCTTableViewManager.m
index a831052..2979137 100644
--- a/RCTTableView/RCTTableViewManager.m
+++ b/RCTTableView/RCTTableViewManager.m
@@ -24,6 +24,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(filter, NSString)
RCT_EXPORT_VIEW_PROPERTY(selectedValue, NSString)
RCT_EXPORT_VIEW_PROPERTY(filterArgs, NSArray)
+RCT_EXPORT_VIEW_PROPERTY(additionalItems, NSArray)
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(selectedSection, NSInteger)
RCT_CUSTOM_VIEW_PROPERTY(tableViewStyle, UITableViewStyle, RCTTableView) {
diff --git a/README.md b/README.md
index 34e6fbb..86a9c77 100755
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ Native iOS UITableView for React Native with JSON support.
- Automatic scroll to initial selected value during component initialization
- Automatic item selection with "checkmark" with old item de-selection (optionally), see demo, useful to select country/state/etc.
- Native JSON support for datasource. If you need to display large dataset, generated Javascript will became very large and impact js loading time. To solve this problem the component could read JSON directly from app bundle without JS!
+- Filter JSON datasources using NSPredicate syntax. For example you could select states for given country only (check demo)
## Supports UITableView styles
- UITableViewStylePlain (TableView.Consts.Style.Plain)
@@ -98,6 +99,21 @@ AppRegistry.registerComponent('TableViewExample', () => TableViewExample);
}
```
+### Example 3 (JSON filter and optional items at the beginning)
+```
+ // list spanish provinces and add 'All states' item at the beginning
+ render(){
+ var country = "ES";
+ return (
+ console.log(event.nativeEvent)}>
+ - All states
+
+ );
+ }
+```
+
## Getting started
1. `npm install react-native-tableview --save`
2. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]`
diff --git a/examples/.DS_Store b/examples/.DS_Store
index c9c2e11..8721fb1 100644
Binary files a/examples/.DS_Store and b/examples/.DS_Store differ
diff --git a/examples/TableViewDemo/.idea/workspace.xml b/examples/TableViewDemo/.idea/workspace.xml
index facae95..af2ec37 100644
--- a/examples/TableViewDemo/.idea/workspace.xml
+++ b/examples/TableViewDemo/.idea/workspace.xml
@@ -25,7 +25,7 @@
-
+
@@ -35,17 +35,7 @@
-
-
-
-
-
-
-
-
-
-
-
+
@@ -64,8 +54,8 @@
-
-
+
+
@@ -96,7 +86,10 @@
-
+
+
+
+
@@ -105,7 +98,7 @@
-
+
@@ -117,8 +110,8 @@
-
-
+
+
@@ -135,9 +128,10 @@
+
+
-
@@ -150,10 +144,10 @@
-
+
-
-
+
+
@@ -323,18 +317,18 @@
-
+
-
+
-
+
-
+
-
+
@@ -421,7 +415,10 @@
-
+
+
+
+
@@ -622,11 +619,14 @@
-
+
+
+
+
-
+
@@ -634,34 +634,34 @@
-
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
@@ -669,7 +669,7 @@
-
+
@@ -679,8 +679,8 @@
-
-
+
+
diff --git a/examples/TableViewDemo/TableViewDemo.xcodeproj/project.pbxproj b/examples/TableViewDemo/TableViewDemo.xcodeproj/project.pbxproj
index 85da33c..60542a2 100755
--- a/examples/TableViewDemo/TableViewDemo.xcodeproj/project.pbxproj
+++ b/examples/TableViewDemo/TableViewDemo.xcodeproj/project.pbxproj
@@ -24,6 +24,7 @@
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
87D0FD621B83239D00B0DA60 /* libRCTTableView.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 87D0FD5E1B8322FA00B0DA60 /* libRCTTableView.a */; };
87D0FDC41B836F5500B0DA60 /* countries.json in Resources */ = {isa = PBXBuildFile; fileRef = 87D0FDC31B836F5500B0DA60 /* countries.json */; };
+ 87D0FDC61B8471D200B0DA60 /* states.json in Resources */ = {isa = PBXBuildFile; fileRef = 87D0FDC51B8471D200B0DA60 /* states.json */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -127,6 +128,7 @@
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = ""; };
87D0FD591B8322FA00B0DA60 /* RCTTableView.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTTableView.xcodeproj; path = "node_modules/react-native-tableview/RCTTableView.xcodeproj"; sourceTree = ""; };
87D0FDC31B836F5500B0DA60 /* countries.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = countries.json; sourceTree = ""; };
+ 87D0FDC51B8471D200B0DA60 /* states.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = states.json; sourceTree = ""; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -210,6 +212,7 @@
13B07FAE1A68108700A75B9A /* TableViewDemo */ = {
isa = PBXGroup;
children = (
+ 87D0FDC51B8471D200B0DA60 /* states.json */,
87D0FDC31B836F5500B0DA60 /* countries.json */,
008F07F21AC5B25A0029DE68 /* main.jsbundle */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
@@ -469,6 +472,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 87D0FDC61B8471D200B0DA60 /* states.json in Resources */,
008F07F31AC5B25A0029DE68 /* main.jsbundle in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
87D0FDC41B836F5500B0DA60 /* countries.json in Resources */,
diff --git a/examples/TableViewDemo/index.ios.js b/examples/TableViewDemo/index.ios.js
index d9fb1ff..7853643 100755
--- a/examples/TableViewDemo/index.ios.js
+++ b/examples/TableViewDemo/index.ios.js
@@ -7,14 +7,28 @@ var Section = TableView.Section;
var Item = TableView.Item;
class TableViewExample extends React.Component {
+
+ // list spanish provinces and add 'All states' item at the beginning
render(){
+ var country = "ES";
+ return (
+ console.log(event.nativeEvent)}>
+ - All states
+
+ );
+ }
+
+ // list all countries, select Spain
+ render2(){
return (
console.log(event.nativeEvent)}/>
);
}
- render2(){
+ render3(){
return (
B){
@@ -11,5 +11,10 @@ obj[0].items.sort(function(a,b){
}else{
return 0;
}});
-
-fs.writeFileSync('countries.json',JSON.stringify(obj))
+obj.forEach(function(el){
+ if (el.name) {
+ items.push({label: el.name, value: el.ident, country: el.country});
+ }
+});
+console.log(items);
+fs.writeFileSync('states.json',JSON.stringify([{items:items}]));
diff --git a/index.js b/index.js
index 1402bc3..05cbf10 100644
--- a/index.js
+++ b/index.js
@@ -4,6 +4,13 @@ var {NativeMethodsMixin, ReactNativeViewAttributes, NativeModules, StyleSheet, V
var RCTTableViewConsts = NativeModules.UIManager.RCTTableView.Constants;
var TABLEVIEW = 'tableview';
+
+function clone(map){
+ var el = {};
+ for (var i in map)
+ el[i] = map[i];
+ return el;
+}
var TableView = React.createClass({
mixins: [NativeMethodsMixin],
@@ -25,23 +32,39 @@ var TableView = React.createClass({
_stateFromProps: function(props) {
var selectedIndex = -1;
var sections = [];
+ var additionalItems = [];
var selectedSection = props.selectedSection || 0;
// iterate over sections
React.Children.forEach(props.children, function (section, index) {
var items=[];
- React.Children.forEach(section.props.children, function(child, itemIndex){
- if (child.props.selected || props.selectedValue==child.props.value) {
- console.log("SELECT "+itemIndex+" "+index);
- selectedIndex = itemIndex;
- selectedSection = index;
+ if (section.type==TableView.Section) {
+ React.Children.forEach(section.props.children, function (child, itemIndex) {
+ if (child.props.selected || props.selectedValue == child.props.value) {
+ selectedIndex = itemIndex;
+ selectedSection = index;
+ }
+ var el = clone(child.props);
+
+ if (section.props.arrow){
+ el.arrow = section.props.arrow;
+ }
+ if (!el.label){
+ el.label = el.children;
+ }
+ items.push(el);
+ });
+ sections.push({label:section.props.label, items: items});
+ }
+ if (section.type==TableView.Item){
+ var el = clone(section.props);
+ if (!el.label){
+ el.label = el.children;
}
- items.push({value: child.props.value, label: child.props.children, detail:child.props.detail,
- selected: child.props.selected, arrow:section.props.arrow | child.props.arrow});
- });
- sections.push({label:section.props.label, items: items});
+ additionalItems.push(el);
+ }
});
- return {selectedIndex, selectedSection, sections};
+ return {selectedIndex, selectedSection, sections, additionalItems};
},
render: function() {
@@ -52,6 +75,7 @@ var TableView = React.createClass({
sections={this.state.sections}
selectedIndex={this.state.selectedIndex}
selectedSection={this.state.selectedSection}
+ additionalItems={this.state.additionalItems}
onPress={this._onChange}
tableViewStyle={TableView.Consts.Style.Plain}
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
@@ -61,7 +85,6 @@ var TableView = React.createClass({
},
_onChange: function(event) {
- console.log("onPress!");
if (this.props.onPress) {
this.props.onPress(event);
}
diff --git a/package.json b/package.json
index 72b42db..ff5c598 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "react-native-tableview",
- "version": "1.0.1",
+ "version": "1.0.2",
"description": "Native iOS TableView wrapper for React Native",
"main": "index.js",
"scripts": {