Skip to content

Feature upgrade: UUI-select does not reflect value #1141

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 3 additions & 21 deletions packages/uui-select/lib/uui-select.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ declare global {
name: string;
value: string;
group?: string;
selected?: boolean;
disabled?: boolean;
}
}
Expand Down Expand Up @@ -87,7 +86,6 @@ export class UUISelectElement extends UUIFormControlMixin(LitElement, '') {
name: string;
value: string;
group?: string;
selected?: boolean;
disabled?: boolean;
}`
*/
Expand Down Expand Up @@ -173,8 +171,6 @@ export class UUISelectElement extends UUIFormControlMixin(LitElement, '') {
if (changedProperties.has('options')) {
this._extractGroups();
this._values = this.options.map(option => option.value);
const selected = this.options.find(option => option.selected);
this.value = selected ? selected.value : '';
}

if (changedProperties.has('value')) {
Expand All @@ -200,13 +196,9 @@ export class UUISelectElement extends UUIFormControlMixin(LitElement, '') {
private _renderOption(
name: string,
value: string,
selected: boolean | undefined,
disabled: boolean | undefined,
) {
return html`<option
value="${value}"
?selected=${selected}
?disabled=${disabled}>
return html`<option value="${value}" ?disabled=${disabled}>
${name}
</option>`;
}
Expand All @@ -224,12 +216,7 @@ export class UUISelectElement extends UUIFormControlMixin(LitElement, '') {
)}>
${this.options.map(option =>
option.group === group
? this._renderOption(
option.name,
option.value,
option.selected,
option.disabled,
)
? this._renderOption(option.name, option.value, option.disabled)
: '',
)}
</optgroup>`,
Expand Down Expand Up @@ -259,12 +246,7 @@ export class UUISelectElement extends UUIFormControlMixin(LitElement, '') {
${this.options
.filter(option => !option.group)
.map(option =>
this._renderOption(
option.name,
option.value,
option.selected,
option.disabled,
),
this._renderOption(option.name, option.value, option.disabled),
)}
</select>`;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/uui-select/lib/uui-select.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ const options: Array<Option> = [
{ name: 'Strawberry', value: 'red' },
];

const preselectedOptions: Array<Option> = options.map(option => {
if (option.name === 'Aubergine') return { ...option, selected: true };
return option;
});
const preselectedValue: string = 'purple';

const groupedOptions: Array<Option> = options.map(option => {
if (options.indexOf(option) <= 2) return { ...option, group: 'Vegetables' };
Expand All @@ -70,8 +67,9 @@ export const Default: Story = {

export const Preselected: Story = {
args: {
options: preselectedOptions,
options: options,
label: 'Preselected',
value: preselectedValue,
},
parameters: {
controls: { include: ['placeholder'] },
Expand All @@ -86,7 +84,7 @@ export const Preselected: Story = {
const options: Array<Option> = [
{ name: 'Carrot', value: 'orange' },
{ name: 'Cucumber', value: 'green' },
{ name: 'Aubergine', value: 'purple', selected: true },
{ name: 'Aubergine', value: 'purple' },
{ name: 'Blueberry', value: 'Blue' },
{ name: 'Banana', value: 'yellow' },
{ name: 'Strawberry', value: 'red' },
Expand Down Expand Up @@ -161,7 +159,8 @@ export const Readonly: Story = {
args: {
readonly: true,
label: 'Label',
options: preselectedOptions,
options: options,
value: preselectedValue,
},
parameters: {
docs: {
Expand Down
12 changes: 10 additions & 2 deletions packages/uui-select/lib/uui-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { elementUpdated, expect, fixture, html } from '@open-wc/testing';
import { UUISelectElement } from './uui-select.element';

const options: Array<Option> = [
{ name: 'Carrot', value: 'orange', selected: true },
{ name: 'Carrot', value: 'orange' },
{ name: 'Cucumber', value: 'green' },
{ name: 'Aubergine', value: 'purple' },
{ name: 'Blueberry', value: 'Blue' },
{ name: 'Banana', value: 'yellow' },
{ name: 'Strawberry', value: 'red' },
];

const selectdVaue: string = 'orange';

describe('UUISelectElement', () => {
let element: UUISelectElement;
let input: HTMLSelectElement | null | undefined;
Expand All @@ -20,6 +22,7 @@ describe('UUISelectElement', () => {
html`<uui-select
label="foo"
name="bar"
.value="orange"
.options=${options}></uui-select>`,
);
input = element.shadowRoot?.querySelector('#native');
Expand Down Expand Up @@ -67,7 +70,11 @@ describe('UUISelect in Form', () => {
beforeEach(async () => {
formElement = await fixture(
html` <form>
<uui-select label="foo" name="bar" .options=${options}></uui-select>
<uui-select
label="foo"
name="bar"
.value=${selectdVaue}
.options=${options}></uui-select>
</form>`,
);
element = formElement.querySelector('uui-select') as any;
Expand Down Expand Up @@ -112,6 +119,7 @@ describe('UUISelect in Form', () => {
<uui-select
label="test label"
name="test"
.value="orange"
.options=${options}></uui-select>
</form>`,
);
Expand Down
Loading