clear input on end editing

This commit is contained in:
ammarahm-ed
2020-12-31 18:53:05 +05:00
parent 1e13c1c286
commit 36c1f0f32d

View File

@@ -1,4 +1,4 @@
import React from 'react'; import React, {createRef} from 'react';
import { import {
Keyboard, Keyboard,
KeyboardAvoidingView, KeyboardAvoidingView,
@@ -54,6 +54,9 @@ export class AddNotebookDialog extends React.Component {
this.titleRef; this.titleRef;
this.descriptionRef; this.descriptionRef;
this.topicsToDelete = []; this.topicsToDelete = [];
this.hiddenInput = createRef();
this.topicInputRef = createRef();
this.addingTopic = false;
} }
open = () => { open = () => {
@@ -118,7 +121,7 @@ export class AddNotebookDialog extends React.Component {
this.prevIndex = null; this.prevIndex = null;
this.prevItem = null; this.prevItem = null;
this.currentInputValue = null; this.currentInputValue = null;
this.topicInputRef.setNativeProps({ this.topicInputRef.current?.setNativeProps({
text: null, text: null,
}); });
} }
@@ -193,9 +196,7 @@ export class AddNotebookDialog extends React.Component {
}; };
onSubmit = (forward = true) => { onSubmit = (forward = true) => {
this.topicInputRef.setNativeProps({ this.hiddenInput.current?.focus();
text: null,
});
let {topics} = this.state; let {topics} = this.state;
if (!this.currentInputValue || this.currentInputValue?.trim().length === 0) if (!this.currentInputValue || this.currentInputValue?.trim().length === 0)
return; return;
@@ -206,7 +207,6 @@ export class AddNotebookDialog extends React.Component {
this.setState({ this.setState({
topics: prevTopics, topics: prevTopics,
}); });
setTimeout(() => { setTimeout(() => {
this.listRef.scrollToEnd({animated: true}); this.listRef.scrollToEnd({animated: true});
}, 30); }, 30);
@@ -219,7 +219,7 @@ export class AddNotebookDialog extends React.Component {
this.currentInputValue = null; this.currentInputValue = null;
console.log('edit topic is', this.state.editTopic); console.log('edit topic is', this.state.editTopic);
if (this.state.editTopic) { if (this.state.editTopic) {
this.topicInputRef.blur(); this.topicInputRef.current?.blur();
Keyboard.dismiss(); Keyboard.dismiss();
this.setState({ this.setState({
editTopic: false, editTopic: false,
@@ -235,6 +235,8 @@ export class AddNotebookDialog extends React.Component {
}, 30); }, 30);
} }
} }
this.topicInputRef.current?.focus();
}; };
render() { render() {
@@ -255,10 +257,20 @@ export class AddNotebookDialog extends React.Component {
animationType={DDS.isTab ? 'fade' : 'slide'} animationType={DDS.isTab ? 'fade' : 'slide'}
onShow={() => { onShow={() => {
this.topicsToDelete = []; this.topicsToDelete = [];
this.titleRef.focus(); this.hiddenInput.current?.focus();
}} }}
onRequestClose={this.close}> onRequestClose={this.close}>
<SafeAreaView> <SafeAreaView>
<TextInput
ref={this.hiddenInput}
style={{
width: 1,
height: 1,
opacity: 0,
position: 'absolute',
}}
blurOnSubmit={false}
/>
<KeyboardAvoidingView <KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null} behavior={Platform.OS === 'ios' ? 'padding' : null}
style={styles.wrapper}> style={styles.wrapper}>
@@ -304,14 +316,15 @@ export class AddNotebookDialog extends React.Component {
}} }}
placeholder="Describe your notebook." placeholder="Describe your notebook."
onSubmit={() => { onSubmit={() => {
this.topicInputRef.focus(); this.topicInputRef.current?.focus();
}} }}
defaultValue={toEdit ? toEdit.description : null} defaultValue={toEdit ? toEdit.description : null}
/> />
<Input <Input
fwdRef={(ref) => (this.topicInputRef = ref)} fwdRef={this.topicInputRef}
testID={notesnook.ids.dialogs.notebook.inputs.topic} testID={notesnook.ids.dialogs.notebook.inputs.topic}
clearTextOnFocus={true}
onChangeText={(value) => { onChangeText={(value) => {
this.currentInputValue = value; this.currentInputValue = value;
if (this.prevItem !== null) { if (this.prevItem !== null) {
@@ -345,10 +358,10 @@ export class AddNotebookDialog extends React.Component {
onPress={(item, index) => { onPress={(item, index) => {
this.prevIndex = index; this.prevIndex = index;
this.prevItem = item; this.prevItem = item;
this.topicInputRef.setNativeProps({ this.topicInputRef.current?.setNativeProps({
text: item, text: item,
}); });
this.topicInputRef.focus(); this.topicInputRef.current?.focus();
this.currentInputValue = item; this.currentInputValue = item;
this.setState({ this.setState({
editTopic: true, editTopic: true,
@@ -461,7 +474,7 @@ const styles = StyleSheet.create({
width: DDS.isTab ? 500 : '100%', width: DDS.isTab ? 500 : '100%',
height: DDS.isTab ? 600 : '100%', height: DDS.isTab ? 600 : '100%',
maxHeight: DDS.isTab ? 600 : '100%', maxHeight: DDS.isTab ? 600 : '100%',
borderRadius: 5, borderRadius: DDS.isTab ? 5 : 0,
paddingHorizontal: 12, paddingHorizontal: 12,
paddingVertical: pv, paddingVertical: pv,
}, },