Typeahead2.0
WORK IN PROGRESS: This component has not gone through final testing and a11y validation. It may see breaking changes in the near future based on the feedback from testing.
Ads by Tryg Careers
Installation
npm i @tryg/vue-ui-libraryUsage
Install the component library as a Vue plugin. This globally registers all Anchor components.
import { createApp } from 'vue';
import { ComponentLibrary } from '@tryg/vue-ui-library';
import App from './App.vue';
const app = createApp(App);
app.use(ComponentLibrary);
app.mount('#app');<template>
<anchor-typeahead label="Hello"></anchor-typeahead>
</template>Import only the components you need for optimal bundle size.
<script setup>
import { AnchorTypeahead } from '@tryg/vue-ui-library';
</script>
<template>
<anchor-typeahead label="Hello"></anchor-typeahead>
</template>Disabled
<template>
<anchor-typeahead disabled label="Hello"></anchor-typeahead>
</template>Read Only
<template>
<anchor-typeahead readonly label="Hello"></anchor-typeahead>
</template>Required
If you pass the required property to the input, the input will be required.
<template>
<anchor-typeahead required label="Hello"></anchor-typeahead>
</template>Help text
You can add a helpful text to the input by passing the help-text property.
<template>
<anchor-typeahead help-text="This is a help text" label="Hello"></anchor-typeahead>
</template>With Error Message
You can combine the error-prop properties to show an invalid input.
<template>
<anchor-typeahead error-prop="This is an error message" label="Hello"></anchor-typeahead>
</template>Accessibility
- Uses
role="combobox"witharia-autocomplete="list"to communicate the autocomplete behavior to assistive technology. - The dropdown list uses
role="listbox"with individual options marked asrole="option", following the ARIA combobox pattern. - Arrow key navigation moves focus through the available options, with
aria-activedescendanttracking the currently focused option. - Pressing Enter selects the focused option, collapsing the dropdown and updating the input value.
- The
aria-expandedattribute reflects whether the dropdown is open or closed, andaria-controlslinks the input to its listbox.
API
anchor-typeahead
WORK IN PROGRESS: This component has not gone through final testing and a11y validation. It may see breaking changes in the near future based on the feedback from testing.
How to use the typeahead component
Basic typeahead
<anchor-typeahead options='["foo","bar","foobar"]' language="NO" texts='{"noResult":"The search yeilded no results"}'></anchor-typeahead>Properties
| Property | Attribute | Description | Type | Default |
|---|---|---|---|---|
disabled | disabled | Value indicating whether or not this typeahead should be disabled | boolean | undefined |
errorProp | error-prop | Show an error message | string | '' |
filter | filter | The type of filter to use | "contains" | "containsWords" | "custom" | "startsWith" | 'containsWords' |
inputId | input-id | The id of the input field | string | `anchor-typeahead-${uuidv4()}` |
label | label | Label for the input field | string | undefined |
language | language | The display language for the component. | "DK" | "NO" | 'NO' |
limitNumberOfVisibleOptions | limit-number-of-visible-options | When this is true, the component will have a maximum number of options to show, determined by the numberOfOptionsToDisplay prop. Default value is false. | boolean | false |
messagePlacement | message-placement | This prop decides if the messages should render between the label of an input and the input field, or below the input field. | "below" | "inside" | undefined |
numberOfOptionsToDisplay | number-of-options-to-display | The maximum number of options to display when limitNumberOfVisibleOptions is true. Default value is 100. | number | 100 |
options | options | Array of options to choose from, a JSON serialized string or an array of OptionValue | OptionValue[] | string | undefined |
placeholder | placeholder | Placeholder for the input field | string | undefined |
readonly | readonly | Set the field to be read only | boolean | false |
required | required | Mark the field as required | boolean | false |
showAllOptions | show-all-options | This prop decides if the component should show all available options before the user has typed a search phrase. | boolean | true |
texts | texts | Override some, or all, texts used internally in the component | string | JSON.stringify({}) |
uuid | uuid | The id of the input field | string | uuidv4() |
validityMissingValueText | validity-missing-value-text | Text to be displayed if when value is missing an native form validation is used | string | undefined |
value | value | The value of the input field | string | '' |
whitespaceCharacters | -- | Array of strings which should be treated as whitespace to make it easier to match results | string[] | undefined |
Events
| Event | Description | Type |
|---|---|---|
collapse | Event emitted when the dropdown select collapses | CustomEvent<any> |
expand | Event emitted when the dropdown select expands | CustomEvent<any> |
inputChange | [DEPRECATED] this event has been replaced by the selected event. inputChange is emitted by the underlying anchor-input. Will be removed 20.12.2023 | CustomEvent<any> |
selected | Event emitted when a valid option has been selected | CustomEvent<any> |
CSS Custom Properties
| Name | Description |
|---|---|
--typeahead-border-color | Set the border color of the |
--typeahead-border-color-disabled | Set the disabled border color of the |
--typeahead-border-color-error | Set the error border color of the |
--typeahead-border-color-focus | Set the focus border color of the |
--typeahead-border-color-hover | Set the hover border color of the |
--typeahead-border-color-readonly | Set the readonly border color of the |
--typeahead-border-color-warning | Set the warning border color of the |
--typeahead-border-radius | Set the border radius of the |
--typeahead-border-width | Set the border width of the |
--typeahead-font-weight-label | Set the font weight of the |
--typeahead-icon-color | Set the icon color of the |
--typeahead-icon-color-disabled | Set the icon color of the |
--typeahead-max-width | Set the max width of the |
--typeahead-outline | Set the outline of the |
--typeahead-width | Set the width of the |
--typeahead-z-index | Set the z index of the |
Dependencies
Depends on
Graph
graph TD;
anchor-typeahead --> anchor-input
anchor-typeahead --> anchor-option
anchor-input --> anchor-form-field
anchor-input --> anchor-icon
anchor-input --> anchor-tooltip
anchor-form-field --> anchor-icon
anchor-option --> anchor-icon
style anchor-typeahead fill:#f9f,stroke:#333,stroke-width:4pxBuilt with StencilJS
