Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More shared code refactoring in native-land
- Loading branch information
Showing
5 changed files
with
126 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,22 @@ | ||
var React = require('react-native'); | ||
|
||
var { Text, TextInput, View, ListView, SwitchAndroid, | ||
TouchableHighlight } = React; | ||
var { SwitchAndroid } = React; | ||
|
||
var Views = require('../index'); | ||
var styles = require('./styles'); | ||
|
||
var TodoItem = module.exports = React.createClass({ | ||
|
||
mixins: [Views.TodoItemCommonMixin], | ||
|
||
render() { | ||
|
||
var title = (!this.state.editing) ? ( | ||
<Text style={styles.todoTitle} onPress={this.handleEditStart}> | ||
{this.state.item.title} | ||
</Text> | ||
) : ( | ||
<View style={styles.todoEditGroup}> | ||
|
||
<TextInput | ||
style={styles.todoEditField} | ||
ref="editField" | ||
autoFocus={true} | ||
defaultValue={this.state.item.title} | ||
onSubmitEditing={this.handleEditSubmit} /> | ||
|
||
<TouchableHighlight | ||
style={styles.todoDeleteButton} | ||
activeOpacity={0.6} | ||
underlayColor={'red'} | ||
onPress={this.handleDelete}> | ||
|
||
<Text style={styles.todoDeleteButtonText}>Delete</Text> | ||
|
||
</TouchableHighlight> | ||
|
||
</View> | ||
); | ||
|
||
styles: styles, | ||
mixins: [ | ||
Views.TodoItemCommonMixin, | ||
Views.TodoItemNativeCommonMixin | ||
], | ||
renderTodoCompleted() { | ||
return ( | ||
<View style={styles.todoItem}> | ||
<SwitchAndroid | ||
style={styles.todoCompleted} | ||
onValueChange={this.handleCompletedChange} | ||
value={this.state.item.completed} /> | ||
{title} | ||
</View> | ||
<SwitchAndroid | ||
style={this.styles.todoCompleted} | ||
onValueChange={this.handleCompletedChange} | ||
value={this.state.item.completed} /> | ||
); | ||
|
||
}, | ||
|
||
handleEditSubmit(event) { | ||
var val = event.nativeEvent.text.trim(); | ||
this.refs.editField.setNativeProps({text: ''}); | ||
this.setState({ editing: false }); | ||
this.state.item.title = val; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,16 @@ | ||
var React = require('react-native'); | ||
|
||
var { ListView } = React; | ||
|
||
var Views = require('../index'); | ||
var styles = require('./styles'); | ||
var TodoItem = require('./TodoItem'); | ||
|
||
var TodoList = module.exports = React.createClass({ | ||
|
||
mixins: [Views.TodoListCommonMixin], | ||
|
||
render() { | ||
return ( | ||
<ListView | ||
style={styles.todoList} | ||
dataSource={this.state.dataSource} | ||
renderRow={(item) => | ||
<TodoItem key={item.cid} item={item} />} | ||
/> | ||
); | ||
}, | ||
|
||
getInitialState() { | ||
return { | ||
collection: this.props.collection, | ||
dataSource: new React.ListView.DataSource({ | ||
rowHasChanged: (row1, row2) => row1 !== row2 | ||
}) | ||
}; | ||
}, | ||
|
||
componentDidMount() { | ||
// TODO: Should this happen in getInitialState instead? | ||
this.setState({ dataSource: this.updateDataSource() }); | ||
|
||
this.state.collection.on('change add remove', () => { | ||
this.setState({ dataSource: this.updateDataSource() }); | ||
}, this); | ||
}, | ||
|
||
updateDataSource() { | ||
// NOTE: The DataSource retains a reference to the previous model list in | ||
// order to diff it against new lists to detect changes. So, we have to | ||
// make a fresh clone of the list, every time. | ||
// TODO: Is there a better way to do this? | ||
var rows = this.state.collection.map((model) => model); | ||
return this.state.dataSource.cloneWithRows(rows); | ||
styles: styles, | ||
mixins: [ | ||
Views.TodoListCommonMixin, | ||
Views.TodoListNativeCommonMixin | ||
], | ||
renderRow(item) { | ||
return (<TodoItem key={item.cid} item={item} />); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,22 @@ | ||
var React = require('react-native'); | ||
|
||
var { Text, TextInput, View, ListView, SwitchIOS, | ||
TouchableHighlight } = React; | ||
var { SwitchIOS } = React; | ||
|
||
var Views = require('../index'); | ||
var styles = require('./styles'); | ||
|
||
var TodoItem = module.exports = React.createClass({ | ||
|
||
mixins: [Views.TodoItemCommonMixin], | ||
|
||
render() { | ||
|
||
var title = (!this.state.editing) ? ( | ||
<Text style={styles.todoTitle} onPress={this.handleEditStart}> | ||
{this.state.item.title} | ||
</Text> | ||
) : ( | ||
<View style={styles.todoEditGroup}> | ||
|
||
<TextInput | ||
style={styles.todoEditField} | ||
ref="editField" | ||
autoFocus={true} | ||
defaultValue={this.state.item.title} | ||
onSubmitEditing={this.handleEditSubmit} /> | ||
|
||
<TouchableHighlight | ||
style={styles.todoDeleteButton} | ||
activeOpacity={0.6} | ||
underlayColor={'red'} | ||
onPress={this.handleDelete}> | ||
|
||
<Text style={styles.todoDeleteButtonText}>Delete</Text> | ||
|
||
</TouchableHighlight> | ||
|
||
</View> | ||
); | ||
|
||
styles: styles, | ||
mixins: [ | ||
Views.TodoItemCommonMixin, | ||
Views.TodoItemNativeCommonMixin | ||
], | ||
renderTodoCompleted() { | ||
return ( | ||
<View style={styles.todoItem}> | ||
<SwitchIOS | ||
style={styles.todoCompleted} | ||
onValueChange={this.handleCompletedChange} | ||
value={this.state.item.completed} /> | ||
{title} | ||
</View> | ||
<SwitchIOS | ||
style={styles.todoCompleted} | ||
onValueChange={this.handleCompletedChange} | ||
value={this.state.item.completed} /> | ||
); | ||
|
||
}, | ||
|
||
handleEditSubmit(event) { | ||
var val = event.nativeEvent.text.trim(); | ||
this.refs.editField.setNativeProps({text: ''}); | ||
this.setState({ editing: false }); | ||
this.state.item.title = val; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,16 @@ | ||
var React = require('react-native'); | ||
|
||
var { ListView } = React; | ||
|
||
var Views = require('../index'); | ||
var styles = require('./styles'); | ||
var TodoItem = require('./TodoItem'); | ||
|
||
var TodoList = module.exports = React.createClass({ | ||
|
||
mixins: [Views.TodoListCommonMixin], | ||
|
||
render() { | ||
return ( | ||
<ListView | ||
style={styles.todoList} | ||
dataSource={this.state.dataSource} | ||
renderRow={(item) => | ||
<TodoItem key={item.cid} item={item} />} | ||
/> | ||
); | ||
}, | ||
|
||
getInitialState() { | ||
return { | ||
collection: this.props.collection, | ||
dataSource: new React.ListView.DataSource({ | ||
rowHasChanged: (row1, row2) => row1 !== row2 | ||
}) | ||
}; | ||
}, | ||
|
||
componentDidMount() { | ||
// TODO: Should this happen in getInitialState instead? | ||
this.setState({ dataSource: this.updateDataSource() }); | ||
|
||
this.state.collection.on('change add remove', () => { | ||
this.setState({ dataSource: this.updateDataSource() }); | ||
}, this); | ||
}, | ||
|
||
updateDataSource() { | ||
// NOTE: The DataSource retains a reference to the previous model list in | ||
// order to diff it against new lists to detect changes. So, we have to | ||
// make a fresh clone of the list, every time. | ||
// TODO: Is there a better way to do this? | ||
var rows = this.state.collection.map((model) => model); | ||
return this.state.dataSource.cloneWithRows(rows); | ||
styles: styles, | ||
mixins: [ | ||
Views.TodoListCommonMixin, | ||
Views.TodoListNativeCommonMixin | ||
], | ||
renderRow(item) { | ||
return (<TodoItem key={item.cid} item={item} />); | ||
} | ||
|
||
}); |