npm 如何在react-native中输入文本时获得建议

vybvopom  于 2023-11-19  发布在  React
关注(0)|答案(2)|浏览(191)

我想要一个文本输入框,它应该在输入时显示建议,如果没有建议,它应该接受输入的输入,否则它应该接受建议数组的输入。我如何实现这一点?我已经通过了一些文档和模块react-native-autocomplete-input,但无法理解代码。有人能帮助我吗?

guykilcj

guykilcj1#

你也可以像下面这样构建自己的组件。这样你就可以更好地控制组件并修改行为。
在这个例子中,我使用https://callstack.github.io/react-native-paper/text-input.html

  1. import { View } from "react-native";
  2. import { Menu, TextInput } from "react-native-paper";
  3. import { bs } from "../../styles";
  4. import React, { useState } from "react";
  5. const Autocomplete = ({
  6. value: origValue,
  7. label,
  8. data,
  9. containerStyle,
  10. onChange: origOnChange,
  11. icon = 'bike',
  12. style = {},
  13. menuStyle = {},
  14. right = () => {},
  15. left = () => {},
  16. }) => {
  17. const [value, setValue] = useState(origValue);
  18. const [menuVisible, setMenuVisible] = useState(false);
  19. const [filteredData, setFilteredData] = useState([]);
  20. const filterData = (text) => {
  21. return data.filter(
  22. (val) => val?.toLowerCase()?.indexOf(text?.toLowerCase()) > -1
  23. );
  24. };
  25. return (
  26. <View style={[containerStyle]}>
  27. <TextInput
  28. onFocus={() => {
  29. if (value.length === 0) {
  30. setMenuVisible(true);
  31. }
  32. }}
  33. // onBlur={() => setMenuVisible(false)}
  34. label={label}
  35. right={right}
  36. left={left}
  37. style={style}
  38. onChangeText={(text) => {
  39. origOnChange(text);
  40. if (text && text.length > 0) {
  41. setFilteredData(filterData(text));
  42. } else if (text && text.length === 0) {
  43. setFilteredData(data);
  44. }
  45. setMenuVisible(true);
  46. setValue(text);
  47. }}
  48. value={value}
  49. />
  50. {menuVisible && filteredData && (
  51. <View
  52. style={{
  53. flex: 1,
  54. backgroundColor: 'white',
  55. borderWidth: 2,
  56. flexDirection: 'column',
  57. borderColor: 'grey',
  58. }}
  59. >
  60. {filteredData.map((datum, i) => (
  61. <Menu.Item
  62. key={i}
  63. style={[{ width: '100%' }, bs.borderBottom, menuStyle]}
  64. icon={icon}
  65. onPress={() => {
  66. setValue(datum);
  67. setMenuVisible(false);
  68. }}
  69. title={datum}
  70. />
  71. ))}
  72. </View>
  73. )}
  74. </View>
  75. );
  76. };
  77. export default Autocomplete;

字符串

用法

  1. <Autocomplete
  2. value={'Honda'}
  3. style={[style.input]}
  4. containerStyle={[bs.my2]}
  5. label="Model"
  6. data={['Honda', 'Yamaha', 'Suzuki', 'TVS']}
  7. menuStyle={{backgroundColor: 'white'}}
  8. onChange={() => {}}
  9. />

展开查看全部
ht4b089n

ht4b089n2#

  1. <Autocomplete
  2. autoCapitalize="none"
  3. autoCorrect={false}
  4. containerStyle={styles.autocompleteContainer}
  5. //data to show in suggestion
  6. data={films.length === 1 && comp(query, films[0].title) ? [] : films}
  7. //default value if you want to set something in input
  8. defaultValue={query}
  9. /*onchange of the text changing the state of the query which will trigger
  10. the findFilm method to show the suggestions*/
  11. onChangeText={text => this.setState({ query: text })}
  12. placeholder="Enter the film title"
  13. renderItem={({ item }) => (
  14. //you can change the view you want to show in suggestion from here
  15. <TouchableOpacity onPress={() => this.setState({ query: item.title })}>
  16. <Text style={styles.itemText}>
  17. {item.title} ({item.release_date})
  18. </Text>
  19. </TouchableOpacity>
  20. )}
  21. />

字符串
这是从aboutreact.com得到的,这里的注解解释了你想传递给特定区域的东西。我建议尝试传递一个数组作为数据属性。

展开查看全部

相关问题