Skip to content
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

Improve _value type for single select: Allow arbitrary values #7428

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
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;

@State() public state: SingleSelectStates = {
_hideMsg: false,
Expand Down Expand Up @@ -609,7 +610,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 @@ -620,10 +621,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;
} & PropAccessKey &
PropDisabled &
PropHideMsg &
Expand Down
Loading