Skip to content

Commit 305aa05

Browse files
committed
SingleSelect: Fix prop/state synchronization
Refs: #7557
1 parent 6943f75 commit 305aa05

File tree

1 file changed

+10
-6
lines changed
  • packages/components/src/components/single-select

1 file changed

+10
-6
lines changed

packages/components/src/components/single-select/shadow.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class KolSingleSelect implements SingleSelectAPI {
5050
@Method()
5151
// eslint-disable-next-line @typescript-eslint/require-await
5252
public async getValue(): Promise<StencilUnknown | undefined> {
53-
return this.state._value;
53+
return this._value;
5454
}
5555

5656
@Method()
@@ -80,9 +80,14 @@ export class KolSingleSelect implements SingleSelectAPI {
8080
}
8181
};
8282

83+
/**
84+
* If there are options and the current input value doesn't match any option's label,
85+
* resets the input value to match the label of the currently selected option.
86+
* Closes the dropdown and resets the opened state.
87+
*/
8388
private onBlur() {
8489
if (Array.isArray(this.state._options) && this.state._options.length > 0 && !this.state._options.some((option) => option.label === this._inputValue)) {
85-
this._inputValue = this.state._options.find((option) => (option as Option<string>).value === this.state._value)?.label as string;
90+
this._inputValue = this.state._options.find((option) => (option as Option<string>).value === this._value)?.label as string;
8691
this._filteredOptions = [...this.state._options];
8792
}
8893
this._isOpen = false;
@@ -110,7 +115,7 @@ export class KolSingleSelect implements SingleSelectAPI {
110115
this.controller.onFacade.onChange(new CustomEvent('change', { bubbles: true, detail: { name: this.state._name, value: option.value } }), option.value);
111116
this._filteredOptions = [...this.state._options];
112117

113-
this.controller.setFormAssociatedValue(this.state._value);
118+
this.controller.setFormAssociatedValue(this._value);
114119
}
115120

116121
private onInput(event: Event) {
@@ -281,7 +286,7 @@ export class KolSingleSelect implements SingleSelectAPI {
281286
}}
282287
tabIndex={-1}
283288
role="option"
284-
aria-selected={this.state._value === (option as Option<string>).value ? 'true' : undefined}
289+
aria-selected={this._value === (option as Option<string>).value ? 'true' : undefined}
285290
onClick={(event: Event) => {
286291
this.selectOption(option as Option<string>);
287292
this.refInput?.focus();
@@ -313,7 +318,7 @@ export class KolSingleSelect implements SingleSelectAPI {
313318
name="options"
314319
id={`option-radio-${index}`}
315320
value={(option as Option<string>).value}
316-
checked={this.state._value === (option as Option<string>).value || index === this._focusedOptionIndex}
321+
checked={this._value === (option as Option<string>).value || index === this._focusedOptionIndex}
317322
/>
318323

319324
<label htmlFor={`option-radio-${index}`} class="radio-label">
@@ -571,7 +576,6 @@ export class KolSingleSelect implements SingleSelectAPI {
571576
_id: `id-${nonce()}`,
572577
_label: '', // ⚠ required
573578
_options: [],
574-
_value: '',
575579
_hideClearButton: false,
576580
};
577581

0 commit comments

Comments
 (0)