2016-02-18 19:59:34 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-10-27 23:19:30 +00:00
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-16 20:21:28 +00:00
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
|
|
import {
|
2016-01-19 23:10:06 +00:00
|
|
|
|
Platform,
|
|
|
|
|
Text,
|
|
|
|
|
TextInput,
|
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
|
View,
|
2016-02-15 23:03:16 +00:00
|
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
|
|
import realm from './realm';
|
|
|
|
|
import styles from './styles';
|
2016-01-19 23:10:06 +00:00
|
|
|
|
|
|
|
|
|
const iOS = (Platform.OS == 'ios');
|
2015-10-27 08:52:26 +00:00
|
|
|
|
|
2016-02-15 23:03:16 +00:00
|
|
|
|
export default class TodoListItem extends React.Component {
|
2015-10-27 08:52:26 +00:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this._onChangeText = this._onChangeText.bind(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get done() {
|
|
|
|
|
let items = this.props.item.items;
|
2017-12-13 16:36:10 +00:00
|
|
|
|
if (items) {
|
|
|
|
|
return items.length > 0 && items.every((item) => item.done);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return this.props.item.done;
|
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get text() {
|
|
|
|
|
return this.props.item.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set text(text) {
|
|
|
|
|
this.props.item.name = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
// The autoFocus prop on TextInput was not working for us :(
|
|
|
|
|
this._focusInputIfNecessary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
|
this._focusInputIfNecessary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<View style={styles.listItem}>
|
|
|
|
|
{this.renderLeftSide()}
|
|
|
|
|
{this.renderText()}
|
|
|
|
|
{this.renderRightSide()}
|
|
|
|
|
</View>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderLeftSide() {
|
|
|
|
|
return (
|
|
|
|
|
<View style={styles.listItemLeftSide}>
|
|
|
|
|
<Text>{this.done ? '✓' : '⁃'}</Text>
|
|
|
|
|
</View>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderRightSide() {
|
|
|
|
|
// Only show the delete button while not editing the text.
|
|
|
|
|
return this.props.editing ? null : this.renderDelete();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 10:08:18 +00:00
|
|
|
|
renderText(extraStyle) {
|
2015-10-27 08:52:26 +00:00
|
|
|
|
if (this.props.editing) {
|
|
|
|
|
return (
|
|
|
|
|
<TextInput
|
|
|
|
|
ref="input"
|
|
|
|
|
value={this.text}
|
2015-10-28 18:25:54 +00:00
|
|
|
|
placeholder="Todo…"
|
2015-10-27 10:08:18 +00:00
|
|
|
|
style={[styles.listItemInput, extraStyle]}
|
2015-10-27 08:52:26 +00:00
|
|
|
|
onChangeText={this._onChangeText}
|
|
|
|
|
onEndEditing={this.props.onEndEditing}
|
|
|
|
|
enablesReturnKeyAutomatically={true} />
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<Text
|
2015-10-27 10:08:18 +00:00
|
|
|
|
style={[styles.listItemText, extraStyle]}
|
2015-10-27 08:52:26 +00:00
|
|
|
|
onPress={this.props.onPress}
|
|
|
|
|
suppressHighlighting={true}>
|
|
|
|
|
{this.text}
|
|
|
|
|
</Text>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDelete() {
|
|
|
|
|
return (
|
|
|
|
|
<TouchableWithoutFeedback onPress={this.props.onPressDelete}>
|
|
|
|
|
<View style={styles.listItemDelete}>
|
2016-01-19 23:10:06 +00:00
|
|
|
|
<Text>{iOS ? '𐄂' : '×'}</Text>
|
2015-10-27 08:52:26 +00:00
|
|
|
|
</View>
|
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onChangeText(text) {
|
|
|
|
|
realm.write(() => {
|
|
|
|
|
this.text = text;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_focusInputIfNecessary() {
|
|
|
|
|
if (!this.props.editing) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let input = this.refs.input;
|
|
|
|
|
if (!input.isFocused()) {
|
|
|
|
|
input.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|