From 391c11a3dd609583769fc713f7e13bcfd0bd554d Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 22 May 2025 16:02:37 +0100 Subject: [PATCH 1/2] feat(uui-input): Adds `datalist` options support --- packages/uui-input/lib/uui-input.element.ts | 35 +++++++++++++++++++-- packages/uui-input/lib/uui-input.story.ts | 14 +++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/uui-input/lib/uui-input.element.ts b/packages/uui-input/lib/uui-input.element.ts index b2323877c..05180e8e7 100644 --- a/packages/uui-input/lib/uui-input.element.ts +++ b/packages/uui-input/lib/uui-input.element.ts @@ -34,6 +34,11 @@ export type InputMode = | 'email' | 'url'; +export type InputDataListOption = { + name?: string; + value: string; +}; + /** * Custom element wrapping the native input element.This is a formAssociated element, meaning it can participate in a native HTMLForm. A name:value pair will be submitted. * @element uui-input @@ -164,6 +169,16 @@ export class UUIInputElement extends UUIFormControlMixin( @property() placeholder = ''; + /** + * An array of options to be rendered by the input's datalist. The option interface has 2 properties: + * `interface Option { + name: string; + value: string; + }` + */ + @property({ type: Array, attribute: false }) + options: Array = []; + /** * Defines the input autocomplete. * @type {string} @@ -324,11 +339,26 @@ export class UUIInputElement extends UUIFormControlMixin( return html``; } + protected renderDataList() { + if (!this.options?.length) return; + return html` + + ${this.options.map( + option => html` + + `, + )} + + `; + } + render() { return html` ${this.renderPrepend()} ${this.autoWidth ? this.renderInputWithAutoWidth() : this.renderInput()} - ${this.renderAppend()} + ${this.renderAppend()} ${this.renderDataList()} `; } @@ -351,6 +381,7 @@ export class UUIInputElement extends UUIFormControlMixin( spellcheck=${this.spellcheck} autocomplete=${ifDefined(this.autocomplete as any)} placeholder=${ifDefined(this.placeholder)} + list=${ifDefined(this.options?.length ? 'options' : undefined)} aria-label=${ifDefined(this.label)} inputmode=${ifDefined(this.inputMode)} ?disabled=${this.disabled} @@ -363,7 +394,7 @@ export class UUIInputElement extends UUIFormControlMixin( } private renderAutoWidthBackground() { - return html` `; + return html``; } private renderText() { diff --git a/packages/uui-input/lib/uui-input.story.ts b/packages/uui-input/lib/uui-input.story.ts index 85c4045ad..be98b6b08 100644 --- a/packages/uui-input/lib/uui-input.story.ts +++ b/packages/uui-input/lib/uui-input.story.ts @@ -209,3 +209,17 @@ export const AutoWidth: Story = { placeholder: 'Start typing...', }, }; + +export const DataList: Story = { + args: { + autocomplete: 'off', + options: [ + { name: 'Carrot', value: 'orange' }, + { name: 'Cucumber', value: 'green' }, + { name: 'Aubergine', value: 'purple' }, + { name: 'Blueberry', value: 'Blue' }, + { name: 'Banana', value: 'yellow' }, + { name: 'Strawberry', value: 'red' }, + ], + }, +}; From 8412c6cb951b246af81017aa05cb89c46dd1185e Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 22 May 2025 16:28:57 +0100 Subject: [PATCH 2/2] Updates story --- packages/uui-input/lib/uui-input.story.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/uui-input/lib/uui-input.story.ts b/packages/uui-input/lib/uui-input.story.ts index be98b6b08..6d4d49e42 100644 --- a/packages/uui-input/lib/uui-input.story.ts +++ b/packages/uui-input/lib/uui-input.story.ts @@ -222,4 +222,25 @@ export const DataList: Story = { { name: 'Strawberry', value: 'red' }, ], }, + parameters: { + docs: { + source: { + format: false, + language: 'jsx', + code: ` +// this is an example of array you need to pass to the input component +const options: Array