Skip to content

Commit 391c11a

Browse files
committed
feat(uui-input): Adds datalist options support
1 parent def7b1b commit 391c11a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/uui-input/lib/uui-input.element.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export type InputMode =
3434
| 'email'
3535
| 'url';
3636

37+
export type InputDataListOption = {
38+
name?: string;
39+
value: string;
40+
};
41+
3742
/**
3843
* 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.
3944
* @element uui-input
@@ -164,6 +169,16 @@ export class UUIInputElement extends UUIFormControlMixin(
164169
@property()
165170
placeholder = '';
166171

172+
/**
173+
* An array of options to be rendered by the input's datalist. The option interface has 2 properties:
174+
* `interface Option {
175+
name: string;
176+
value: string;
177+
}`
178+
*/
179+
@property({ type: Array, attribute: false })
180+
options: Array<InputDataListOption> = [];
181+
167182
/**
168183
* Defines the input autocomplete.
169184
* @type {string}
@@ -324,11 +339,26 @@ export class UUIInputElement extends UUIFormControlMixin(
324339
return html`<slot name="append"></slot>`;
325340
}
326341

342+
protected renderDataList() {
343+
if (!this.options?.length) return;
344+
return html`
345+
<datalist id="options">
346+
${this.options.map(
347+
option => html`
348+
<option value=${option.value}>
349+
${option.name ?? option.value}
350+
</option>
351+
`,
352+
)}
353+
</datalist>
354+
`;
355+
}
356+
327357
render() {
328358
return html`
329359
${this.renderPrepend()}
330360
${this.autoWidth ? this.renderInputWithAutoWidth() : this.renderInput()}
331-
${this.renderAppend()}
361+
${this.renderAppend()} ${this.renderDataList()}
332362
`;
333363
}
334364

@@ -351,6 +381,7 @@ export class UUIInputElement extends UUIFormControlMixin(
351381
spellcheck=${this.spellcheck}
352382
autocomplete=${ifDefined(this.autocomplete as any)}
353383
placeholder=${ifDefined(this.placeholder)}
384+
list=${ifDefined(this.options?.length ? 'options' : undefined)}
354385
aria-label=${ifDefined(this.label)}
355386
inputmode=${ifDefined(this.inputMode)}
356387
?disabled=${this.disabled}
@@ -363,7 +394,7 @@ export class UUIInputElement extends UUIFormControlMixin(
363394
}
364395

365396
private renderAutoWidthBackground() {
366-
return html` <div id="auto" aria-hidden="true">${this.renderText()}</div>`;
397+
return html`<div id="auto" aria-hidden="true">${this.renderText()}</div>`;
367398
}
368399

369400
private renderText() {

packages/uui-input/lib/uui-input.story.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,17 @@ export const AutoWidth: Story = {
209209
placeholder: 'Start typing...',
210210
},
211211
};
212+
213+
export const DataList: Story = {
214+
args: {
215+
autocomplete: 'off',
216+
options: [
217+
{ name: 'Carrot', value: 'orange' },
218+
{ name: 'Cucumber', value: 'green' },
219+
{ name: 'Aubergine', value: 'purple' },
220+
{ name: 'Blueberry', value: 'Blue' },
221+
{ name: 'Banana', value: 'yellow' },
222+
{ name: 'Strawberry', value: 'red' },
223+
],
224+
},
225+
};

0 commit comments

Comments
 (0)