How to use react autocomplete package to select users from input in React

Viewed 14

i am using https://www.npmjs.com/package/react-search-autocomplete package to select users from input field but it is not displaying the results. can any one help what is wrong with my code ?

const Home = ({socket, username, onlineUsers}) => {
  const [message, setMessage] = useState("");
  const handleSubmitMessage =(e)=> {
    e.preventDefault();
    const newMessage = {
        text: message,
        sender: username,
        id: socket.id,
        timeStamp: Date.now()
    }
    socket.emit("sendmessage", newMessage)
  }

  const handleOnSearch = (string, results) => {
    // onSearch will have as the first callback parameter
    // the string searched and for the second the results.
    console.log(string, results)
  }

  const handleOnHover = (result) => {
    // the item hovered
    console.log(result)
  }

  const handleOnSelect = (item) => {
    // the item selected
    console.log("ITEM",item)
  }

  const handleOnFocus = () => {
    console.log('Focused')
  }

  const formatResult = (item) => {
    return (
      <>
        <span style={{ display: 'block', textAlign: 'left' }}>name: {item.name}</span>
      </>
    )
  }
return (
    <Container fluid className="px-4">
      <Row className="my-3 d-flex justify-content-center" style={{ height: "95vh" }}>
        <Col md={6} className="p-4 d-flex flex-column justify-content-between chat-container">
          {/* USERNAME INPUT */}
          <div style={{ width: 400 }}>
          <ReactSearchAutocomplete
            items={onlineUsers}
            onSearch={handleOnSearch}
            onHover={handleOnHover}
            onSelect={handleOnSelect}
            onFocus={handleOnFocus}
            autoFocus
            formatResult={formatResult}
          />
        </div>
          {/* MESSAGE BOX */}
          <ListGroup >
            <ListGroup.Item className="message-input">Cras justo odio</ListGroup.Item>
          </ListGroup>
          {/* NEW MESSAGE INPUT */}
          <Form className="d-flex" >
            <Form.Control
            className="inputs rounded-pill"
              type="text"
              placeholder="What is your message ?"
              value={message}
              onChange={(e) => setMessage(e.target.value)}
            />
            <BiSend className="text-info" style={{fontSize:"39px"}} onClick={handleSubmitMessage}/>
          </Form>
        </Col>
      </Row>
    </Container>
  );

Here is my onlineUsers object. When i use react-autocomplete package default data it is working but with this object did not work

[
    {
        "username": "Asd",
        "id": "BB-z5RFducnJ029xAAAX"
    }
]
1 Answers

By default the search will be done on the property "name" but you have username instead, to change this behavior, change the fuseOptions prop. and that the component uses the key "name" in your items list to display the result. but your list of items does not have a "name" key, use resultStringKeyName to tell what key ('username') to use to display in the results.

just change :

<span style={{ display: 'block', textAlign: 'left' }}>name: {item.name}</span>

to :

<span style={{ display: 'block', textAlign: 'left' }}>name: {item.username}</span>

and change :

<ReactSearchAutocomplete
            items={onlineUsers}
            onSearch={handleOnSearch}
            onHover={handleOnHover}
            onSelect={handleOnSelect}
            onFocus={handleOnFocus}
            autoFocus
            formatResult={formatResult}
          />

to :

<ReactSearchAutocomplete
            fuseOptions={{ keys: ["username"] }}
            resultStringKeyName="username"
            items={onlineUsers}
            onSearch={handleOnSearch}
            onHover={handleOnHover}
            onSelect={handleOnSelect}
            onFocus={handleOnFocus}
            autoFocus
            formatResult={formatResult}
          />

this is a demo in codesandbox

Related