简体   繁体   English

当输入失去焦点时,如何防止屏幕滚动到 x:0, y:0?

[英]How do I prevent the screen from scrolling to x:0, y:0 when an input loses focus?

I'm of aware of this:https://github.com/APSL/react-native-keyboard-aware-scroll-view &我知道这一点:https ://github.com/APSL/react-native-keyboard-aware-scroll-view &

import { KeyboardAvoidingView } from "react-native";

but I don't think I'm doing something right.但我认为我做的不对。

The code below is an example of one of the many forms on the application.下面的代码是应用程序中众多表单之一的示例。 The issue is when the user deselects an input field, the screen jumps all the way to the top of the form.问题是当用户取消选择输入字段时,屏幕会一直跳到表单的顶部。

Imagine filling out a form with 10 inputs and every time you were done with a field, the screen jumps to the top.想象一下用 10 个输入填写一个表单,每次完成一个字段时,屏幕都会跳到顶部。 That's poor UX and I'm trying to prevent that from happening.这是糟糕的用户体验,我正在努力防止这种情况发生。

I'm using redux-form and native-base.我正在使用 redux-form 和 native-base。

(I have a switch in the sidebar which toggles a boolean. If it's true, then input will display what's stored in its auto_fill prop. This has nothing to do with the issue, just a little bit of context to understand the code better.) (我在侧边栏中有一个开关可以切换布尔值。如果为真,则输入将显示存储在其 auto_fill 道具中的内容。这与问题无关,只是一点上下文可以更好地理解代码。)

<Form>
  <Field
    label="Address *"
    name="address" 
    component={SmartInput}
    auto_fill={side_bar_switch ? "182 Blink" : null}
    validate={
      side_bar_switch ? null : [required, minLength1, maxLength100]
    }
  />
  <Field
    label="City *"
    name="city" 
    component={SmartInput}
    auto_fill={side_bar_switch ? "Los Angeles" : null}
    validate={
      side_bar_switch ? null : [required, minLength1, maxLength30]
    }
  />
  <Field
    label="Apt Number"
    name="apt" 
    component={SmartInput}
    auto_fill={side_bar_switch ? "5" : null}
    validate={side_bar_switch ? null : [maxLength5]}
  />
  <Field
    label="ZIP code *"
    name="zipcode" 
    component={SmartInput}
    auto_fill={side_bar_switch ? "90210" : null}
    validate={
      side_bar_switch ? null : [required, number, maxLength5]
    }
  />
  <Field
    label="State *" 
    name="state"
    component={SmartInput}
    auto_fill={side_bar_switch ? "CALIFORNIA" : null}
    validate={side_bar_switch ? null : [required]}
  />
  <Field
    label="Phone Number *"
    name="phoneNumber" 
    component={SmartInput}
    auto_fill={side_bar_switch ? "2816769900" : null}
    validate={side_bar_switch ? null : [required, phoneNumber]}
  />
  <Field
    label="Email *"
    name="app_email"
    component={SmartInput}
    auto_fill={side_bar_switch ? "threadpool.io@x_x.com" : null}
    validate={
      side_bar_switch ? null : [required, email, maxLength50]
    }
  />
</Form>

I have created a component for this use case我为这个用例创建了一个组件

its a wrapper component use this instead of View when playing with TextInput它的包装组件在使用 TextInput 时使用 this 而不是 View

import React, {Component} from "react";
import {
StyleSheet,
KeyboardAvoidingView,
ScrollView,
} from "react-native";
import themeConstants from "../../theme/themeConstants";

class KeyboardAwareScrollView extends Component {

render() {
return (
  <ScrollView style={styles.container}>
    <KeyboardAvoidingView style={styles.container2} behavior="padding" enabled>
      {this.props.children}
    </KeyboardAvoidingView>
  </ScrollView>
 );
}
}

export default KeyboardAwareScrollView;

const styles = StyleSheet.create({
container: {
flex: 1,

backgroundColor: themeConstants.offWhite
},
container2: {
flex: 1,
justifyContent: "flex-start",
paddingTop: 30,
alignItems: "center",
backgroundColor: themeConstants.offWhite
},
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当您专注于新输入时,如何防止在移动设备上滚动? - How to Prevent Scrolling on mobile when you focus on a new input? 当焦点从 aria-live 区域 div 获得输入字段时,如何防止屏幕阅读器停止阅读内容? - How can I prevent the screen reader from stopping to read the content when the focus is gained to the input field from an aria-live region div? 在我做“x=y”并改变“x”之后,它也改变了“y”。 我该如何防止这种情况? - After I do “x=y” and change “x”, it also changes “y”. How do I prevent this? 如何防止触摸屏滚动输入元素? - How to prevent touch screen scrolling for input elements? 如何防止文本框失去焦点后提交按钮的提交 - How to prevent submit button from submitting after textbox loses focus 每当输入字段失去焦点时,如何执行我的 function? - How do I execute my function whenever the input field loses its focus? 使用Javascript Focus时防止滚动到输入框 - Prevent scrolling to input box when Javascript Focus is used 隐藏(jQuery)时如何防止将注意力集中在输入上? - How to prevent getting focus on an input when i hide it (jQuery)? 防止iPhone Web应用程序滚动输入字段的焦点 - Prevent iPhone web-app from scrolling on focus of an input field 如何停止dojo滚动以在焦点上形成小部件? - How do I stop dojo from scrolling to form widgets on focus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM