react ant design 5 search bar example

react ant design 5 search bar example

May 12, 2023 By Aaronn

In this tutorial, we will create search bar in react with ant design 5. We will see ant design 5 search bar with typescript, search bar with icon example with react ant design 5.

install & setup vite + react + typescript + ant design 5


React Ant Design 5 Search Bar Example

1. Create react ant design 5 simple search bar using react-antd Input component.

import { useState } from "react";
import { Input } from "antd";

export default function SearchBar() {
  const [value, setValue] = useState("");

  const onChange = (event) => {
    setValue(event.target.value);
  };

  const onSearch = (value) => {
    console.log(value);
  };

  return (
    <Input.Search
      placeholder="input search text"
      value={value}
      onChange={onChange}
      onSearch={onSearch}
      enterButton
      style={{ width: "300px" }}
    />
  );
}
search bar in react with ant design 5

search bar in react with ant design 5

2. react ant design 5 search bar with icons using typescript.

import React from 'react';
import { AudioOutlined } from '@ant-design/icons';
import { Input, Space } from 'antd';

const { Search } = Input;

const suffix = (
  <AudioOutlined
    style={{
      fontSize: 16,
      color: '#1890ff',
    }}
  />
);

const onSearch = (value: string) => console.log(value);

const App: React.FC = () => (
  <Space direction="vertical">
    <Search placeholder="input search text" onSearch={onSearch} style={{ width: 200 }} />
    <Search placeholder="input search text" allowClear onSearch={onSearch} style={{ width: 200 }} />
    <Search
      addonBefore="https://"
      placeholder="input search text"
      allowClear
      onSearch={onSearch}
      style={{ width: 304 }}
    />
    <Search placeholder="input search text" onSearch={onSearch} enterButton />
    <Search
      placeholder="input search text"
      allowClear
      enterButton="Search"
      size="large"
      onSearch={onSearch}
    />
    <Search
      placeholder="input search text"
      enterButton="Search"
      size="large"
      suffix={suffix}
      onSearch={onSearch}
    />
  </Space>
);

export default App;
react ant design 5 search bar with icons

react ant design 5 search bar with icons

3. react ant design 5 search bar with AutoComplete dropdown.

import  { useState } from "react";
import { AutoComplete, Input } from "antd";

export default function SearchBar() {
  const [value, setValue] = useState("");

  const options = [
    { value: "Option1" },
    { value: "Option2" },
    { value: "Option3" },
  ];

  const onSearch = (searchText) => {
    setValue(searchText);
  };

  const onSelect = (data) => {
    console.log("onSelect", data);
  };

  return (
    <AutoComplete
      options={options}
      style={{
        width: 300,
      }}
      onSelect={onSelect}
      onSearch={onSearch}
    >
      <Input.Search size="large" value={value} enterButton />
    </AutoComplete>
  );
}