Skip to content

Commit

Permalink
feat(rdom-forms): add value type generics for selectXX/multiSelectXX
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 11, 2023
1 parent 25f95f0 commit 55d9897
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 6 additions & 4 deletions packages/rdom-forms/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ export interface SelectItem<T> {
desc?: string;
}

export interface SelectStr extends Select<string> {
export interface SelectStr<T extends string = string> extends Select<T> {
type: "selectStr";
}

export interface SelectNum extends Select<number> {
export interface SelectNum<T extends number = number> extends Select<T> {
type: "selectNum";
}

Expand All @@ -173,11 +173,13 @@ export interface MultiSelect<T> extends Value {
size?: number;
}

export interface MultiSelectStr extends MultiSelect<string> {
export interface MultiSelectStr<T extends string = string>
extends MultiSelect<T> {
type: "multiSelectStr";
}

export interface MultiSelectNum extends MultiSelect<number> {
export interface MultiSelectNum<T extends number = number>
extends MultiSelect<T> {
type: "multiSelectNum";
}

Expand Down
28 changes: 22 additions & 6 deletions packages/rdom-forms/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ type ReadonlyPartialSpec<T extends Value, V = string> = Omit<
};

const $ =
<T extends Value, R = string>(type: string, defaults?: Partial<T>) =>
(spec: PartialSpec<T> | ReadonlyPartialSpec<T, R>): T =>
<T extends Value, V = string>(type: string, defaults?: Partial<T>) =>
(spec: PartialSpec<T> | ReadonlyPartialSpec<T, V>): T =>
<any>{
id: spec.id || `${type}-${__nextID++}`,
type,
Expand All @@ -138,17 +138,13 @@ export const email = $<Email>("email", { autocomplete: true });
export const file = $<FileVal, never>("file");
export const month = $<Month>("month");
export const multiFile = $<MultiFileVal, never>("multiFile");
export const multiSelectNum = $<MultiSelectNum, number>("multiSelectNum");
export const multiSelectStr = $<MultiSelectStr>("multiSelectStr");
export const num = $<Num, number>("num");
export const password = $<Password>("password", { autocomplete: true });
export const phone = $<Email>("tel", { autocomplete: true });
export const radioNum = $<RadioNum, number>("radioNum");
export const radioStr = $<RadioStr>("radioStr");
export const range = $<Range, number>("range");
export const search = $<Str>("search");
export const selectNum = $<SelectNum, number>("selectNum");
export const selectStr = $<SelectStr>("selectStr");
export const str = $<Str, string>("str");
export const text = $<Text>("text");
export const time = $<Time>("time");
Expand All @@ -157,6 +153,26 @@ export const trigger = $<Trigger>("trigger");
export const url = $<UrlVal>("url");
export const week = $<Week>("week");

export const selectNum = <T extends number = number>(
spec: PartialSpec<SelectNum<T>> | ReadonlyPartialSpec<SelectNum<T>>
) => $<SelectNum<T>>("selectNum")(spec);

export const selectStr = <T extends string = string>(
spec: PartialSpec<SelectStr<T>> | ReadonlyPartialSpec<SelectStr<T>>
) => $<SelectStr<T>>("selectStr")(spec);

export const multiSelectNum = <T extends number = number>(
spec:
| PartialSpec<MultiSelectNum<T>>
| ReadonlyPartialSpec<MultiSelectNum<T>>
) => $<MultiSelectNum<T>>("multiSelectNum")(spec);

export const multiSelectStr = <T extends string = string>(
spec:
| PartialSpec<MultiSelectStr<T>>
| ReadonlyPartialSpec<MultiSelectStr<T>>
) => $<MultiSelectStr<T>>("multiSelectStr")(spec);

/** @internal */
const __genID = (id: string, opts: Partial<FormOpts>) =>
opts.prefix ? opts.prefix + id : id;
Expand Down

0 comments on commit 55d9897

Please sign in to comment.