Skip to content

Commit 38d4ff0

Browse files
author
Pavlo Aksonov
committed
simplify onPress event parameter (remove newValue, oldValue)
1 parent e61701a commit 38d4ff0

File tree

7 files changed

+299
-53
lines changed

7 files changed

+299
-53
lines changed

RCTTableView/RCTTableView.m

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ - (void)createTableView {
8181
- (void)setSections:(NSArray *)sections
8282
{
8383
_sections = [NSMutableArray arrayWithCapacity:[sections count]];
84+
BOOL found = NO;
8485
for (NSDictionary *section in sections){
8586
NSMutableDictionary *sectionData = [NSMutableDictionary dictionaryWithDictionary:section];
8687
NSMutableArray *allItems = [NSMutableArray array];
@@ -96,12 +97,18 @@ - (void)setSections:(NSArray *)sections
9697
_selectedSection = [_sections count];
9798
_selectedIndex = [items count];
9899
itemData[@"selected"] = @YES;
100+
found = YES;
99101
}
100102
[items addObject:itemData];
101103
}
102104
sectionData[@"items"] = items;
103105
[_sections addObject:sectionData];
104-
106+
}
107+
// check first element if no match
108+
if (!found && self.selectedValue && [_sections count] && [_sections[0][@"items"] count]){
109+
_selectedSection = 0;
110+
_selectedIndex = 0;
111+
_sections[0][@"items"][0][@"selected"] = @YES;
105112
}
106113
}
107114

