reactjs - 错误 - PrimeReact 自动完成建议未显示

标签 reactjs react-redux primereact react-lifecycle-hooks

https://primefaces.org/primereact/showcase/#/autocomplete

我试图在组件加载 componentDidMount 中的一些数据后立即加载建议下拉列表。 suggestionsList 正在使用 componentDidMount 中的 obj 数据进行更新,但建议下拉列表未显示。

简单地说,每当输入获得焦点并且没有输入文本时,就会显示一个建议下拉列表。

abcCmp.jsx

class abcCmp extends React.Component {
      state;
      constructor() {
            super();
            this.state = {
                suggestionsList: []
            };
      }
    
    componentDidMount() {
      let obj = [{'color':'red',name: 'Danny', id: '1'}];
      this.setState({suggestionsList: [...obj]})
    }
    
    render(){
           return (
                <div className="containerBox">
                   <AutoComplete suggestions={this.state.suggestionsList}
                                minLength={1} placeholder="Add People" field="name"  multiple={true}  
                     autoFocus={true} />
                </div>
           )
    }

最佳答案

如果您阅读了文档,还需要其他参数。 它们是:completeMethodvalueonChange,其中 completeMethod 需要在您键入时显示筛选列表。在 completeMethod 中,您需要过滤数据,这就是当您输入更多内容时下拉列表会减少的方式。

您需要 ref 来切换下拉功能,并且如果输入值为空且未选择任何值,则还需要检查焦点,以便切换下拉菜单。

这是我创建的简单 POC,供引用。尝试输入 D

代码:

import React from "react";
import { AutoComplete } from "primereact/autocomplete";
import "./styles.css";
let obj = [
  { color: "red", name: "Dagny", id: "1" },
  { color: "red", name: "kanny", id: "2" },
  { color: "red", name: "Dgnny", id: "3" },
  { color: "red", name: "Danny", id: "4" },
  { color: "red", name: "Dmnny", id: "5" },
  { color: "red", name: "Donny", id: "" }
];
class MyComponent extends React.Component {
  myRef = React.createRef();
  constructor() {
    super();
    this.state = {
      suggestionsList: [],
      selected: null,
      inputValue: null
    };
  }

  componentDidMount() {
    this.setState({ suggestionsList: [...obj] });
  }
  searchList = (event) => {
    let suggestionsList;

    if (!event.query.trim().length) {
      suggestionsList = [...obj];
    } else {
      suggestionsList = [...obj].filter((list) => {
        return list.name.toLowerCase().startsWith(event.query.toLowerCase());
      });
    }

    this.setState({ suggestionsList });
  };

  render() {
    return (
      <div className="containerBox">
        <AutoComplete
          suggestions={this.state.suggestionsList}
          completeMethod={this.searchList}
          minLength={1}
          ref={this.myRef}
          dropdown
          inputId="my"
          placeholder="Add People"
          field="name"
          onFocus={(e) => {
            if (!this.state.inputValue && !this.state.selected) {
              this.myRef.current.onDropdownClick(e, "");
            }
          }}
          onKeyUp={(e) => this.setState({ inputValue: e.target.value })}
          // completeOnFocus={true}
          multiple={true}
          autoFocus={true}
          value={this.state.selected}
          onChange={(e) => this.setState({ selected: e.value })}
        />
      </div>
    );
  }
}

export default function App() {
  return (
    <div className="App">
      <MyComponent />
    </div>
  );
}


演示:https://codesandbox.io/s/prime-react-autocomplete-forked-n3x2x?file=/src/App.js

关于reactjs - 错误 - PrimeReact 自动完成建议未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63494712/

相关文章:

javascript - React router v4 使用声明式重定向而不渲染当前组件

javascript - React-Redux:开 Jest 未能测试异步操作(始终通过)

reactjs - 连接 redux 功能不适用于 react native

javascript - Prime React 表中的按钮不起作用

reactjs - 如何在 React js 中自动更新站点地图

reactjs - 在不同容器中渲染组件并共享状态

javascript - Redux 存储和 PouchDB 未与 redux-pouchdb 同步

reactjs - 从 ref 获取 HTMLInputElement 到 PrimeReact 的 InputText

reactjs - 无法转到完整日历上的特定日期

javascript - 为什么react-router setRouteLeaveHook破坏了索引链接?