-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagEditModal.js
123 lines (110 loc) · 3.6 KB
/
TagEditModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React from 'react';
import TagData from './TagData'
import {
ActivityIndicator,
SafeAreaView,
View,
StyleSheet,
} from 'react-native';
// Local Imports
import TagEditorView from './TagEditorView'
// Paper
import { Appbar, FAB, TextInput } from "react-native-paper";
'use strict';
export default class TagEditModal extends React.Component {
constructor(props) {
super(props);
this.state = {
editModalVisible: false,
items: [],
nodeData: undefined,
}
this.newNode = true;
this.input = React.createRef();
}
componentDidMount = async () => {
this.tagData = await TagData.getRoot();
var node = this.tagData.getNodeByPath(this.props.route.params.path)
if (node) {
this.newNode = false;
this.title = node.title;
this.setState({ nodeData: this.tagData.rootNode, items: node.data ?? [] });
} else {
this.setState({ nodeData: this.tagData.rootNode });
}
}
onRemove = (text) => {
console.log("Removing #" + text);
var newItems = this.state.items ? this.state.items.slice(0) : new Array();
var idx = newItems.indexOf(text);
newItems.splice(idx, 1);
this.setState({ items: newItems });
return true;
}
onAddItems = (items) => {
this.setState({ items: this.state.items.concat(items), editModalVisible: false });
}
onReorderItems = (items) => {
// We're going to trust that the items are all there. Right?
let itemsText = items.map(({ text }) => text);
this.setState({ items: itemsText });
};
render() {
if (this.state.nodeData) {
return (
<View style={{ flex: 1 }}>
<Appbar.Header>
<Appbar.Content
title="Edit Section"
/>
<Appbar.BackAction
onPress={async () => {
this.setState({ nodeData: null })
if (this.tagData.updatePath(this.props.route.params.path, this.state.items, this.title)) {
await this.tagData.commit();
this.props.navigation.push("Root", { nodeData: this.tagData.rootNode });
return;
}
this.props.navigation.goBack();
}}
/>
</Appbar.Header>
<SafeAreaView style={{ flex: 1.0, flexDirection: 'column' }}>
<TextInput
style={{ margin: 8, minWidth: 160, backgroundColor: '#fff1' }}
label="Section Title"
autoFocus={this.newNode}
defaultValue={this.title}
onChangeText={(value) => { this.title = value }}
/>
<View style={{ margin: 8, borderWidth: 1, borderRadius: 4, borderColor: "#0004", flex: 1 }}>
<TagEditorView
style={{ width: '100%', height: '100%' }}
items={this.state.items.map(x => { return { text: x } })}
onRemoveItem={this.onRemove}
onAddItems={this.onAddItems}
onReorderItems={this.onReorderItems}
editModalVisible={this.state.editModalVisible}
onRequestModalClose={() => this.setState({ editModalVisible: false })} />
</View>
<FAB style={styles.bottomFab} icon="plus" onPress={() => this.setState({ editModalVisible: true })} />
</SafeAreaView>
</View>
);
} else {
return (
<View style={{ justifyContent: 'center' }}>
<ActivityIndicator size="large" style={{ padding: 32 }} />
</View>
);
}
}
};
const styles = StyleSheet.create({
bottomFab: {
position: 'absolute',
right: 0,
bottom: 0,
margin: 16,
},
})