@@ -143,16 +150,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
143150
[tableView deselectRowAtIndexPath:indexPath animated:NO];
144151
NSMutableDictionary *oldValue = self.selectedIndex>=0 ?[self dataForRow:self.selectedIndex section:self.selectedSection] : [NSMutableDictionary dictionaryWithDictionary:@{}];
145152
NSMutableDictionary *newValue = [self dataForRow:indexPath.item section:indexPath.section];
146-
NSDictionary *event = @{
147-
@"target": self.reactTag,
148-
@"oldValue": oldValue,
149-
@"newValue": newValue,
150-
@"indexPath": @{
151-
@"section": @([indexPath section]),
152-
@"row": @([indexPath row])
153-
}
154-
};
155-
[_eventDispatcher sendInputEventWithName:@"topTap" body:event];
153+
newValue[@"target"] = self.reactTag;
154+
newValue[@"selectedIndex"] = [NSNumber numberWithInteger:indexPath.item];
155+
newValue[@"selectedSection"] = [NSNumber numberWithInteger:indexPath.section];
156+
157+
158+
[_eventDispatcher sendInputEventWithName:@"topTap" body:newValue];
156159
if (oldValue[@"selected"]){
157160
[oldValue removeObjectForKey:@"selected"];
158161
[newValue setObject:@1 forKey:@"selected"];

examples/TableViewDemo/.idea/workspace.xml

Lines changed: 179 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/TableViewDemo/cities.json

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

examples/TableViewDemo/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var fs = require('fs');
2-
var obj = JSON.parse(fs.readFileSync('states.json', 'utf8'));
2+
var obj = JSON.parse(fs.readFileSync('cities.json', 'utf8'));
33
var items=[];
44
obj.sort(function(a,b){
55
var A=a.name;

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,18 @@ var TableView = React.createClass({
7676
selectedIndex={this.state.selectedIndex}
7777
selectedSection={this.state.selectedSection}
7878
additionalItems={this.state.additionalItems}
79-
onPress={this._onChange}
8079
tableViewStyle={TableView.Consts.Style.Plain}
8180
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
8281
{...this.props}
82+
onPress={this._onChange}
8383
/>
8484
);
8585
},
8686

8787
_onChange: function(event) {
88+
console.log("_ONCHANGE:")
8889
if (this.props.onPress) {
89-
this.props.onPress(event);
90+
this.props.onPress(event.nativeEvent);
9091
}
9192
//if (this.props.onValueChange) {
9293
// this.props.onValueChange(event.nativeEvent.newValue);

npm-debug.log

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ '/Users/aksonov/.nvm/versions/io.js/v2.5.0/bin/iojs',
3+
1 verbose cli '/Users/aksonov/.nvm/versions/io.js/v2.5.0/bin/npm',
4+
1 verbose cli 'publish',
5+
1 verbose cli './' ]
6+
2 info using npm@2.13.2
7+
3 info using node@v2.5.0
8+
4 verbose publish [ './' ]
9+
5 silly cache add args [ './', null ]
10+
6 verbose cache add spec ./
11+
7 silly cache add parsed spec { raw: './',
12+
7 silly cache add scope: null,
13+
7 silly cache add name: null,
14+
7 silly cache add rawSpec: './',
15+
7 silly cache add spec: '/Users/aksonov/Documents/react-native-tableview',
16+
7 silly cache add type: 'directory' }
17+
8 verbose addLocalDirectory /Users/aksonov/.npm/react-native-tableview/1.0.2/package.tgz not in flight; packing
18+
9 verbose tar pack [ '/Users/aksonov/.npm/react-native-tableview/1.0.2/package.tgz',
19+
9 verbose tar pack '/Users/aksonov/Documents/react-native-tableview' ]
20+
10 verbose tarball /Users/aksonov/.npm/react-native-tableview/1.0.2/package.tgz
21+
11 verbose folder /Users/aksonov/Documents/react-native-tableview
22+
12 info prepublish react-native-tableview@1.0.2
23+
13 verbose addLocalTarball adding from inside cache /Users/aksonov/.npm/react-native-tableview/1.0.2/package.tgz
24+
14 silly cache afterAdd react-native-tableview@1.0.2
25+
15 verbose afterAdd /Users/aksonov/.npm/react-native-tableview/1.0.2/package/package.json not in flight; writing
26+
16 verbose afterAdd /Users/aksonov/.npm/react-native-tableview/1.0.2/package/package.json written
27+
17 silly publish { name: 'react-native-tableview',
28+
17 silly publish version: '1.0.2',
29+
17 silly publish description: 'Native iOS TableView wrapper for React Native',
30+
17 silly publish main: 'index.js',
31+
17 silly publish scripts: { test: 'echo "Error: no test specified" && exit 1' },
32+
17 silly publish repository:
33+
17 silly publish { type: 'git',
34+
17 silly publish url: 'git+https://github.com/aksonov/react-native-tableview.git' },
35+
17 silly publish keywords: [ 'react-native', 'UITableView', 'iOS' ],
36+
17 silly publish author:
37+
17 silly publish { name: 'Pavlo Aksonov',
38+
17 silly publish email: 'akso.nov@gmail.com',
39+
17 silly publish url: 'http://aksonov.com' },
40+
17 silly publish license: 'ISC',
41+
17 silly publish bugs: { url: 'https://github.com/aksonov/react-native-tableview/issues' },
42+
17 silly publish homepage: 'https://github.com/aksonov/react-native-tableview#readme',
43+
17 silly publish dependencies: { 'react-native': '^0.9.0' },
44+
17 silly publish readme: '# react-native-tableview\nNative iOS UITableView for React Native with JSON support.\n\n## Why I need to use it?\n- To display long lists of data (like country list) - built-in list view has performance issues for long lists\n- To use built-in accessory types (checkmark or disclosure indicator)\n- Automatic scroll to initial selected value during component initialization\n- Automatic item selection with "checkmark" with old item de-selection (optionally), see demo, useful to select country/state/etc.\n- 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!\n- Filter JSON datasources using NSPredicate syntax. For example you could select states for given country only (check demo)\n\n## Supports UITableView styles\n- UITableViewStylePlain (TableView.Consts.Style.Plain)\n- UITableViewStyleGrouped (TableView.Consts.Style.Grouped)\n\n## Supports UITableViewCell styles\n- UITableViewCellStyleDefault (TableView.Consts.CellStyle.Default)\n- UITableViewCellStyleValue1 (TableView.Consts.CellStyle.Value1)\n- UITableViewCellStyleValue2 (TableView.Consts.CellStyle.Value2)\n- UITableViewCellStyleSubtitle (TableView.Consts.CellStyle.Subtitle)\n\n## Supports accessory types\n- UITableViewCellAccessoryDisclosureIndicator ("arrow" attribute for TableView.Item or TableView.Section)\n- UITableViewCellAccessoryCheckmark ("selected" attribute for TableView.Item)\n\n## Example 1\n![demo](https://cloud.githubusercontent.com/assets/1321329/9329083/cc77b4e0-45ae-11e5-9919-34f12e6d407c.gif)\n\n```\n\'use strict\';\n\nvar React = require(\'react-native\');\nvar { AppRegistry } = React;\nvar TableView = require(\'react-native-tableview\');\nvar Section = TableView.Section;\nvar Item = TableView.Item;\n\nclass TableViewExample extends React.Component {\n render(){\n return (\n <TableView style={{flex:1}}\n tableViewStyle={TableView.Consts.Style.Grouped}\n tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}\n onPress={(event) => console.log(event.nativeEvent)}>\n <Section label="Section 1" arrow={true}>\n <Item value="1" detail="Detail1" >Item 1</Item>\n <Item value="2">Item 2</Item>\n <Item>Item 3</Item>\n <Item>Item 4</Item>\n <Item>Item 5</Item>\n <Item>Item 6</Item>\n <Item>Item 7</Item>\n <Item>Item 8</Item>\n <Item>Item 9</Item>\n <Item>Item 10</Item>\n <Item>Item 11</Item>\n <Item>Item 12</Item>\n <Item>Item 13</Item>\n <Item>Item 14</Item>\n <Item>Item 15</Item>\n <Item>Item 16</Item>\n <Item>Item 17</Item>\n <Item>Item 18</Item>\n <Item>Item 19</Item>\n <Item>Item 20</Item>\n <Item>Item 21</Item>\n <Item>Item 22</Item>\n <Item>Item 23</Item>\n <Item>Item 24</Item>\n <Item>Item 25</Item>\n <Item>Item 26</Item>\n <Item>Item 27</Item>\n <Item>Item 28</Item>\n <Item>Item 29</Item>\n <Item>Item 30</Item>\n </Section>\n <Section label="Section 2" arrow={false}>\n <Item selected={true}>Item 1</Item>\n <Item>Item 2</Item>\n <Item>Item 3</Item>\n </Section>\n </TableView>\n );\n }\n}\n\nAppRegistry.registerComponent(\'TableViewExample\', () => TableViewExample);\n```\n## Example 2 (JSON source support), reads country list JSON from app bundle and display UITableView with selected value checkmarked\n![demo2](https://cloud.githubusercontent.com/assets/1321329/9335801/7a4d42ca-45d6-11e5-860c-969db80413ca.gif)\n\n```\n render(){\n return (\n <TableView selectedValue="ES" style={{flex:1}} json="countries"\n tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}\n onPress={(event) => console.log(event.nativeEvent)}/>\n );\n }\n```\n\n### Example 3 (JSON filter and optional items at the beginning)\n```\n // list spanish provinces and add \'All states\' item at the beginning\n render(){\n var country = "ES";\n return (\n <TableView selectedValue="" style={{flex:1}} json="states" filter={`country==\'${country}\'`}\n tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}\n onPress={(event) => console.log(event.nativeEvent)}>\n <Item value="">All states</Item>\n </TableView>\n );\n }\n```\n\n## Getting started\n1. `npm install react-native-tableview --save`\n2. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project\'s name]`\n3. add `./node_modules/react-native-tableview/RCTTableView.xcodeproj`\n4. In the XCode project navigator, select your project, select the `Build Phases` tab and in the `Link Binary With Libraries` section add **libRCTTableView.a**\n5. (optional) If you will use JSON file, add it to iOS application bundle\n6. `var TableView = require(\'react-native-tableview\')`\n',
45+
17 silly publish readmeFilename: 'README.md',
46+
17 silly publish gitHead: 'e61701a8e00a08d5f84322b56f0960ab1f2829ef',
47+
17 silly publish _id: 'react-native-tableview@1.0.2',
48+
17 silly publish _shasum: '6acf284d2e17dd1cb7e7cf273f8558c8718ef62e',
49+
17 silly publish _from: '.' }
50+
18 verbose getPublishConfig undefined
51+
19 silly mapToRegistry name react-native-tableview
52+
20 silly mapToRegistry using default registry
53+
21 silly mapToRegistry registry https://registry.npmjs.org/
54+
22 silly mapToRegistry uri https://registry.npmjs.org/react-native-tableview
55+
23 verbose publish registryBase https://registry.npmjs.org/
56+
24 silly publish uploading /Users/aksonov/.npm/react-native-tableview/1.0.2/package.tgz
57+
25 verbose request uri https://registry.npmjs.org/react-native-tableview
58+
26 verbose request sending authorization for write operation
59+
27 info attempt registry request try #1 at 10:42:55
60+
28 verbose request using bearer token for auth
61+
29 verbose request id 6d9c98d7edde9759
62+
30 http request PUT https://registry.npmjs.org/react-native-tableview
63+
31 http 403 https://registry.npmjs.org/react-native-tableview
64+
32 verbose headers { 'content-type': 'application/json',
65+
32 verbose headers 'cache-control': 'max-age=60',
66+
32 verbose headers 'content-length': '95',
67+
32 verbose headers 'accept-ranges': 'bytes',
68+
32 verbose headers date: 'Wed, 19 Aug 2015 08:42:57 GMT',
69+
32 verbose headers via: '1.1 varnish',
70+
32 verbose headers connection: 'keep-alive',
71+
32 verbose headers 'x-served-by': 'cache-fra1221-FRA',
72+
32 verbose headers 'x-cache': 'MISS',
73+
32 verbose headers 'x-cache-hits': '0',
74+
32 verbose headers 'x-timer': 'S1439973776.026287,VS0,VE1072' }
75+
33 verbose request invalidating /Users/aksonov/.npm/registry.npmjs.org/react-native-tableview on PUT
76+
34 error publish Failed PUT 403
77+
35 verbose stack Error: "You cannot publish over the previously published version 1.0.2." : react-native-tableview
78+
35 verbose stack at CachingRegistryClient.<anonymous> (/Users/aksonov/.nvm/versions/io.js/v2.5.0/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:247:14)
79+
35 verbose stack at Request._callback (/Users/aksonov/.nvm/versions/io.js/v2.5.0/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:170:14)
80+
35 verbose stack at Request.self.callback (/Users/aksonov/.nvm/versions/io.js/v2.5.0/lib/node_modules/npm/node_modules/request/request.js:197:22)
81+
35 verbose stack at emitTwo (events.js:87:13)
82+
35 verbose stack at Request.emit (events.js:172:7)
83+
35 verbose stack at Request.<anonymous> (/Users/aksonov/.nvm/versions/io.js/v2.5.0/lib/node_modules/npm/node_modules/request/request.js:1050:14)
84+
35 verbose stack at emitOne (events.js:82:20)
85+
35 verbose stack at Request.emit (events.js:169:7)
86+
35 verbose stack at IncomingMessage.<anonymous> (/Users/aksonov/.nvm/versions/io.js/v2.5.0/lib/node_modules/npm/node_modules/request/request.js:996:12)
87+
35 verbose stack at emitNone (events.js:72:20)
88+
36 verbose statusCode 403
89+
37 verbose pkgid react-native-tableview
90+
38 verbose cwd /Users/aksonov/Documents/react-native-tableview
91+
39 error Darwin 14.4.0
92+
40 error argv "/Users/aksonov/.nvm/versions/io.js/v2.5.0/bin/iojs" "/Users/aksonov/.nvm/versions/io.js/v2.5.0/bin/npm" "publish" "./"
93+
41 error node v2.5.0
94+
42 error npm v2.13.2
95+
43 error code E403
96+
44 error "You cannot publish over the previously published version 1.0.2." : react-native-tableview
97+
45 error If you need help, you may report this error at:
98+
45 error <https://github.com/npm/npm/issues>
99+
46 verbose exit [ 1, true ]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-tableview",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Native iOS TableView wrapper for React Native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)