Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reordering handlers #186

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,81 @@ an `imageWidth` is unnecessary.
;<Item image={require('../images/icon-success.png')} />
```

## TableView inside ScrollView (like iOS Customise Control Center)

In some cases you need place your TableView into ScrollView. There is some problems with handling
scroll events, so we don't recommend do it with large number of items
### Without reordering

Just explicitly set height for `TableView` and disable it's scrolling.

```jsx
render() {
return (
<ScrollView style={{ flex: 1 }}>
<TableView
scrollEnabled={false}
style={{
// You should explicitly set height for TableView
// default height of header in iOS is 26, row height is 44
height: headerHeight + (items.count * itemHeight),
}}
>
<Section>
{items.map(obj => (
<Item key={obj.id} label={obj.name} />
))}
</Section>
</TableView>
</ScrollView>
)
}
```

### With reordering

Explicitly set height for `TableView`, disable it's scrolling and handle reorder actions
for toggling `scrollEnabled` prop of container `ScrollView`:

```jsx
render() {
return (
<ScrollView
scrollEnabled={this.state.scrollEnabled}
style={{ flex: 1 }}
>
<TableView
editing
scrollEnabled={false}
onReorderingStart={() => {
this.setState({ scrollEnabled: false });
}}
onReorderingEnd={() => {
this.setState({ scrollEnabled: true });
}}
onReorderingCancel={() => {
this.setState({ scrollEnabled: true });
}}
style={{
// You should explicitly set height for TableView
// default height of header in iOS is 26, row height is 44
height: headerHeight + (items.count * itemHeight),
}}
>
<Section canMove canEdit>
{items.map(obj => (
<Item
key={obj.id}
label={obj.name}
/>
))}
</Section>
</TableView>
</ScrollView>
)
}
```

### Editable Complex Components

Only `Item`s can be edited or moved. However you can create a complex component
Expand Down
4 changes: 4 additions & 0 deletions RNTableView/RNTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
@property(nonatomic, copy) RCTDirectEventBlock onScroll;
@property(nonatomic, copy) RCTDirectEventBlock onRefresh;

@property(nonatomic, copy) RCTBubblingEventBlock onReorderingStart;
@property(nonatomic, copy) RCTBubblingEventBlock onReorderingEnd;
@property(nonatomic, copy) RCTBubblingEventBlock onReorderingCancel;

- (void)addRefresh;
- (void)stopRefreshing;
- (void)startRefreshing;
Expand Down
18 changes: 18 additions & 0 deletions RNTableView/RNTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,24 @@ - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)
return [value[@"canMove"] boolValue];
}

- (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.onReorderingStart) {
self.onReorderingStart(@{});
}
}

- (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.onReorderingEnd) {
self.onReorderingEnd(@{});
}
}

- (void)tableView:(UITableView *)tableView didCancelReorderingRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.onReorderingCancel) {
self.onReorderingCancel(@{});
}
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
self.onChange(@{@"target":self.reactTag, @"sourceIndex":@(sourceIndexPath.row), @"sourceSection": @(sourceIndexPath.section), @"destinationIndex":@(destinationIndexPath.row), @"destinationSection":@(destinationIndexPath.section), @"mode": @"move"});
}
Expand Down
4 changes: 4 additions & 0 deletions RNTableView/RNTableViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ - (NSArray *)customDirectEventTypes
RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onRefresh, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onReorderingStart, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onReorderingEnd, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onReorderingCancel, RCTBubblingEventBlock)

RCT_CUSTOM_VIEW_PROPERTY(refreshing, BOOL, RNTableView) {
view.refreshing = [RCTConvert BOOL:json];
}
Expand Down
7 changes: 7 additions & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Example5 from './screens/Example5'
import Example6 from './screens/Example6'
import Example7 from './screens/Example7'
import Example8 from './screens/Example8'
import Example9 from './screens/Example9'

import TableViewExampleCell from './cells/TableViewExampleCell'

Expand Down Expand Up @@ -68,6 +69,12 @@ const Stack = createStackNavigator(
title: 'Scroll to Index',
},
},
insideScrollView: {
screen: Example9,
navigationOptions: {
title: 'Reorderable TableView in ScrollView',
},
},
},
{
navigationOptions: {
Expand Down
2 changes: 1 addition & 1 deletion example/src/screens/Example8.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Section, Item } = TableView
class Example8 extends React.Component{
render() {
return(
<View style={{flex: 1}}>
<View style={{ flex: 1 }}>

<Button title="Scroll To Section 2" onPress={() => this.tableView.scrollToIndex({index: 2, section: 1})} />

Expand Down
62 changes: 62 additions & 0 deletions example/src/screens/Example9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react'
import { ScrollView } from 'react-native'
import TableView from 'react-native-tableview'

const { Section, Item } = TableView
const headerHeight = 26;
const itemHeight = 44;

class Example9 extends React.Component {
state = {
scrollEnabled: true,
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
{ id: 4, name: 'Item 4' },
{ id: 5, name: 'Item 5' },
{ id: 6, name: 'Item 6' },
],
}

render() {
const { scrollEnabled, items } = this.state;

return(
<ScrollView
scrollEnabled={scrollEnabled}
contentContainerStyle={{ flex: 1 }}
>
<TableView
editing
scrollEnabled={false}
onReorderingStart={() => {
this.setState({ scrollEnabled: false });
}}
onReorderingEnd={() => {
this.setState({ scrollEnabled: true });
}}
onReorderingCancel={() => {
this.setState({ scrollEnabled: true });
}}
style={{
// You should explicitly set height for TableView
// default height of header in iOS is 26, row height is 44
height: headerHeight + (items.length * itemHeight),
}}
>
<Section canMove canEdit>
{items.map(obj => (
<Item
key={obj.id}
label={obj.name}
/>
))}
</Section>
</TableView>
</ScrollView>
);
}
}

export default Example9
1 change: 1 addition & 0 deletions example/src/screens/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const App = ({ navigation }: NavigationScreenConfigProps) => {
<Item onPress={() => navigate('edit', { editing: true })}>Editing mode</Item>
<Item onPress={() => navigate('refresh')}>Pull to Refresh</Item>
<Item onPress={() => navigate('index')}>Scroll To Index</Item>
<Item onPress={() => navigate('insideScrollView')}>Reorderable Table in ScrollView</Item>
</Section>
</TableView>
)
Expand Down
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ interface TableViewProps {
* Fired when pull to refresh is active
*/
onRefresh?(): void
onReorderingStart?(): void
onReorderingEnd?(): void
onReorderingCancel?(): void
onAccessoryPress?(event: AccessoryCallBack): void
onWillDisplayCell?(event: DisplayCallBack): void
onEndDisplayingCell?(event: DisplayCallBack): void
Expand Down