react-govuk-autocomplete
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

GOV.UK React Auto Complete User Guide

The GOV.UK Design System is an official design framework produced by the UK Government and defines best practices and rules that must be followed to ensure that your code base is GDS Compliant.

You can find the guide at the GOV.UK Design System website.

Overview

The standard implementation for the GDS Auto Complete does not support large datasets or the dynamic creation of selectable items.

The remit was to develop an accessible react based auto complete component that would both allow large datasets and the ability to dynamically select an option that was not in the list.

Rather than build the whole component from scratch, two libraries have been utilised. The actual select control uses the React Select component which has excellent accessibility support and the ability to create a select control that supports the dynamic creation of items.

The support for large datasets was implemented by overriding the MenuList component of the react select control with a virtual list implementation utilising the Tanstack Virtual library.

The resulting component is shown below:

Auto Complete!

A working (Demo) can be found under the auto complete menu option, showing two variants, one with a simple list and the second with a large dataset creatable item list.

Configuration

The below shows one of our example implementations of the auto complete integrated with React Hook Form:

<Controller
  control={control}
  name="model"
  render={({ field: { value, onChange }, fieldState: { error } }) => (
    <AutoComplete
      identifier="model"
      label="Model"
      labelClassExt="govuk-label-s"
      containerClassExt="govuk-input--width-20"
      hint="Large dataset and create"
      allowCreate={true}
      useUpperCase={true}
      options={vehicleModels}
      value={vehicleModels.find(x => x.value === value)}
      getOptionLabel={x => x.label}
      onChange={x => onChange(x?.value)}
      error={error?.message}
    />
  )}
/>

Installation

The package can be installed via npm:

npm install react-govuk-autocomplete --save

Or via yarn:

yarn add react-govuk-autocomplete

The auto complete uses GDS styles which can be installed via the govuk-frontend package:

npm install govuk-frontend --save
yarn add govuk-frontend

License

Copyright (c) 2024 Barry Jacobs and individual contributors. Licensed under MIT license, see LICENSE for the full license.

Package Sidebar

Install

npm i react-govuk-autocomplete

Weekly Downloads

7

Version

1.0.6

License

MIT

Unpacked Size

881 kB

Total Files

10

Last publish

Collaborators

  • barry.jacobs