7.4.0 • Published 6 months ago

@purpurds/autocomplete v7.4.0

Weekly downloads
-
License
AGPL-3.0-only
Repository
-
Last release
6 months ago

import { Meta, Stories, ArgTypes, Primary, Subtitle } from "@storybook/blocks";

import * as AutocompleteStories from "./src/autocomplete.stories"; import packageInfo from "./package.json";

Autocomplete

Version {packageInfo.version}

Showcase

Properties

Installation

Via NPM

Add the dependency to your consumer app like "@purpurds/purpur": "^x.y.z"

In MyApp.tsx

import "@purpurds/purpur/styles";

Examples

In MyComponent.tsx

With uncontrolled input (using Purpur TextField)

import { Autocomplete, Option, TextField } from "@purpurds/purpur";
import { useState } from "React";

export const MyComponent = () => {
  const options: Options[] = [
    /* Your options */
  ];
  const [selectedOption, setSelectedOption] = useState<Option>();

  return (
    <Autocomplete
      id="autocomplete"
      listboxLabel="Label for listbox, for a11y (describe the options)"
      selectedOption={selectedOption}
      onSelect={selectedOption}
      options={options}
      selectedOption
      renderInput={(inputProps) => (
        <TextField
          {...inputProps}
          label="Text Field"
          id="autocomplete-input"
          type="text"
          placeholder="Write something"
        />
      )}
    />
  );
};

With controlled input (using Purpur SearchField)

import { Autocomplete, Option, SearchField } from "@purpurds/purpur";
import { useState } from "React";

export const MyComponent = () => {
  const options: Options[] = [
    /* Your options */
  ];
  const [selectedOption, setSelectedOption] = useState<Option>();
  const [inputValue, setInputValue] = useState("");

  return (
    <Autocomplete
      id="autocomplete"
      listboxLabel="Label for listbox, for a11y (describe the options)"
      selectedOption={selectedOption}
      onSelect={selectedOption}
      options={options}
      selectedOption
      onInputChange={setInputValue}
      inputValue={inputValue}
      renderInput={(inputProps) => (
        <SearchField
          {...inputProps}
          label="Text Field"
          id="autocomplete-input"
          type="text"
          placeholder="Write something"
          onClear={() => setInputValue("")}
          clearButtonAllyLabel="Rensa sökfältet"
          variant="button-attached"
          iconOnlySearchButton
          searchButtonLabel="Search"
        />
      )}
    />
  );
};

With controlled input & with renderOption function (using Purpur TextField)

Highlight parts of the option labels that are matching the input value

import { Autocomplete, Option, Paragraph, TextField } from "@purpurds/purpur";
import { useState } from "React";

export const MyComponent = () => {
  const options: Options[] = [
    /* Your options */
  ];
  const [inputValue, setInputValue] = useState("");
  const [selectedOption, setSelectedOption] = useState<Option>();

  function renderOption(option: Option) {
    const optionText = option.label;
    if (!inputValue.trim()) {
      return optionText;
    }

    const escapeRegExp = (str = "") => str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
    const regex = new RegExp(`(${escapeRegExp(inputValue)})`, "gi");
    const parts = optionText.split(regex);

    return (
      <Paragraph>
        {parts
          .filter((part) => part)
          .map((part, i) =>
            regex.test(part) ? <strong key={i}>{part}</strong> : <span key={i}>{part}</span>
          )}
      </Paragraph>
    );
  }

  return (
    <Autocomplete
      id="autocomplete"
      listboxLabel="Label for listbox, for a11y (describe the options)"
      selectedOption={selectedOption}
      onSelect={setSelectedOption}
      onInputChange={setInputValue}
      inputValue={inputValue}
      renderOption={renderOption}
      options={options}
      renderInput={(inputProps) => (
        <TextField {...inputProps} label="With renderOption" id="autocomplete-input" type="text" />
      )}
    />
  );
};
6.1.0

10 months ago

5.27.5

1 year ago

5.27.4

1 year ago

6.1.2

10 months ago

5.27.3

1 year ago

6.1.1

10 months ago

5.27.2

1 year ago

6.5.0

9 months ago

6.1.4

10 months ago

5.27.1

1 year ago

6.1.3

10 months ago

5.27.0

1 year ago

5.34.2

10 months ago

5.34.3

10 months ago

5.34.0

10 months ago

5.34.1

10 months ago

5.30.1

11 months ago

5.30.0

11 months ago

7.3.1

7 months ago

7.3.0

7 months ago

6.6.1

9 months ago

6.6.0

9 months ago

6.6.2

9 months ago

5.28.3

1 year ago

5.28.2

1 year ago

6.2.0

9 months ago

5.28.1

1 year ago

5.28.0

1 year ago

5.24.3

1 year ago

5.35.0

10 months ago

5.31.0

11 months ago

7.0.0

7 months ago

6.12.5

7 months ago

7.4.0

6 months ago

6.12.0

8 months ago

6.12.4

7 months ago

6.12.3

7 months ago

6.12.2

7 months ago

6.12.1

7 months ago

6.7.0

8 months ago

5.24.2

1 year ago

6.7.2

8 months ago

6.7.1

8 months ago

6.3.0

9 months ago

5.29.0

11 months ago

6.3.1

9 months ago

5.32.0

11 months ago

7.1.0

7 months ago

6.11.1

8 months ago

6.11.0

8 months ago

5.25.0

1 year ago

6.8.1

8 months ago

6.8.0

8 months ago

6.8.3

8 months ago

6.8.2

8 months ago

6.0.1

10 months ago

6.0.0

10 months ago

6.0.2

10 months ago

6.4.0

9 months ago

5.33.0

11 months ago

7.2.0

7 months ago

6.10.0

8 months ago

5.34.4

10 months ago

5.26.0

1 year ago

6.9.0

8 months ago

5.25.1-0

1 year ago

5.24.1

1 year ago

5.24.0

1 year ago

5.23.0

1 year ago

5.22.0

1 year ago

5.21.1

1 year ago

5.21.0

1 year ago

5.18.3

1 year ago

5.19.1

1 year ago

5.18.2

1 year ago

5.19.0

1 year ago

5.18.1

1 year ago

5.18.0

1 year ago

5.17.0

1 year ago

5.20.1

1 year ago

5.20.0

1 year ago

5.16.1

1 year ago

5.16.0

1 year ago

5.15.1

1 year ago

5.15.0

1 year ago

5.14.0

1 year ago

5.6.0

2 years ago

5.4.2

2 years ago

5.5.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.3.0

2 years ago

5.1.2

2 years ago

5.2.0

2 years ago

5.13.0

1 year ago

5.11.2

1 year ago

5.12.0

1 year ago

5.11.1

1 year ago

5.11.0

1 year ago

5.10.1

1 year ago

5.10.0

1 year ago

5.8.2

1 year ago

5.9.0

1 year ago

5.8.1

1 year ago

5.8.0

1 year ago

5.7.0

2 years ago

5.4.3

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

4.6.0

2 years ago

0.0.1

2 years ago