Skip to content

Commit

Permalink
Improve _value type for single select: Allow arbitrary values (#7428)
Browse files Browse the repository at this point in the history
  • Loading branch information
laske185 authored Mar 4, 2025
2 parents 923f011 + afa7f73 commit 5948a63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Option, OptionsPropType, SelectOption, SingleSelectProps, SingleSelectWatches, W3CInputValue } from '../../schema';
import type { Option, OptionsPropType, SelectOption, SingleSelectProps, SingleSelectWatches, StencilUnknown, W3CInputValue } from '../../schema';
import { watchValidator } from '../../schema';
import { validateOptions, watchBoolean, watchString } from '../../schema';

import { InputIconController } from '../@deprecated/input/controller-icon';
Expand Down Expand Up @@ -42,8 +43,8 @@ export class SingleSelectController extends InputIconController implements Singl
watchBoolean(this.component, '_required', value);
}

public validateValue(value?: string): void {
watchString(this.component, '_value', value);
public validateValue(value?: StencilUnknown): void {
watchValidator(this.component, '_value', v => v !== undefined, new Set([`StencilUnknown`]), value);
}

public validatePlaceholder(value?: string): void {
Expand Down
15 changes: 8 additions & 7 deletions packages/components/src/components/single-select/shadow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ShortKeyPropType,
SingleSelectAPI,
SingleSelectStates,
StencilUnknown,
Stringified,
SyncValueBySelectorPropType,
TooltipAlignPropType,
Expand Down Expand Up @@ -47,11 +48,11 @@ export class KolSingleSelect implements SingleSelectAPI {
@Element() private readonly host?: HTMLKolSingleSelectElement;
private refInput?: HTMLInputElement;
private refOptions: HTMLLIElement[] = [];
private oldValue?: string;
private oldValue?: StencilUnknown;

@Method()
// eslint-disable-next-line @typescript-eslint/require-await
public async getValue(): Promise<string | undefined> {
public async getValue(): Promise<StencilUnknown | undefined> {
return this._value;
}

Expand Down Expand Up @@ -502,7 +503,7 @@ export class KolSingleSelect implements SingleSelectAPI {
/**
* Defines the value of the input.
*/
@Prop({ mutable: true, reflect: true }) public _value?: string;
@Prop({ mutable: true, reflect: true }) public _value?: StencilUnknown;

/**
* Defines the whether the clear button should be hidden.
Expand Down Expand Up @@ -615,7 +616,7 @@ export class KolSingleSelect implements SingleSelectAPI {
}

@Watch('_value')
public validateValue(value?: string): void {
public validateValue(value?: StencilUnknown): void {
this.controller.validateValue(value);
this.oldValue = value;
this.updateInputValue(value);
Expand All @@ -631,10 +632,10 @@ export class KolSingleSelect implements SingleSelectAPI {
this.blockSuggestionMouseOver = false;
}

private updateInputValue(value?: string) {
private updateInputValue(value?: StencilUnknown) {
if (Array.isArray(this._options)) {
const matchedOption = this._options.find((option) => (option as Option<string>).value === value);
this._inputValue = matchedOption ? (matchedOption.label as string) : '';
const matchedOption = this._options.find((option) => option.value === value);
this._inputValue = matchedOption ? String(matchedOption.label) : '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/schema/components/single-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type OptionalProps = {
msg: Stringified<MsgPropType>;
on: InputTypeOnDefault;
placeholder: string;
value: string;
value: StencilUnknown;
hideClearButton: boolean;
} & PropAccessKey &
PropDisabled &
Expand Down

0 comments on commit 5948a63

Please sign in to comment.