Skip to content

feat(uui-input): Adds <datalist> options support #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions packages/uui-input/lib/uui-input.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<InputDataListOption> = [];

/**
* Defines the input autocomplete.
* @type {string}
Expand Down Expand Up @@ -324,11 +339,26 @@ export class UUIInputElement extends UUIFormControlMixin(
return html`<slot name="append"></slot>`;
}

protected renderDataList() {
if (!this.options?.length) return;
return html`
<datalist id="options">
${this.options.map(
option => html`
<option value=${option.value}>
${option.name ?? option.value}
</option>
`,
)}
</datalist>
`;
}

render() {
return html`
${this.renderPrepend()}
${this.autoWidth ? this.renderInputWithAutoWidth() : this.renderInput()}
${this.renderAppend()}
${this.renderAppend()} ${this.renderDataList()}
`;
}

Expand All @@ -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}
Expand All @@ -363,7 +394,7 @@ export class UUIInputElement extends UUIFormControlMixin(
}

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

private renderText() {
Expand Down
35 changes: 35 additions & 0 deletions packages/uui-input/lib/uui-input.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,38 @@ 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' },
],
},
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<Option> = [
{ name: 'Carrot', value: 'orange' },
{ name: 'Cucumber', value: 'green' },
{ name: 'Aubergine', value: 'purple' },
{ name: 'Blueberry', value: 'Blue' },
{ name: 'Banana', value: 'yellow' },
{ name: 'Strawberry', value: 'red' },
];

<uui-input .options=\${options}></uui-input>
`,
},
},
},
};
Loading