将搜索结果固定在容器顶部

时间:2020-07-09 14:36:05

标签: javascript css reactjs typescript react-native

我有一个看起来像这样的屏幕:

return (
    <SafeAreaView>
      <View style={styles.container}>
        <View style={styles.topContainer}>
          <View style={styles.searchFieldContainer}>
            <FavoritePointInput
              textChangeHandler={textChangeHandler}
            />
          </View>
          <LocationsFound
            addressesFound={locations.favAddressesFoundList}
          />
          {/* <View style={styles.fixed}> */}
          <View style={styles.fieldDescription}>
            <Text>Set Location's Name:</Text>
          </View>
          <View style={styles.searchFieldContainer}>
            <Item style={styles.searchField}>
              <Input
                placeholder="Library"
                onChangeText={(text) => setLocationName(text)}
                style={styles.searchText}
              />
            </Item>
          </View>
        
          <View style={styles.buttonContainer}>
            <Button style={styles.button}>
              <Text>Save Location</Text>
            </Button>
          </View>
          </View>
        </View>
      {/* </View> */}
    </SafeAreaView>
  );

其Stlyes如下:

export const styles = StyleSheet.create({
  container: {
    height: '100%',
    width: '100%',
  },
  topContainer: {
    height: moderateScale(750),
  },
  topTextContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginVertical: moderateScale(15),
    height: moderateScale(30),
    paddingLeft: moderateScale(30),
    paddingRight: moderateScale(30),
  },
  topMiddleContainer: {
    alignItems: 'center',
  },
  topMiddleText: {
    justifyContent: 'center',
    paddingBottom: 40,
  },
  searchFieldContainer: {
    alignItems: 'center',
    height: moderateScale(120),
  },
  button: {
    width: moderateScale(120),
    borderRadius: moderateScale(20),
    alignItems: 'center',
    alignContent: 'center',
  },
  buttonContainer: {
    flexDirection: 'row',
    justifyContent: 'center',
  },
  searchField: {
    width: moderateScale(300, 0.3),
    height: verticalScale(40),
    marginVertical: moderateScale(3),
    borderRadius: verticalScale(10),
  },
  fieldDescription: {
    alignItems: 'center',
  },
  fixed: {
    position: 'absolute',
  }
});

当我在第一个输入字段中输入内容时,我会通过LocationsFound组件获得搜索结果。这会干扰整个样式,并向下推第二个输入字段。我希望它简单地重叠并位于第二个字段的顶部,直到选择其中一个。有可能吗?

enter image description here

enter image description here

编辑: enter image description here

1 个答案:

答案 0 :(得分:1)

从代码中可以看出,<LocationsFound>是您的下拉菜单。您需要设置此组件的样式并为其提供绝对位置,以将其从UI的常规定位流程中删除。请注意,您相对于其最接近的祖先放置绝对作品,因此,您还需要相对地定位其父作品。

在您的标记中。

<LocationsFound
  styles={styles.dropdown}
  addressesFound={locations.favAddressesFoundList}
/>

以您的风格

searchFieldContainer {
  position: relative,
  // other attributes
}

dropdown {
  position: absolute,
  // adjust top and left according to your situation e.g. 10px
  top: 0;
  left: 0;
}