Anchor Design System
Form

Typeahead2.0

Typeahead description

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.

Installation

npm i @tryg/ui-library

Usage

Individual only imports the desired component, and only needs to be included once within the application. Which is beneficial to keep the application size small.

Webcomponent
<script type="module">
  import { defineCustomElement } from '@tryg/ui-library/dist/components/anchor-typeahead';
  defineCustomElement();

  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead>Hello</anchor-typeahead>

Global only needs to be defined once in the application, but it will import all components even the ones that are not in use..

Webcomponent
<script type="module">
  import { defineCustomElements } from '@tryg/ui-library/dist/loader';
  defineCustomElements();

  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead>Hello</anchor-typeahead>

Disabled

Webcomponent
<script type="module">
  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead disabled>Hello</anchor-typeahead>

Read Only

Webcomponent
<script type="module">
  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead readonly>Hello</anchor-typeahead>

Required

If you pass the required property to the input, the input will be required.

Webcomponent
<script type="module">
  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead required>Hello</anchor-typeahead>

Help text

You can add a helpful text to the input by passing the help-text property.

Webcomponent
<script type="module">
  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead help-text="This is a help text">Hello</anchor-typeahead>

With Error Message

You can combine the error-prop properties to show an invalid input.

Webcomponent
<script type="module">
  const options = ['foo', 'bar', 'foobar', 'foofoo', 'barbar', 'foofoobarbar', 'foobarfoobar']
  document.querySelector('anchor-typeahead').setAttribute('options', JSON.stringify(options));
</script>
<anchor-typeahead error-prop="This is a error message">Hello</anchor-typeahead>

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

PropertyAttributeDescriptionTypeDefault
disableddisabledValue indicating whether or not this typeahead should be disabledbooleanundefined
errorProperror-propShow an error messagestring''
filterfilterThe type of filter to use"contains" | "containsWords" | "custom" | "startsWith"'containsWords'
inputIdinput-idThe id of the input fieldstring`anchor-typeahead-${uuidv4()}`
labellabelLabel for the input fieldstringundefined
languagelanguageThe display language for the component."DK" | "NO"'NO'
limitNumberOfVisibleOptionslimit-number-of-visible-optionsWhen this is true, the component will have a maximum number of options to show, determined by the numberOfOptionsToDisplay prop. Default value is false.booleanfalse
messagePlacementmessage-placementThis 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
numberOfOptionsToDisplaynumber-of-options-to-displayThe maximum number of options to display when limitNumberOfVisibleOptions is true. Default value is 100.number100
optionsoptionsArray of options to choose from, a JSON serialized string or an array of OptionValueOptionValue[] | stringundefined
placeholderplaceholderPlaceholder for the input fieldstringundefined
readonlyreadonlySet the field to be read onlybooleanfalse
requiredrequiredMark the field as requiredbooleanfalse
showAllOptionsshow-all-optionsThis prop decides if the component should show all available options before the user has typed a search phrase.booleantrue
textstextsOverride some, or all, texts used internally in the componentstringJSON.stringify({})
uuiduuidThe id of the input fieldstringuuidv4()
validityMissingValueTextvalidity-missing-value-textText to be displayed if when value is missing an native form validation is usedstringundefined
valuevalueThe value of the input fieldstring''
whitespaceCharacters--Array of strings which should be treated as whitespace to make it easier to match resultsstring[]undefined

Events

EventDescriptionType
collapseEvent emitted when the dropdown select collapsesCustomEvent<any>
expandEvent emitted when the dropdown select expandsCustomEvent<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.2023CustomEvent<any>
selectedEvent emitted when a valid option has been selectedCustomEvent<any>

CSS Custom Properties

NameDescription
--typeahead-border-colorSet the border color of the
--typeahead-border-color-disabledSet the disabled border color of the
--typeahead-border-color-errorSet the error border color of the
--typeahead-border-color-focusSet the focus border color of the
--typeahead-border-color-hoverSet the hover border color of the
--typeahead-border-color-readonlySet the readonly border color of the
--typeahead-border-color-warningSet the warning border color of the
--typeahead-border-radiusSet the border radius of the
--typeahead-border-widthSet the border width of the
--typeahead-font-weight-labelSet the font weight of the label
--typeahead-icon-colorSet the icon color of the icon
--typeahead-icon-color-disabledSet the icon color of the icon when disabled
--typeahead-max-widthSet the max width of the
--typeahead-outlineSet the outline of the
--typeahead-widthSet the width of the
--typeahead-z-indexSet the z index of the

Dependencies

Depends on

Graph


Built with StencilJS

On this page