Skip to content

Commit 6078bb9

Browse files
committed
prvent setting a value to something different than options
1 parent 6ae94ee commit 6078bb9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export class UUISelectElement extends FormControlMixin(LitElement) {
158158
@state()
159159
private _disabledGroups: string[] = [];
160160

161+
private _values: string[] = [];
162+
161163
@query('#native')
162164
protected _input!: HTMLSelectElement;
163165

@@ -212,9 +214,16 @@ export class UUISelectElement extends FormControlMixin(LitElement) {
212214
willUpdate(changedProperties: Map<string | number | symbol, unknown>) {
213215
if (changedProperties.has('options')) {
214216
this._extractGroups();
217+
this._values = this.options.map(option => option.value);
215218
const selected = this.options.find(option => option.selected);
216219
this.value = selected ? selected.value : '';
217220
}
221+
222+
if (changedProperties.has('value')) {
223+
this.value = this._values.includes(this.value as string)
224+
? this.value
225+
: '';
226+
}
218227
if (changedProperties.has('disabledGroups')) this._createDisabledGroups();
219228
}
220229

packages/uui-select/lib/uui-select.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ describe('UUISelect in Form', () => {
7575
expect(element.value).to.be.equal('orange');
7676
});
7777

78+
it('if value is set to a string that is not in the options array the value is empty string', async () => {
79+
element.value = 'something silly';
80+
await elementUpdated(element);
81+
const formData = new FormData(formElement);
82+
expect(element.value).to.be.equal('');
83+
expect(formData.get('bar')).to.be.equal('');
84+
});
85+
7886
it('form output', () => {
7987
const formData = new FormData(formElement);
8088
expect(formData.get('bar')).to.be.equal('orange');

0 commit comments

Comments
 (0)