Search Input

Jump to Props

Provides a TextInput with preconfigured adornments and a loading state.

Import

import { SearchInput } from "@vitality-ds/components";

Usage

SearchInput under the hood is a TextInput (Read more about TextInput here) with preset adornments. Both the startAdorn and the endAdorn are handled by the SearchInput and a loading state is provided. The purpose of this component is to perform asynchronous functions on external components based on the value of the TextInput and the process is communicated to the user through the isLoading loading state.

<SearchInput
  value={undefined}
  onChange={(event) => console.log(event.target.value)}
  id="search"
  name="search"
/>

isLoading

The key difference between SearchInput and TextInput is the loading state, which can be indicated by simply passing the isLoading boolean to the component.

<SearchInput
  isLoading
  value={undefined}
  onChange={(event) => console.log(event.target.value)}
  id="search"
  name="search"
/>

icon

SearchInput renders a Search icon by default, which can be customized using the icon prop to any valid Vitality Icon. To remove the icon, pass null to the icon prop.

() => {
  const requiredProps = {
    value: undefined,
    name: "name",
    id: "id",
    onChange: () => {},
  };

  return (
    <Stack spacing="md">
      <SearchInput {...requiredProps} />
      <SearchInput icon={Hospital} {...requiredProps} />
      <SearchInput icon={null} {...requiredProps} />
    </Stack>
  );
};

A functional example

This demo shows the SearchInput in action:

() => {
  const [value, setValue] = React.useState();
  const [isLoading, setIsLoading] = React.useState(false);

  function fetchPatients(filter) {
    const patientNames = [
      { name: "John Campbell" },
      { name: "Jonathan Cash" },
      { name: "John Hawthorne" },
      { name: "John Smith" },
      { name: "John Wilson" },
      { name: "Johnny Wilson" },
    ];
    if (typeof filter === "string") {
      return patientNames.filter((patient) =>
        patient.name.toLowerCase().includes(filter.toLowerCase())
      );
    }
    return patientNames;
  }

  const [list, setList] = React.useState(fetchPatients(""));

  function changeHandler(event) {
    setValue(event.target.value);
    setIsLoading(true);
  }

  React.useEffect(() => {
    const timeoutId = setTimeout(() => {
      const patients = fetchPatients(value);
      setList(patients);
      setIsLoading(false);
    }, 2000);
    return () => clearTimeout(timeoutId);
  }, [value]);

  return (
    <div style={{ width: "180px" }}>
      <Stack>
        <SearchInput
          value={value}
          isLoading={isLoading}
          onChange={(event) => changeHandler(event)}
          id="search"
          name="search"
        />
        {!isLoading && list.map((item, key) => <p key={key}>{item.name}</p>)}
      </Stack>
    </div>
  );
};

Props

disabled

Description

Determines whether the user has the ability to interact with the input.

Type

boolean

Default Value

false

hasError

Description

Adds additional styling to indicate failed validation.

Type

boolean

Default Value

false

icon

Description

Replaces the default icon with another.

Type

FC<IconType>

id

Description

The id of the input. Used to connect label to input.

Type

string

isLoading

Description

Determines if the Search input is currently loading. This adds a a spinner adornment to the end of the input.

Type

boolean

maskProps

Description

Mask Props

Type

MaskProps

name

Description

The name of the text input. Submitted with its owning form as part of a name/value pair.

Type

string

onChange

Description

The callback function. Triggered when any character is added or removed from the input.

Type

ChangeEventHandler<HTMLInputElement> & ((e: ChangeEvent<HTMLInputElement>) => void)

placeholder

Description

Shows an example of what could be inputted. Only visible when no value is present. Commonly used as a "pseudo" helper message.

Type

string

textAlign

Description

aligns the Text of the input

Type

("left" | "right"

type

Description

A string specifying the type of input element type to render. If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.

Type

HTMLInputTypeAttribute

value

Description

The inputted value. Can be used to prepopulate data.

Type

(string | number | readonly string[]) & string

© 2025